2012-07-01 87 views
4

我想知道是否可以讓applescript應用程序運行shell腳本,然後在shell腳本執行完成之前退出。在給定的時間內運行服務器時,這會很有用。不需要在後臺不斷運行蘋果腳本,有沒有辦法獨立運行一個函數?執行shell腳本後退出applescript應用程序

set server_name to text returned of (display dialog "Choose server." default answer "") 
set success to do shell script "if [ -f \"/Users/jessefrohlich/Documents/Minecraft/" & server_name & "/minecraft_server.jar\" ]; then 
    echo 1; 
else 
    echo 0; 
fi" 
if success is equal to "1" then 
    do shell script "cd /Users/jessefrohlich/Documents/Minecraft/" & server_name & " 
    java -Xmx1024M -Xms1024M -jar minecraft_server.jar" 
else 
    display dialog "Sorry, the file you chose is invalid." 
end if 

當上述情況作爲應用程序運行時,它將正常啓動服務器。但是,應用程序runScript.app將繼續運行。即使蘋果強制退出,服務器也將繼續運行。一旦服務器啓動,有沒有辦法讓它自動退出?

謝謝

回答

4

試試這個。祝你好運。

-- "> /dev/null" redirects standout out to nowhere land 
--  - you can use some other file path if you want to capture its output 
-- "2>&1" redirects standard error to the same place as standard out 
--  - 2 stands for standard error 
--  - 1 stands for standard out 
--  - > is the redirect symbol 
--  - & changes redirect's output from a file to a file descriptor (in this case standard out) 
-- & the trailing & sends the process to the background 

do shell script "cd /Users/jessefrohlich/Documents/Minecraft/" & server_name & " java -Xmx1024M -Xms1024M -jar minecraft_server.jar > /dev/null 2>&1 &" 
+0

感謝您的幫助! – Phro

1

您可以添加一個條件,你的AppleScript有它「忽略應用程序響應」,然後它會繼續到其他任何在你的AppleScript,包括戒菸吧。

該蘋果網站有詳細信息:https://developer.apple.com/library/mac/#documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_control_statements.html

+2

'忽略應用程序響應'。此控制語句不適用於'do shell腳本'命令。 – jackjr300

+0

謝謝,我沒有意識到這一點。正確的方法是將腳本放在後臺並將響應重定向到文件或/ dev/null。請參閱「其他問題」一節:https://developer.apple.com/library/mac/technotes/tn2065/_index.html – Dave