我正在创建一个 Mac 应用程序,需要自行启动听写 (OSX 10.8)。因为无法“直接”启动听写,所以最好的方法是通过菜单栏“编辑”/“开始听写”,因为用户可能有不同的键盘快捷键。
这是我的应用程序调用的简单脚本(使用 NSAppleScript
对象):
tell application "System Events"
tell application process "MyApp"
tell menu bar 1
tell menu bar item "Edit"
tell menu "Edit"
click menu item "Start Dictation"
end tell
end tell
end tell
end tell
end tell
这里是结果(NSLog
来自 AppleScript 的错误)
Error:-1719 System Events got an error:
Can’t get menu bar 1 of application process "MyApp". Invalid index.
我做了一个基本测试来看看发生了什么
我的应用程序:
tell application "System Events"
tell application process "MyApp"
set x to menu bars
return x
end tell
end tell
result: <NSAppleEventDescriptor: [ ]>
查找器:
tell application "System Events"
tell application process "Finder"
set x to menu bars
return x
end tell
end tell
result: <NSAppleEventDescriptor: [ 'obj '{ 'form':'indx', 'want':'mbar', 'seld':1, 'from':'obj '{ 'form':'name', 'want':'pcap', 'seld':'utxt'("Finder"), 'from':null() } } ]>
所以 AppleScript 基本上是告诉我我的应用程序没有菜单栏?我运行 Accessibility Inspector,果然有一个菜单栏(而且我可以看到它......)。
这里出了什么问题?
最佳答案
我正在寻找类似的东西,我想从 Emacs 中激活一个服务,否则该服务会覆盖该服务的全局键盘快捷键 - 我将在另一个线程中提出一个不同的问题。
运行上面的示例,我确实发现我必须将应用程序(Emacs 和用于测试 Automator)添加到辅助功能应用程序(系统偏好设置 -> 安全和隐私 -> 隐私 -> 可访问性)。有关此的更多信息,请访问 select-a-menu-item-in-applescript-without-using-system-events-in-10-9-maverick还有AppleScript - uiscripting .
有了该访问权限,我就可以看到服务下的菜单项:
tell application "Emacs"
activate
end tell
tell application "System Events"
tell application process "Emacs"
tell menu bar 1
tell menu "Emacs"
tell menu item "Services"
tell menu "Services"
set x to menu items
click menu item "XXX"
return x
end tell
end tell
end tell
end tell
end tell
end tell
但是该操作未激活!
使用上面的示例会返回“XXX”不存在的错误。用正确的字符串替换该字符串意味着不会发生错误,但操作不会发生。
关于objective-c - 根据 AppleScript,Cocoa 应用程序不包含菜单栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17007846/