0

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

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

这被证明是相当困难的,因为 AppleScript 创建新文件与写入现有文件的语法不同。我还考虑过删除已经存在的文件,然后创建它,但 AppleScript 默认会将内容移至垃圾箱,因此即使这样也相当复杂。

我要写入文件的数据中可能有很多特殊字符,所以我真的不想做类似的事情,do shell script "cat " + data + " > /tmp/txt.txt"因为我不知道escapeshellargAppleScript 有什么。

以下是我到目前为止的内容,但出现错误:

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

我真的很想通过改进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 创建或替换文件?

4

2 回答 2

2

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

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

或使用创建 UTF-8 文件的 do shell 脚本,但给定的路径现在需要是 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

选择你喜欢的那一款

于 2012-12-03T19:56:53.167 回答
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
于 2012-12-03T05:20:23.220 回答