如何使用Applescript创建或替换文件?

时间:2012-12-03 03:45:27

标签: macos applescript

我正在尝试使用AppleScript将内容写入文件/tmp/test.txt

  • 如果文件不存在,我想创建它。
  • 如果确实存在,我想替换内容。

这非常困难,因为AppleScript创建新文件与写入现有文件的语法不同。我还考虑删除一个文件,如果它已经存在,然后创建它,但AppleScript会默认将其移动到垃圾箱,所以即使这样也相当复杂。

我要写入文件的数据可能包含很多特殊字符,所以我真的不想做do shell script "cat " + data + " > /tmp/txt.txt"之类的事情,因为我不知道任何escapeshellarg对于AppleScript。

以下是我到目前为止所得到的错误:

  

无法将文件“Macintosh HD:private:tmp:test.txt”转换为类型引用。

我真的想通过改进我得到的here辅助函数write_to_file来抽象这一点,这样如果文件不存在就会创建文件。

my write_to_file("hello", "/tmp/test.txt", false)

on write_to_file(this_data, target_file, append_data)
    try

        if not (exists POSIX file target_file) then
            make new document file at ("/tmp/" as POSIX file as alias) with properties {name:"test.txt", text:the_data}
        end if

        set the target_file to the target_file as POSIX file
        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 e
        try
            display dialog e
            close access file target_file
        end try
        return false
    end try
end write_to_file

如何使用Applescript创建或替换文件?

2 个答案:

答案 0 :(得分:2)

您犯的第一个错误是将POSIX路径与HFS路径混合在一起。 posix路径是/tmp/test.txt,同一文件的hfs路径是Macintosh HD:tmp:test.txt,如果启动卷的名称当然是Macintosh HD。

open for access file "Volume:Path:to:file.txt"

第二个错误是对于Mac OS X,/ tmp文件夹不是存储临时文件的地方。您将需要管理员权限才能在那里写入,系统将提示用户输入用户名和密码。它存储在/ var / folders中,每个用户都有一个登录时创建的文件夹,也可以快速切换用户。要访问此文件夹的路径,您应该使用临时项的命令路径。初创公司之间将删除临时项目。

set theFile to (path to temporary items as string) & "test.txt"

第三个错误是,当您使用引用的形式时,您可以使用do shell脚本。由于AppleScript是像shell这样的unicode,因此do shell脚本无法处理这些字符。

do shell script "/bin/echo -n " & (quoted form of "$@%^&*()@") & " > " POSIX path of theFile

最后你犯了一个错误,你需要检查是否存在文件。当您使用open for access命令时,如果文件不存在,将创建该文件。所以你需要的唯一代码是:

set theFile to (path to temporary items as text) & "test.txt"
writeToFile(theFile, "Hello!", false)
writeToFile(theFile, (linefeed & "Goodbye!" as string), true)

on writeToFile(p, d, a)
    try
        set fd to open for access file p with write permission
        if not a then set eof of fd to 0
        write d to fd starting at (get eof of fd) as «class utf8»
        close access fd
    on error
        close access file p
    end try
end writeToFile

或者使用do shell脚本创建一个UTF-8文件,但是给定的路径现在需要是一个POSIX路径

set theFile to (path to temporary items as text) & "test.txt"
writeToFile(POSIX path of theFile, "goodbye!", false)
writeToFile(POSIX path of theFile, (linefeed & "hello!" as string), true)

on writeToFile(p, d, a)
    set cmd to "/bin/echo -n " & quoted form of d & " >"
    if a then set cmd to cmd & ">"
    do shell script cmd & space & quoted form of p
end writeToFile

选择你喜欢的那个

答案 1 :(得分:1)

尝试:

my write_to_file("hello", "/tmp/test.txt", true)

on write_to_file(this_data, target_file, append_data)
    try
        tell application "System Events" to exists file target_file
        if not the result then do shell script "> " & quoted form of target_file
        set the open_target_file to open for access 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 e
        try
            display dialog e
            close access target_file
        end try
        return false
    end try
end write_to_file