JavaEar 专注于收集分享传播有价值的技术资料

Access context menu items by name using AppleScript

I'm trying to use AppleScript to click on context menu items in Logic Pro, preferably by simply providing the name of the menu item. It seems like this should be possible because I'm able to set up keyboard shortcuts for these context menu items using system preferences and providing the name of the command.

For instance, if you right click on the main editing window in Logic, a menu pops up with an option called "Add Audio File..." If I create a system preferences keyboard shortcut for Logic and give it this menu item name, it's able to execute just fine. I'd like to recreate this with a script. I'm familiar with accessing normal menu items using the hierarchy like so:

        tell process "Logic Pro"
            tell menu bar 1
                tell menu bar item "File"
                    tell menu "File"
                        click menu item "Save"

but as far as I know, there's no way to access the context menu (right click menu) that I want like this. It seems there should be a way to simply access a non-menu-bar menu item by name since system preferences is obviously able to do so.

2个回答

    最佳答案
  1. Logic pro is not scriptable so my suggestion would be that you set a keyboard shortcut in the system preferences then use system events to use said shortcuts.

    for example to enter find mode (assuming there is a find mode since i don't own Logic Pro)

    tell application "Logic Pro" to activate
    
    tell application "System Events"
        tell application process "Logic Pro"
            keystroke "f" using command down
        end tell
    end tell
    
  2. 参考答案2
  3. I don’t think you need to use the context menus. “Add Audio File…” is available in other parts of the Logic Pro X user interface. If you open the Project Audio window, there is an “Audio File” menu button with an “Add Audio File…” button in it. So this AppleScript will activate the “Add Audio File…” command:

    tell application "System Events"
        tell application "Logic Pro X" to activate
        tell process "Logic Pro X"
            tell menu bar 1
                click menu item "Open Project Audio" of menu "Window"
            end tell
            delay 1
            tell window 1
                click menu button "Audio File" of group 1 of group 1
                click menu item "Add Audio File..." of menu 1 of menu button "Audio File" of group 1 of group 1
            end tell
        end tell
    end tell
    

    One thing to keep in mind if distributing a GUI script is that the above script will only work in Logic Pro X running on a Mac set to US English (and maybe other kinds of English) because the names of the menus change if the system is set to another language. What you can do is replace the names in the above script with numbers, which is a totally experimental process, as far as I know. You have to try different numbers and see which continues to work.

    So you may be able to replace:

    menu button "Audio File" of group 1 of group 1
    

    … in the above script with:

    menu button 1 of group 1 of group 1
    

    … and get the same functionality, and the script would work on any Mac. Or you may need to use “menu button 2.” Same goes for the other named items in the above script.

    Also keep in mind that the user you distribute this script to has to give System Events permission to control their Mac in the Security pane of System Preferences or this script won’t work. That can be a giant obstacle to distributing GUI scripts. And if you save your script as an Application, you have to digitally sign it or it won’t run on other people’s computers, and that can be complicated.