使用applescript打印到Stdout

kb_*_*kb_ 18 applescript

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

 osascript myFile.scpt "/path/to/a/file"
Run Code Online (Sandbox Code Playgroud)

我尝试着:

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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

标签在这方面并没有真正表现出来:(

Rap*_*ert 16

运行AppleScript时#!/usr/bin/osascript,只需在脚本末尾返回所需的文本输出和return语句即可.

  • 但是当它们遇到时只能输出文本块肯定会很好,而不是必须将它们全部累积成一个大字符串,然后必须在最后返回那个大字符串. (5认同)
  • 这比生成单独的shell进程只是为了打印另一个的输出更简单.在上面的例子中,这看起来像`在`end run`行之前返回输出`的引用形式. (2认同)

mar*_*nte 8

它不是很清楚你如何试图在Termanil中运行它.但我假设你已经用#!/ usr/bin/osascript保存了一个applescript文本文件,chmod'ed该文件能够执行它.

然后在终端中调用该文件.只需使用文件的路径.

更新:使用echo

#!/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
Run Code Online (Sandbox Code Playgroud)

更新 2,回应评论.

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

track   Skin Deep   Beady Belle Closer
Run Code Online (Sandbox Code Playgroud)

标签设置如下:track*TAB Skin Deep TAB Beady Belle TAB*更接近

并且脚本文件为:

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
Run Code Online (Sandbox Code Playgroud)

在终端运行:

/usr/bin/osascript ~/Documents/testOsa2.scpt ~/Documents/testTab.txt
Run Code Online (Sandbox Code Playgroud)

我回来了: 宋:皮肤深 - Beady Belle - 更接近


小智 5

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

copy "Hello World!" to stdout
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果您执行多个这样的输出,则只有最后一个可见。解决方案是在进行过程中保存字符串(`set FinalString to FinalString & CurrString & "\n"`)并在末尾输出连接的字符串。 (5认同)