osascript“显示对话框”有时有效,有时会出现“不允许用户交互”的错误
我的 OSX 10.7 (Lion) macbook pro 上运行着一个小型 shell 脚本,它使用 osascript 收集用户的输入。该脚本有时工作正常,有时返回执行错误:系统事件发生错误:不允许用户交互。 (-1713)
。
重新启动我的 Mac 后,它是否有效是稳定的 - 如果有效,它将一直工作直到我重新启动;如果它不起作用,那么直到我重新启动后它才会起作用;当我重新启动时,我无法预测它是否会工作,直到我尝试它,即使运行这是我重新启动后做的第一件事。
脚本的相关部分可以在 Terminal.app 中进行模拟(复制所有内容并将其粘贴到 Terminal.app 中):
echo $(/usr/bin/osascript <<-'__HEREDOC__'
tell application "System Events"
activate
set input to display dialog "What you type here should be returned:" default answer "" buttons {"Cancel", "OK"} default button 2
return text returned of input as string
end tell
__HEREDOC__
)
如果该代码向您显示一个对话框,那么它就适合您。如果它显示“无用户交互...”错误,则它不适合您。如果您重新启动,它可能会从工作状态切换到不工作状态或返回。
谷歌返回了几千个“osascript“不允许用户交互””的结果,但我找不到其他人遭受我得到的不一致结果的困扰。一些建议一种解决方法,涉及保存脚本文件并从 AppleScript 执行它
on run {sourcefile}
tell application “AppleScript Runner”
do script sourcefile
end tell
end run
......我没有尝试过这个,因为它看起来非常丑陋。有时什么可能导致此功能不起作用?您将如何解决此类问题?
I have a small shell script running on my OSX 10.7 (Lion) macbook pro that uses osascript
to gather input from the user. This script sometimes works perfectly, and sometimes returns execution error: System Events got an error: No user interaction allowed. (-1713)
.
Whether it works or not is stable after rebooting my mac - if it works, it'll work until I reboot; if it doesn't work, it won't work until I reboot; when I reboot I can't predict whether it'll work or not until I try it, even if running this is the first thing I do after a reboot.
The relevant part of the script can be simulated in Terminal.app with (copy all of this and paste it into Terminal.app):
echo $(/usr/bin/osascript <<-'__HEREDOC__'
tell application "System Events"
activate
set input to display dialog "What you type here should be returned:" default answer "" buttons {"Cancel", "OK"} default button 2
return text returned of input as string
end tell
__HEREDOC__
)
If that code shows you a dialog it's working for you. If it shows a "No user interaction…" error it's not working for you. If you reboot it may swap from working to not working or back.
Google returns a couple thousand results for 'osascript "No user interaction allowed"', but I can't find others suffering the inconsistent results I'm getting. Some suggest a workaround involving saving a script file and executing it from AppleScript with
on run {sourcefile}
tell application “AppleScript Runner”
do script sourcefile
end tell
end run
… I've not tried this because it seems so very ugly. What could be causing this sometimes not to work? How would you go about troubleshooting a problem like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论