在 Xcode 中使用 AppleScript App 读取文本文件

Read text file with AppleScript App in Xcode

我想在 Xcode

的 AppleScript 应用程序中读取文本文件
set TextOfFile to read file "Macintosh HD:Users:myself:Desktop:Text.txt" using delimiter linefeed
set FirstText to item 1 of TextOfFile
log FirstText

总是提前致谢!

编辑:

我写了这段代码(上面),它成功地读取了 AppleScript 中的文本文件,但是当我在 Xcode 中的 AppleScript 应用程序中使用相同的代码时,它不起作用。

在很多情况下,如果不理解常规的 AppleScript 命令,您可以解决当前应用程序(注意获取段落 将使用各种行结尾):

tell current application to set textOfFile to paragraphs of (read file "Macintosh HD:Users:myself:Desktop:Text.txt")
set firstText to item 1 of textOfFile
log firstText

如果您喜欢冒险,可以使用多种 Cocoa 方法,例如:

set textOfFile to (current application's NSString's stringWithContentsOfFile:"/Users/myself/Desktop/Text.txt" encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)) as text
set firstText to item 1 of paragraphs of textOfFile
log firstText