如何在AppleScript的终端中运行多行命令?

时间:2014-07-06 00:29:37

标签: macos bash shell terminal applescript

请先参考这个问题,

unable to read opensslv.h:No such file or directory

基于此我需要使用AppleScript运行以下三行终端命令,

/tmp/ssl/openssl-1.0.1h/Configure darwin64-x86_64-cc ––prefix=/usr no-threads shared
make -f /tmp/ssl/openssl-1.0.1h/Makefile
sudo make -f /tmp/ssl/openssl-1.0.1h/Makefile install

我尝试了两种使用 .command .sh 扩展名创建文本文件的方法,并添加了以上内容三条线。然后尝试从AppleScript中运行它,

do shell script "/Users/Username/Desktop/RunScript.sh"

但是得到了这个错误,

error "/Users/Username/Desktop/RunScript.sh: line 1: /tmp/ssl/openssl-1.0.1h/Configure: No such file or directory
/Users/Muhriz/Desktop/InstallOpenSSL.sh: line 2: make: command not found sudo: no tty present and no askpass program specified" number 1

这可行,

tell application "Terminal" to activate
tell application "Terminal"
    do script ("/tmp/ssl/openssl-1.0.1h/Configure darwin64-x86_64-cc ––prefix=/usr no-threads shared") in window 1
    do script ("make -f /tmp/ssl/openssl-1.0.1h/Makefile") in window 1
    do script ("sudo make -f /tmp/ssl/openssl-1.0.1h/Makefile install") in window 1
end tell

但是它要求在第三行的终端中输入密码并等待用户响应。 AppleScript显示的密码对话框(使用with administrator privileges时)是正常的。但是在运行命令时,它不能通过终端请求密码。执行AppleScript时只需询问一次并运行所有sudo相关命令而不在终端中请求密码。

需要使用哪些代码才能从AppleScript运行?

2 个答案:

答案 0 :(得分:1)

在运行脚本之前执行以下操作。

chmod a+x /Users/Username/Desktop/RunScript.sh
xattr -r -d "com.apple.quarantine" /tmp/ssl/openssl-1.0.1h

答案 1 :(得分:1)

do shell script "/Users/Username/Desktop/RunScript.sh"

这不起作用,因为您无法将路径传递给“do shell script”命令,您只能将其传递给实际脚本的内容。

如果要运行包含在自己文件中的bash脚本,可以使用TextEdit打开bash脚本文件并将文件内容设置为变量,然后可以将其传递给“do shell script” 。”

tell application "TextEdit"
    set theDesktopPath to the path to the desktop folder as text
    set theShellScriptPath to theDesktopPath & "RunScript.sh"
    open file theShellScriptPath
    set theShellScript to the text of document 1
    set theScriptResult to do shell script theShellScript
    make new document
    set the text of document 1 to theScriptResult
end tell