我尝试使用以下命令从控制台调用简单的消息框:
osascript -e "display dialog \"hello\""
Run Code Online (Sandbox Code Playgroud)
但它返回:
execution error: No user interaction allowed. (-1713)
Run Code Online (Sandbox Code Playgroud)
有解决方法吗?
编辑:
解决方法是: tell application "AppleScript Runner" to display dialog "Hello"
您可以告诉像SystemUIServer这样的后台进程来显示对话框.默认情况下关闭对话框后,先前聚焦的窗口无法获得焦点.系统事件和AppleScript Runner如果之前没有运行,可能会有很小的延迟.
answer=$(osascript -e 'try
tell application "SystemUIServer"
set answer to text returned of (display dialog "" default answer "")
end
activate app (path to frontmost application as text)
answer
end' | tr '\r' '\n')
[[ -z "$answer" ]] && exit
Run Code Online (Sandbox Code Playgroud)
您还可以告诉最前面的应用程序显示一个对话框,但它通常会稍微慢一些.如果应用程序没有响应,则不会立即显示该对话框.如果MPlayer OS X位于最前端,则文本对话框不接受任何键盘输入.
answer=$(osascript -e 'try
tell application (path to frontmost application as text)
text returned of display dialog "" default answer ""
end
end' | tr '\r' '\n')
[[ -z "$answer" ]] && exit
Run Code Online (Sandbox Code Playgroud)