Applescript用于在现有textedit文档中添加和附加文本

时间:2015-12-14 17:02:29

标签: applescript

我是使用AppleScript的新手。我想编写一个脚本来向现有文件添加文本。具体来说,我想在前面添加/附加给定文档,例如,在“Hello”[文档中的现有文本]前加上“Goodbye”。我找到了这个例子:

tell application "TextEdit"
activate
set theDesktopPath to the path to the desktop folder as text
set file_URLs_content to "HEEEELLOOOOOO"
make new document with properties {text:file_URLs_content}
save document 1 in file (theDesktopPath & "file.txt")
close document 1
end tell

但是,这不太正确,因为我不需要新文档,我想指定文本的位置(在文件的开头或结尾)。

我也对其他解决方案持开放态度。最终,我想使用听写命令功能来运行脚本,也许是通过automator。非常感谢你的时间!

1 个答案:

答案 0 :(得分:0)

将纯文本写入磁盘不需要TextEdit。

writeToDisk from theText into thePath given append:append将纯文本写入磁盘

参数:

theFile:HFS路径
theText要写入的文本
with append:附加文字 - without append:从文字开头覆盖

set theFile to (path to desktop as text) & "file.txt"

set file_URLs_content to "HEEEELLOOOOOO"
writeToDisk from file_URLs_content into theFile with append

on writeToDisk from theText into thePath given append:append
    try
        set fileDescriptor to open for access file thePath with write permission
        if not append then set eof of fileDescriptor to 0
        write theText to fileDescriptor
        close access fileDescriptor
    on error
        try
            close access file thePath
        end try
    end try
end writeToDisk

如果您更喜欢编写UTF-8编码文本,请将write行更改为

write theText to fileDescriptor as «class utf8»
相关问题