Applescript使用输入和管理员权限执行命令行管理程序

时间:2014-05-28 07:55:25

标签: bash shell applescript

我试图编写一个自动取款服务来启动终端中的virtualhost.sh

使用“服务”上下文菜单打开对话框,询问虚拟主机的名称,然后运行AppleScript以启动终端并传入输入文本。

我想要的是将我的用户名和密码传递给管理员权限,这样我就不需要在sudo终端中传递它。

这可以使用do shell script完成,但执行bin/shvirtualhost.sh是bash脚本,因此我收到错误bin/sh: virtualhost.sh command not found

或者我可以使用do script with command,但这并不允许我传入用户名和密码。

我的代码如下:

on run {input, parameters}
    set vhost to "virtualhost.sh " & input
    tell application "Terminal"
        activate
        do shell script vhost user name "user" password "pass" with 
                  administrator privileges

    end tell
end run

这会产生前面提到的bin / sh错误。

使用do script with command

on run {input, parameters}
    set vhost to "virtualhost.sh " & input
    tell application "Terminal"
        activate
        do script with command vhost user name "user" password "pass" with
                  administrator privileges

    end tell
end run

这会产生一个转义错误:预期的行尾等,但找到属性。

有没有办法正确地做到这一点?

2 个答案:

答案 0 :(得分:6)

不熟悉AppleScript Studio,但如果您提供virtualhost.sh的完整路径,则可以使用普通的旧AppleScript(看起来具有相同的问题)。 (此外,“执行shell脚本”不需要终端。)示例:

set vhost to "/usr/local/bin/virtualhost.sh " & input
do shell script vhost user name "user" password "pass" ¬
    with administrator privileges

您还可以扩展$ PATH(默认情况下为/ usr / bin:/ bin:/ usr / sbin:/ sbin with“do shell script”)以包含virtualhost.sh的路径,例如:

set vhost to "{ PATH=$PATH:/usr/local/bin; virtualhost.sh " & input & "; }"
do shell script vhost user name "user" password "pass" ¬
    with administrator privileges

如果需要相对路径,可以将virtualhost.sh放在脚本应用程序或包中(例如,在内容/资源中),在终端中或通过按住Control并单击并选择“显示包内容”。然后使用“path to me”:

set vhostPath to "'" & POSIX path of (path to me) & ¬
    "/Contents/Resources/virtualhost.sh" & "'"
set vhost to vhostPath & space & input
do shell script vhost user name "user" password "pass" ¬
    with administrator privileges

答案 1 :(得分:0)

根据对my other answer的评论,我发布了一个次要答案,更多的是本着一种意志的精神,有一种方法,但这是一种不同的,更危险的方法。但是,这是我能想到的唯一解决方案(获得终端的交互性,但无需在以管理员身份运行脚本时提示输入管理员密码)。

此解决方案以root身份运行终端,这是do shell script "command" with administrator privileges所做的。这是危险的,因为您有一个具有root访问权限的开放式终端窗口,因此请仔细权衡利益,例如打开新的终端窗口并以root身份进入Bash提示符。

由于这个原因,打开的终端实例在脚本完成时被终止;如果删除了“kill”命令,请注意您将获得多个Terminal实例,而不是同一实例中的多个窗口。

不知道这是否适用于AppleScript Studio(它在AppleScript编辑器中有效),但我想不出有什么理由不会。

set input to "some_input"
set vhost to "/usr/local/bin/virtualhost.sh " & input
set kill to ¬
    "terminal_pid=$(</tmp/terminal_pid); rm /tmp/terminal_pid; kill $terminal_pid"

-- launch Terminal as root, and save its process ID in /tmp/terminal_pid
tell application "Finder" to set beforeProcesses to processes
do shell script ¬
    "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal " & ¬
    "&> /dev/null & echo $! > /tmp/terminal_pid" user name "user" password ¬
    "pass" with administrator privileges

-- wait until the new Terminal is confirmed to be running
tell application "Finder"
    repeat while (processes is equal to beforeProcesses)
        do shell script "sleep 0.5"
    end repeat
end tell

-- Perform script in root Terminal window that we just opened,
-- and kill Terminal when done to prevent open root prompt
-- and multiple processes.
tell application "Terminal"
    activate
    do script vhost & "; " & kill
end tell

-- optional: wait until Terminal is gone before continuing
do shell script "while [[ ( -f /tmp/terminal_pid ) " & ¬
    "&& ( \"$(ps -p $(</tmp/terminal_pid) -o%cpu='')\" ) ]]; do sleep 0.5; done"
相关问题