2011-08-06 49 views
12

我遇到了使用一小部分Applescript的shell腳本的問題。當我用Applescript編輯器進行編譯時,它可以工作。它不通過shell腳本。osascript /語法錯誤:預計結束行但找到命令名稱。 (-2741)

44:49: syntax error: Expected end of line but found command name. (-2741) 23:28: syntax error: Expected end of line but found 「after」. (-2741)

這裏是shell代碼:

osascript -e 'tell application "System Events" -e 'activate' 

osascript -e 'tell process "Application 10.5" -e 'set frontmost to true' -e 'end tell' 

osascript -e 'delay 1' -e 'keystroke return' -e 'delay 1' -e 'keystroke return' 

end tell 

AppleScript的(即作品):

tell application "System Events" 
activate 
tell process "Application 10.5" 
    set frontmost to true 
end tell 

delay 1 
keystroke return 
delay 1 
keystroke return 

end tell 

[更新]/[解決]

這照顧了我嘗試修改applescript以在其中工作時出現任何類型的問題地獄腳本:

## shell script code 

echo "shell script code" 
echo "shell script code" 

## applescript code 

osascript <<EOF 
tell application "Scriptable Text Editor" 
    make new window 
    activate 
    set contents of window 1 to "Hello World!" & return 
end tell 
EOF 

## resume shell script... 

這是非常酷的,你是能夠直接把純粹的AppleScript到一個shell腳本。 ;-)

回答

1

除了使用-e標誌的,你可以簡單地存儲您的AppleScript代碼在一個小的文本文件,並調用

osascript /path/to/script 

另外,如果你告訴一個應用程序或進程做只有一件事,你可以寫這樣的:

tell process "MyProcess" to perform action. 

現在我想想,單獨運行的每個符合-e標誌,因爲我不認爲所有的線將連接可能無法工作並作爲一個程序運行。例如,我剛剛使用osascript -e測試來設置變量。然後我使用了一個單獨的osascript -e讀取變量,但它不能。

[*]

+1

感謝您的解釋。我實際上已經想出了一個很好的方法,可以將applescript直接放到shell腳本中,而無需經過修改它的所有工作。 (往上看) - –

5

每個osascript(1)命令是完全獨立的過程,並因此一個完全獨立的腳本,所以不能將它們之間使用狀態(例如變量)。您可以使用多個-e選項在osascript中構建一個多行腳本 - 它們都與它們之間的換行連接以形成腳本。對於一個足夠長的腳本,一個單獨的文件或一個「here文檔」,就像你在最終的解決方案中使用的那樣,是一個很好的方法。

另外,如果你的腳本主要是(!或全​​部)的AppleScript,你可以簡單地被使用的AppleScript調用osascript一個認領文件中的「殼」腳本:

#!/usr/bin/osascript 
display dialog "hello world" 

...然後根據需要使用do shell script

相關問題