无法在Automator的AppleScript中执行sudo命令,需自动化重启Logi Options+
解决Logi Options+自动重启的AppleScript问题
问题原因
你使用的with administrator privileges参数并非AppleScript中Terminal应用do script命令的合法参数,因此会触发语法错误。要实现无需手动输入密码的自动化执行,有两种可行方案:
方案一:修改sudoers文件(推荐,更安全)
通过配置sudoers让特定命令无需密码验证:
- 打开终端,执行
sudo visudo - 在文件末尾添加一行(替换成你的系统用户名):
your_username ALL=(ALL) NOPASSWD: /usr/bin/pkill -9 logioptions - 按
Ctrl+O保存,回车确认路径,再按Ctrl+X退出编辑。
修改后的AppleScript脚本:
on run {input, parameters} tell application "Terminal" activate do script "sudo pkill -9 logioptions" -- 可选:执行后自动关闭终端窗口 delay 1 do script "exit" in front window end tell -- 自动重启Logi Options+ tell application "Logi Options+" to activate return input end run
方案二:脚本内获取密码并自动传入
如果不想修改sudoers配置,可以通过弹窗获取管理员密码,再通过管道传给sudo:
on run {input, parameters} set userPassword to text returned of (display dialog "请输入管理员密码:" default answer "" with hidden answer) tell application "Terminal" activate do script "echo " & quoted form of userPassword & " | sudo -S pkill -9 logioptions" delay 1 do script "exit" in front window end tell tell application "Logi Options+" to activate return input end run
内容的提问来源于stack exchange,提问作者Phil Courtney
相关产品推荐
相关产品推荐

