Vin*_*ein 4 bash shell applescript
我正在尝试编写一个automator服务来启动终端中的virtualhost.sh.
使用"服务"上下文菜单,对话框将打开以询问虚拟主机的名称,然后运行applescript以启动终端并传入输入文本.
我想要的是将我的用户名和密码传递给管理员权限,这样我就不需要在终端中使用sudo传递它.
这可以用do shell script但是执行a bin/sh和virtualhost.shbash脚本来完成,所以我得到了错误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
Run Code Online (Sandbox Code Playgroud)
这会产生前面提到的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
Run Code Online (Sandbox Code Playgroud)
这会产生一个转义错误:预期的行尾等,但找到了属性.
有没有办法正确地做到这一点?
不熟悉AppleScript Studio,但如果您提供virtualhost.sh的完整路径,则可以使用普通的旧AppleScript(看起来具有相同的问题).(另外,"do shell script"不需要终端.)示例:
set vhost to "/usr/local/bin/virtualhost.sh " & input
do shell script vhost user name "user" password "pass" ¬
with administrator privileges
Run Code Online (Sandbox Code Playgroud)
您还可以扩展$ 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
Run Code Online (Sandbox Code Playgroud)
如果需要相对路径,可以将virtualhost.sh放在脚本应用程序或包中(例如在Contents/Resources中),或者在终端中,或者通过按住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
Run Code Online (Sandbox Code Playgroud)