osascript“显示对话框”有时会工作,有时会出现错误,“不允许用户交互”

时间:2011-12-07 02:59:46

标签: osx-lion osascript

我在我的OSX 10.7(Lion)macbook pro上运行了一个小的shell脚本,它使用osascript来收集用户的输入。此脚本有时可以正常运行,有时会返回execution error: System Events got an error: No user interaction allowed. (-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__
)

如果该代码显示对话框,那么它就适合您。如果它显示&#34;没有用户交互......&#34;错误它不适合你。如果你重新启动它可能会从工作交换到不工作或返回。

Google会为&#39; osascript&#34;不允许任何用户互动&#34;&#39;返回几千个结果,但我无法找到其他人遇到的不一致的结果我得到了。一些suggest解决方法涉及保存脚本文件并使用

从AppleScript执行它
on run {sourcefile}
tell application “AppleScript Runner”
  do script sourcefile
end tell
end run

......我没试过这个,因为它看起来非常难看。什么可能导致这有时不起作用?您将如何解决此类问题?

0 个答案:

没有答案