如果足够短,您可以改用 Run AppleScript,然后使用:
do shell script "command" with administrator privileges
这将在 GUI 中提示用户输入管理员用户名和密码,然后以 root 身份运行“command”。
根据以下评论更新:
我错过了有关复制到特权文件夹的部分。您不能使用 Automator 的“运行 Shell 脚本”,因为它不支持以管理员身份运行命令(它不是交互式的,因此 sudo 不起作用)。所以有几种不同的方法可以解决这个问题。
要以管理员身份运行您的 Bash 脚本,请将您的 Bash 脚本保存为您在终端中运行的典型脚本。在 Automator 操作选择文件后,使用“运行 AppleScript”操作。然后输入下面的代码。您不会看到任何提示,也不会看到终端窗口;它只会执行。 (如果您确实需要提示,可以通过省略user name "username" password "password" 在 GUI 中获得提示。)用户选择的文件夹将作为 Bash 脚本的参数。
on run {input, parameters}
do shell script ("sudo /path/to/yourscript '" & POSIX path of input & "') ¬
with administrator privileges user name "username" password "password"
end run
另一种方法:将打开一个终端窗口,提示“密码:”,用户将输入他们的密码(可能是管理员)。然后您的脚本将以管理员身份运行并能够复制到特权文件夹中。
on run {input, parameters}
tell application "Terminal"
do script "sudo /path/to/yourscript '" & POSIX path of input & "'"
end tell
end run
或者,如果你真的想把你的 Bash 脚本放在 Automator 中的“运行 Shell 脚本”下,就像你目前所做的那样,并且复制指令在最后,你可以取出复制指令,然后让它回显用户选择作为其最终指令的文件夹。这会将其返回到下一个 Automator 操作。在 Run Shell Script 之后,使用 do shell script 命令输入运行 AppleScript,如上面的第一个示例所示,其中包含 cp 命令代替 /path/to/yourscript,具有管理员权限。