当前位置: 首页 > 知识库问答 >
问题:

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

花玄裳
2023-03-14

请先参考这个问题,

无法读取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

我尝试了两种方法,我创建了带有.命令和. 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运行需要使用哪些代码?


共有2个答案

蒋寒
2023-03-14
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
孙俊彦
2023-03-14

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

chmod a+x /Users/Username/Desktop/RunScript.sh
xattr -r -d "com.apple.quarantine" /tmp/ssl/openssl-1.0.1h
 类似资料:
  • 我正在尝试创建一个bash。sh脚本文件,使用osascript打开一个新的终端窗口/选项卡,然后运行两个命令“cd fs”和“gulp”(都在同一窗口/选项卡中) 这就是我正在尝试的,它在另一个(第三个)窗口/选项卡中运行第二个命令(做脚本“大口吃”),而不是打开的第二个。

  • 我有一个简单的问题涉及child_proccess。如果从脚本调用child_process,它将在该脚本中运行该命令。如果您导出运行该命令的函数,对于我测试的内容,它会尝试在库终端而不是当前终端中运行该命令。示例:假设您有导出到 b 的脚本 a.js.js在 a 中您有一个名为 hello 的函数,它使用 echo 命令打印“ World”: 当你运行它时,它不会给你发送任何东西。如果用cons

  • 问题内容: 我一直在寻找使用Java运行时运行外部程序的方法。可以正常工作,例如: 如您所愿创建一个新目录。现在,在Mac的bash窗口中,我可以这样写: 在名为testgame的文件夹上运行“ Love”游戏引擎。现在,之所以可行,是因为我将“ love”作为别名来调用love可执行文件。我有一种感觉,这是下面没有理由 不 工作: 而且(对于那些想知道的人)也不是: 毫无疑问,这要么是Java方

  • 问题内容: 我想运行一些命令,直到按Ctrl-C才会退出。是否可以运行一次即可运行所有这些程序,而Ctrl-C会全部退出它们?他们可以共享终端输出。 具体来说,我有罗盘编译器,coffeescript编译器和一个自定义命令,用于监视文件更改,所有命令都在运行以监视文件更改。我不想为每个命令加载一个终端。 问题答案: 该bash脚本适用于N个并行线程。每个参数都是一个命令。 捕获SIGINT时将杀死

  • 我希望在中运行多个命令,如下所示: 那么如何保存上一个命令的状态并将其用于下一个命令??

  • 问题内容: 如何通过android app向终端发送命令并获取输出?例如,发送“ ls /”并获取输出以在GUI中将其打印出来? 问题答案: 您必须使用反射来调用android.os.Exec.createSubprocess():