如何从 AppleScript 在终端中运行多行命令? [英] How to run multiple lines of commands in Terminal from AppleScript?

查看:63
本文介绍了如何从 AppleScript 在终端中运行多行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请先参考这个问题,

无法读取 opensslv.h:没有那个文件或目录

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

Based on that I need to run the following three line Terminal commands using 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 运行它,

I tried two methods I created text files with .command and .sh extensions and added the above three lines. Then tried to run it from AppleScript as,

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 相关命令,而无需在终端中询问密码.

But it asks for password in Terminal at the third line and waits for user response. The password dialog shown from the AppleScript (when using with administrator privileges) is OK. But it must no ask for password via Terminal when running the commands. It needs to ask only once when the AppleScript is executed and run all sudo related commands without asking for password in Terminal.

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

What code needs to be used to run from AppleScript?

推荐答案

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

这行不通,因为您无法向do shell script"命令传递路径,您只能将实际脚本的内容传递给它.

That doesn’t work because you can’t pass a path to the "do shell script" command, you can only pass it the contents of the actual script.

如果你想运行一个包含在它自己的文件中的 bash 脚本,你可以使用 TextEdit 打开 bash 脚本文件并将文件的内容设置为一个变量,然后你可以传递给do shell script."

If you want to run a bash script that is contained in its own file, you can use TextEdit to open the bash script file and set the contents of the file to a variable, which you can then pass to "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

这篇关于如何从 AppleScript 在终端中运行多行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆