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

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

问题描述

请参考这个问题首先

<一个href=\"http://stackoverflow.com/questions/24559703/unable-to-read-opensslv-hno-such-file-or-directory\">unable阅读opensslv.h:没有这样的文件或目录

根据我需要运行下面的三线终端命令使用AppleScript,

  /tmp/ssl/openssl-1.0.1h/Configure darwin64-x86_64的-CC  -  preFIX =的/ usr没有线程共享
使-f /tmp/ssl/openssl-1.0.1h/Makefile
须藤使-f /tmp/ssl/openssl-1.0.1h/Makefile安装

我尝试了两种方法,我创建的文本文件的 .command .SH 扩展并且加入了上述三行。然后试图从AppleScript的为运行它,

 做shell脚本/Users/Username/Desktop/RunScript.sh

但得到这个错误,

 错误/Users/Username/Desktop/RunScript.sh:第1行:/tmp/ssl/openssl-1.0.1h/Configure:没有这样的文件或目录
/Users/Muhriz/Desktop/InstallOpenSSL.sh:2号线:品牌:找不到命令sudo的:没有TTY present并没有指定任何askpass程序1号

这可以工作,

 告诉应用程序终端来激活
告诉应用程序终端
    做脚本(/tmp/ssl/openssl-1.0.1h/Configure darwin64-x86_64的-CC - preFIX =的/ usr没有线程共享)的窗口1
    做脚本(使-f /tmp/ssl/openssl-1.0.1h/Makefile)的窗口1
    在窗口1脚本做(命令使-f /tmp/ssl/openssl-1.0.1h/Makefile安装)
告诉结束

但它要求在三线在终端密码,并等待用户的响应。从AppleScript的(使用时,具有管理员权限)中显示的密码对话框确定。但必须没有正在运行的命令时,通过终端索要密码。它需要只要求在执行的AppleScript一次运行所有须藤相关命令,而不在终端询问密码。

需要将用于从AppleScript的运行

什么code


解决方案

 做shell脚本/Users/Username/Desktop/RunScript.sh

这不起作用,因为你不能传递给路径中的做shell脚本命令,你只能通过它的实际脚本的内容。

如果您要运行包含在它自己的文件bash脚本,你可以用文本编辑打开bash脚本文件并设置文件的内容给一个变量,然后可以通过做shell脚本。

 告诉应用程序文字编辑
    设置theDesktopPath的路径到桌面文件夹中的文本
    设置theShellScriptPath为theDesktopPath&安培; RunScript.sh
    打开文件theShellScriptPath
    设置theShellScript以1号文件的案文
    设置theScriptResult做shell脚本theShellScript
    使新文档
    文件1的文本设置为theScriptResult
告诉结束

Please refer to this question first,

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

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

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"

But got this error,

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

This could work,

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

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.

What code needs to be used to run from AppleScript?

解决方案

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

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.

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天全站免登陆