【问题标题】:How do I write to a text file using AppleScript?如何使用 AppleScript 写入文本文件?
【发布时间】:2010-09-23 17:28:16
【问题描述】:

所以,就是这样。如何使用 AppleScript 写入文本文件?

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

【问题讨论】:

    标签: io applescript


    【解决方案1】:
    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
    

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

    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
    

    【讨论】:

    • 我发现此版本无法将非 ASCII 字符写入文本文件。此代码是否有 UTF-8 兼容版本?非 ASCII 字符显示为 ??在生成的文本文件中。
    • 这是一个很好的文件 IO 解决方案,其复杂性在于文件 IO 反映了经典 MacOS 中的调用,例如来自文件管理器的旧 FSReadFSWrite 调用。您需要为您打开的文件提供FSRef,然后在开始写入之前设置 EOF 以清除文件。
    • @kakyo,要使其兼容 utf8,请将 as «class utf8» 添加到第 8 行的代码中,使其变为 to write this_data to the open_target_file starting at eof as «class utf8»
    【解决方案2】:

    纯 AppleScript 的简短版本:

    set myFile to open for access (choose file name) with write permission
    write "hello world" to myFile
    close access myFile
    

    似乎没有原生的单一命令解决方案。相反,您必须打开并稍后关闭该文件。

    【讨论】:

    • 我相信如果文件已经存在并且比现在正在写入的文件长,那么这不会按预期工作 - 然后旧数据仍保留在文件中。要解决这个问题,您首先使用set eof of the myFile to 0 删除内容,如接受的答案所示
    【解决方案3】:

    @JuanANavarro。

    在使用 shell 时,您应该使用 引用形式的 作为 TEXT 和文件路径。 这将有助于阻止文件名中的空格和文本中的撇号等字符出现的错误。

    set someText to "I've also learned that a quick hack, if one only wants to spit a bit of text to a file, is to use the shell."
    
    set textFile to "/Users/USERNAME/Desktop/foo.txt"
    do shell script "echo  " & quoted form of someText & " >  " & quoted form of textFile
    

    上面的脚本运行良好。


    如果我没有 & someText 的引用形式

    但是我有 & someText 我会收到以下错误。

    error "sh: -c: line 0: unexpected EOF while looking for matching `''

    sh: -c: 第 1 行:语法错误:文件意外结束“编号 2”

    I've”中的撇号被视为命令的一部分。


    如果我有

    将 textFile 设置为“/Users/USERNAME/Desktop/some foo.txt” 作为我的文件路径(注意空格。)并且没有 & 引用的 textFile 形式 strong> 但我有 & textFile

    然后,当文件被写出时,它会写入名为“some”而不是“some foo.txt”的文件

    【讨论】:

      【解决方案4】:

      我还了解到,如果只想将一些文本吐出到文件中,那么快速破解就是使用 shell。

      do shell script "echo TEXT > some_file.txt"
      

      【讨论】:

      • 请参阅我对此的回答。
      • 如果TEXT 包含"$()'! 等字符,将失败。你必须告诉 Applescript 首先使用 quoted form of 引用文本
      【解决方案5】:

      对我来说,在 PowerBook G4 上运行 do shell 脚本在循环执行 300000 次时太慢了;),但当然写起来更快,这有时是有道理的。您还想像这样转义 shell 字符:

      执行 shell 脚本 "echo " & foobar & " >> some_file.txt" 的引用形式

      出于审美原因,我会使用

      告诉我做shell脚本“#...”

      但我还没有验证(我相信)如果“do shell script”在“tell Finder”的块中,例如它是 Finder 进程创建一个子shell。使用“告诉我执行 shell 脚本”,至少脚本编辑器日志对我来说看起来更好。 ;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-27
        相关资源
        最近更新 更多