繁体   English   中英

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

[英]How to run multiple lines of commands in Terminal from AppleScript?

请先参考这个问题,

无法读取opensslv.h:无此文件或目录

基于此,我需要使用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运行需要使用什么代码?

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

chmod a+x /Users/Username/Desktop/RunScript.sh
xattr -r -d "com.apple.quarantine" /tmp/ssl/openssl-1.0.1h
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM