2013-03-24 198 views
16

我試圖從終端運行AppleScript腳本,但我不能得到它通過調用打印到標準輸出使用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 

標籤是不是真的出了好這個:(

+0

可能的重複[從osascript/Applescript打印到標準輸出](http://stackoverflow.com/questions/8766868/print-to-stdout-from-osascript-applescript) – 2013-11-14 09:34:30

回答

7

它是不是很清楚你如何在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 

更新 2,在對效應初探意見。

我若與內容作爲標籤定界文本文件:

track Skin Deep Beady Belle Closer 

的選項卡設置,如: 軌道* TAB皮膚深層TAB明眸美女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 

我回去: 宋:皮膚深層 - 明眸美女 - 更緊密

+0

嗯。這似乎也不起作用。更新我如何運行它。感謝您指出:) – Kristin 2013-03-24 23:52:20

+0

我明白了。如何發佈更多的代碼。這可能有助於更好地瞭解你在做什麼。因爲任何一種跑步方式都會產生一些東西你在碼頭回來什麼? – markhunte 2013-03-25 00:06:15

+0

是的,所以我不僅在腳本崩潰的時候出現錯誤,但是當它在AppleScript Editor中運行時,所有的日誌數據都會出現。我會張貼更多的代碼。 – Kristin 2013-03-25 00:09:13

12

當使用#!/usr/bin/osascript運行AppleScript,您可以在腳本結尾處使用return語句返回所需的文本輸出。

+2

這比一個單獨的shell進程產生一個更簡單的方法來打印另一個進程的輸出。在上面的例子中,這看起來就像在'結束運行'行之前'返回輸出的引用形式'。 – Beejor 2015-05-05 01:50:20

+0

這要好得多。謝謝 – fabrizioM 2015-10-11 06:44:52

+5

但是,如果能夠在遇到文本時能夠輸出文本塊,它肯定會很好,而不必將它們全部堆積成大字符串,然後必須在最後返回那個大字符串。 – 2016-03-04 19:05:20

相關問題