我正在创建一个需要自行启动听写(OSX 10.8)的 Mac 应用程序。因为没有办法“直接”启动听写,所以最好的方法是通过菜单栏“编辑”/“开始听写”,因为用户可能有不同的键盘快捷键。
这是我的应用程序调用的简单脚本(使用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'd the error from the 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,果然有一个菜单栏(而且我可以看到它......)。

这里出了什么问题?