Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 如何从AppleScript在终端中运行多行命令?_Macos_Bash_Shell_Terminal_Applescript - Fatal编程技术网

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

Macos 如何从AppleScript在终端中运行多行命令?,macos,bash,shell,terminal,applescript,Macos,Bash,Shell,Terminal,Applescript,请先看这个问题, 基于此,我需要使用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运行以下三行终端命令

/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显示的密码对话框(使用具有管理员权限的
时)正常。但在运行命令时,不得通过终端请求密码。在执行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”命令传递路径,只能向其传递实际脚本的内容

如果要运行包含在自己文件中的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
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