【问题标题】:How to run multiple lines of commands in Terminal from AppleScript?如何从 AppleScript 在终端中运行多行命令?
【发布时间】:2014-07-06 00:29:37
【问题描述】:

请先参考这个问题,

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 运行需要使用哪些代码?

【问题讨论】:

    标签: macos bash shell terminal applescript


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多