2015-03-03 264 views
0

我一直在想如何將多個參數從Applescript傳遞給終端命令腳本。例如運行終端命令文件時,您可以以編程方式接收參數,像這樣:從AppleScript傳遞多個參數到終端命令腳本

#!/bin/bash 

var=$1 

var=$2 

,我一直在使用的AppleScript的代碼如下供參考:

tell application "System Events" to set app_directory to POSIX path of (container of (path to me)) 

set thisFile to "Dev" 

set testTarget to "/Users/lab/Desktop/TestTarget/" 

do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & testTarget with administrator privileges 

在哪裏,我想我出錯了是第二個參數的輸入。當我只有一個參數時,它通過了很好:

do shell script "/path/to/command/mycommand.command" &var with administrative privileges 

我很好奇什麼正確的語法將傳遞在這第二個參數。如果有人有任何建議,請讓我知道!另外如果您需要更多信息,我會很樂意提供!

回答

1

你只需要在你的參數之間添加一個空格。目前,在thisFiletestTarget之間沒有添加空間。你的命令如下:

/Users/lab/Desktop/TempRoot/mycommand.command Dev/Users/lab/Desktop/TestTarget/ 

更改你的shell腳本行:

do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges 

東西,我建立一個腳本時,找到有用的就是確保我的shell命令運行之前正確的。因此,不要直接構建它,請將該命令存儲在變量中並記錄下來。稍後,將日誌語句替換爲do shell script命令。

set shellScript to "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges 
log shellScript 
-- do shell script shellScript 
+0

這工作!我會upvote這篇文章,但顯然我沒有足夠的stackoverflow點:(但非常感謝你! – mmilutinovic1313 2015-03-03 14:56:21

+0

@ mmilutinovic1313:你不需要聲望點_accept_ _yours_問題的答案(點擊複選標記)。 – mklement0 2015-03-03 17:10:56

+1

另一個值得一提的提示是:爲了確保字符串(如文字)文件名被傳遞到未經修改的shell(爲了保護字符串不受shell擴展),以「'quoted form of」作爲前綴。 – mklement0 2015-03-03 17:13:09

0

這是在@Darrick Herwehe's中發佈的答案的簡短描述。我只需要添加空格關鍵字和命令完美工作。非常感謝!

do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges