20

我正在尝试从终端运行 AppleScript 脚本,但是我无法通过调用它来打印任何内容

 osascript myFile.scpt "/path/to/a/file"

我正在努力:

on run fileName

set unique_songs to paragraphs of (read POSIX file fileName)

repeat with nextLine in unique_songs
    if length of nextLine is greater than 0 then
        set AppleScript's text item delimiters to tab
        set song to text item 2 of nextLine
        set artist to text item 3 of nextLine
        set album to text item 4 of nextLine

        set output to ("Song: " & song & " - " & artist & " - " & album)
        copy output to stdout
    end if
end repeat
end run

制表符分隔文件的格式如下:

1282622675  Beneath the Balcony Iron & Wine The Sea & the Rhythm    
1282622410  There Goes the Fear Doves   (500) Days of Summer        
1282622204  Go to Sleep. (Little Man Being Erased.) Radiohead   Hail to the Thief

选项卡在此方面并没有很好地显示:(

4

3 回答 3

17

使用 运行 AppleScript 时#!/usr/bin/osascript,您可以在脚本末尾使用 return 语句返回所需的文本输出。

于 2013-11-14T09:34:10.687 回答
11

它不是很清楚你是如何在终端中运行它的。但我会假设你已经用 #!/usr/bin/osascript shebang保存了一个 applescript 文本文件,并 chmod'ed 文件以便能够执行它。

然后在终端中调用该文件,只需使用文件的路径即可。

#!/usr/bin/osascript

#Here be the rest of your code ...

set output to ("Song: " & song & " - " & artist & " - " & album)


    do shell script "echo " & quoted form of output
end tell

更新2,回应评论。

如果我有一个制表符分隔的文本文件,其内容为:

track   Skin Deep   Beady Belle Closer

标签设置如下:track****TAB****Skin Deep****TAB****Beady Belle****TAB****Closer

脚本文件为:

on run fileName

    set unique_songs to paragraphs of (read POSIX file fileName)

    repeat with nextLine in unique_songs
        if length of nextLine is greater than 0 then
            set AppleScript's text item delimiters to tab
            set song to text item 2 of nextLine
            set artist to text item 3 of nextLine
            set album to text item 4 of nextLine

            set output to ("Song: " & song & " - " & artist & " - " & album)
            do shell script "echo " & quoted form of output
        end if
    end repeat

end run

然后在终端运行:

/usr/bin/osascript ~/Documents/testOsa2.scpt ~/Documents/testTab.txt

我回来了:

*Song: Skin Deep - Beady Belle - Closer*
于 2013-03-24T23:49:03.897 回答
5

根据第一个答案弄清楚了这一点:

copy "Hello World!" to stdout
于 2020-01-02T09:42:09.300 回答