如何使用 AppleScript 写入文本文件? [英] How do I write to a text file using AppleScript?

查看:66
本文介绍了如何使用 AppleScript 写入文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原来如此.如何使用 AppleScript 写入文本文件?

So, that's it. How can I write to a text file using AppleScript?

我试过在谷歌上搜索,但答案似乎已经有好几年了,我不太确定现在什么应该是首选的成语.

I've tried googling around, but answers seem to be years old and I'm not really sure what should be the preferred idiom this days.

推荐答案

on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
    try
        set the target_file to the target_file as text
        set the open_target_file to ¬
            open for access file target_file with write permission
        if append_data is false then ¬
            set eof of the open_target_file to 0
        write this_data to the open_target_file starting at eof
        close access the open_target_file
        return true
    on error
        try
            close access file target_file
        end try
        return false
    end try
end write_to_file

与它的接口可以通过以下方式清理...

Interfacing with it can be cleaned up with the following...

my WriteLog("Once upon a time in Silicon Valley...")

on WriteLog(the_text)
    set this_story to the_text
    set this_file to (((path to desktop folder) as text) & "MY STORY")
    my write_to_file(this_story, this_file, true)
end WriteLog

这篇关于如何使用 AppleScript 写入文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆