Applescript在现有文本编辑文档中添加和附加文本

Applescript在现有文本编辑文档中添加和附加文本,applescript,Applescript,我不熟悉使用applescript。我想写一个脚本,将文本添加到现有文件中。具体来说,我想在给定文档前加上前缀/附加,例如,在“Hello”[文档中的现有文本]前加上“再见”。我发现了以下示例: tell application "TextEdit" activate set theDesktopPath to the path to the desktop folder as text set file_URLs_content to "HEEEELLOOOOOO" make new docu

我不熟悉使用applescript。我想写一个脚本,将文本添加到现有文件中。具体来说,我想在给定文档前加上前缀/附加,例如,在“Hello”[文档中的现有文本]前加上“再见”。我发现了以下示例:

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。非常感谢您的时间

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

将磁盘从文本写入给定路径append:append
将纯文本写入磁盘

参数:

文件
:HFS路径
文本
要编写的文本
带追加
:追加文本-
不带追加
:从文本开头覆盖

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 theText to fileDescriptor as «class utf8»

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

将磁盘从文本写入给定路径append:append
将纯文本写入磁盘

参数:

文件
:HFS路径
文本
要编写的文本
带追加
:追加文本-
不带追加
:从文本开头覆盖

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 theText to fileDescriptor as «class utf8»

非常感谢。这很有帮助。谢谢!!这很有帮助。