根据AppleScript,Cocoa应用程序不包含菜单栏 [英] Cocoa application contains no menu bars, according to AppleScript

查看:165
本文介绍了根据AppleScript,Cocoa应用程序不包含菜单栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个需要自己开始听写的Mac应用程序(OSX 10.8)。因为没有办法启动听写直接,最好的方法是通过菜单栏编辑/开始听写,因为用户可能有不同的键盘快捷键。



这是我的应用程序调用的简单脚本(使用 NSAppleScript 对象):

 告诉应用程序系统事件
告诉应用程序进程MyApp
告诉菜单栏1
告诉菜单栏项目编辑
告诉菜单
点击菜单项开始录音
结束告诉
结束告诉
结束告诉
结束告诉
结束告诉

这里是结果( NSLog '来自AppleScript的错误) p>

 错误:-1719系统事件出现错误:
无法获取应用程序进程MyApp的菜单栏1。索引无效。

我做了一个基本测试,看看发生了什么



我的应用程序:

 告诉应用程序系统事件
告诉应用程序MyApp
set x to menu bars
return x
end tell
end tell

result:< NSAppleEventDescriptor:[]>

Finder:

 code>告诉应用程序系统事件
告诉应用程序进程Finder
将x设置为菜单栏
返回x
结束告诉
结束告诉

result:< NSAppleEventDescriptor:['obj'{'form':'indx','want':'mbar','seld':1,'from':'obj' :'name','want':'pcap','seld':'utxt'(Finder),'from':null()}}]>

所以基本上AppleScript告诉我我的应用没有菜单栏?我运行Accessibility Inspector,确实有足够的菜单栏(加我可以看到它...)。





这里有什么问题?

$ b $我想在Emacs中激活一个服务,否则会覆盖服务的全局键盘快捷方式 - 一个不同的问题,我将在另一个线程中询问。



运行上面的示例我发现我必须将应用程序(Emacs和用于测试Automator)添加到辅助功能应用程序 - >安全和隐私 - >隐私 - >辅助功能)。有关 select-a-menu-item-in-applescript-without-using-system-events-in-10-9-maverick and also AppleScript - uscripting



通过该访问,我可以看到服务:

 告诉应用程序Emacs
activate
end tell
告诉应用程序系统事件
告诉应用程序进程Emacs
告诉菜单栏1
告诉菜单Emacs
告诉菜单项服务
告诉菜单服务
将x设置为菜单项
单击菜单项XXX
返回x
结束告诉
结束告诉
结束告诉
结束告诉
end tell
end tell

但操作未激活!



使用上面的示例返回一个错误,XXX不存在。将字符串替换为正确的字符串意味着不会发生错误,但操作不会发生。


I am creating a mac app that needs to start dictation (OSX 10.8) by itself. Because there is no way to initiate dictation "directly" the best way to do this is through the menu bar "Edit"/"Start Dictation" because a user may have different keyboard shortcuts for it.

Here's the simple script my app calls (using an NSAppleScript object):

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

Here are the results (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.

I did a basic test to see what was going on

My App:

tell application "System Events"
    tell application process "MyApp"
        set x to menu bars
        return x
    end tell
end tell

result: <NSAppleEventDescriptor: [ ]>

Finder:

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() } } ]>

So basically AppleScript is telling me my app has no menu bars? I run Accessibility Inspector and sure enough there is in fact a menu bar (plus I can see it...).

What's going wrong here?

解决方案

I was after something similar, I wanted to activate a service from within Emacs which had otherwise overridden the global keyboard shortcut to the service - A different question which I will ask in another thread.

Running your examples above I did discover that I had to have added the application (Emacs and for testing Automator) to the Accessibility applications (System Preferences -> Security & Privacy -> Privacy -> Accessibility). Further information regarding that on select-a-menu-item-in-applescript-without-using-system-events-in-10-9-maverick and also AppleScript - uiscripting.

With that access in place I am able to see the menu items under services:

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

But the action is not activated!

Using the example above an error is returned that 'XXX' does not exist. Replacing the string with the correct string means no error occurs, but the action does not take place.

这篇关于根据AppleScript,Cocoa应用程序不包含菜单栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆