简体   繁体   English

根据AppleScript,可可应用程序不包含任何菜单栏

[英]Cocoa application contains no menu bars, according to AppleScript

I am creating a mac app that needs to start dictation (OSX 10.8) by itself. 我正在创建一个需要单独启动听写功能(OSX 10.8)的mac应用。 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): 这是我的应用程序调用的简单脚本(使用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

Here are the results ( NSLog 'd the error from the AppleScript) 这是结果( NSLog是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? 所以基本上AppleScript告诉我我的应用程序没有菜单栏? I run Accessibility Inspector and sure enough there is in fact a menu bar (plus I can see it...). 我运行Accessibility Inspector并确定实际上有一个菜单栏(另外我可以看到它...)。

证明实际上有一个菜单!

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. 我在做类似的事情后,想从Emacs内部激活一个服务,否则该服务将覆盖该服务的全局键盘快捷键-我将在另一个线程中提出另一个问题。

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). 在上面的示例中,我确实发现我必须将应用程序(Emacs和用于测试Automator)添加到可访问性应用程序(系统偏好设置->安全和隐私->隐私->可访问性)中。 Further information regarding that on select-a-menu-item-in-applescript-without-using-system-events-in-10-9-maverick and also AppleScript - uiscripting . 有关在没有使用系统事件的情况下在AppleScript中选择一个菜单项的情况,以及在10月9日特立独行的情况,以及有关AppleScript脚本的更多信息

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. 使用上面的示例,返回错误消息“ XXX”不存在。 Replacing the string with the correct string means no error occurs, but the action does not take place. 用正确的字符串替换该字符串意味着没有错误发生,但是该操作不会发生。

Have you tried activating your app with a short delay before running the system events stuff? 在运行系统事件之前,您是否尝试过短暂延迟激活应用程序? You're also missing some other stuff. 您还缺少其他一些东西。 Here's how you would do it in Safari... 这是您在Safari中的操作方式...

tell application "Safari" to activate
delay 1

tell application "System Events"
    tell process "Safari"
        tell menu bar item "Edit" of menu bar 1
            click
            delay 0.5
            tell menu item "Speech" of menu 1
                click
                delay 0.5
                click menu item "Start Speaking" of menu 1
            end tell
        end tell

    end tell
end tell

A second point. 第二点。 If you are creating the application yourself then you do not need to do as you are suggesting. 如果您自己创建应用程序,则无需按照建议进行操作。 You state "Because there is no way to initiate dictation directly" but why do you say that? 您说“因为无法直接发起听写”,但为什么这么说呢? Every menu item is hooked up to a command, so in essence you can hook into that command if this is your own application. 每个菜单项都与命令关联,因此从本质上讲,如果这是您自己的应用程序,则可以与该命令关联。 Just create a button or something in your interface and connect it to the same command that the menu item is connected to. 只需在界面中创建一个按钮或其他东西,然后将其连接到菜单项所连接的同一命令即可。 For example I can see in Xcode that the "Start Speaking" menu item is connected to First Responder's startSpeaking command. 例如,我可以在Xcode中看到“开始讲话”菜单项已连接到“第一响应者”的startSpeaking命令。 As such you can create a button or some other item and connect it to startSpeaking of First Responder in Interface builder yourself. 这样,您可以自己创建一个按钮或其他项目,并将其连接到Interface builder中的startSpeaking of First Responder。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM