0

我是 AppleScript 小伙子,离购买打字机和搬进山里只有片刻路程。

请任何人向我解释为什么会失败:

set mah_file to POSIX file "/Users/me/folder/fileinfo.txt"
set mah_data to "some text!!!!!!"

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 of 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

write_to_file(mah_data, mah_file, true)

即使这成功了:

set mah_file to choose file
set mah_data to "some text!!!"

-- the rest is identical

我试过了:

set mah_file to POSIX file "/Users/me/folder/fileinfo.txt" as file

set mah_file to POSIX file "/Users/me/folder/fileinfo.txt" as alias

set mah_file to POSIX file "/Users/me/folder/fileinfo.txt" as text

而且由于 AppleScript 懒得告诉我为什么这不起作用,我很好,真的疯了。

4

3 回答 3

3

省略fileandPOSIX file关键字:

set mah_file to "/Users/me/folder/fileinfo.txt"
...
set the open_target_file to open for access mah_file with write permission
于 2013-06-21T20:06:15.613 回答
1

read并且write默认使用主要编码(如 MacRoman 或 MacJapanese)。添加as «class utf8»以保留 UTF-8 文件中的非 ASCII 字符。

写作:

set b to open for access "/tmp/1" with write permission
set eof b to 0
write "α" to b as «class utf8»
close access b

附加:

set b to open for access "/tmp/1" with write permission
write "ア" to b as «class utf8» starting at eof
close access b

阅读:

read "/usr/share/dict/connectives" as «class utf8»
于 2013-06-21T22:29:25.217 回答
1

您可以结合使用 echo 命令和 >> 将文本写入文件

如果该纺织品不存在,则会创建一个新纺织品

如果文本文件已经存在并且

您使用 > 您可以覆盖现有的同名纺织品

您使用 >> 文本将被添加到现有文件中

set mypath to "/Users/" & (short user name of (system info)) & "/Desktop/file.txt" as string
set myText to "text to write in the text file" as string

do shell script "echo " & "\"" & myText & "\"" & ">>" & mypath

和 mypath 是和 POSIX 路径

希望这就是你正在寻找的

于 2013-06-24T13:46:55.373 回答