Search code examples
pythonautomationapplescript

Read options of a menu bar for macOS application


Is there a way to read in the options of a menu bar using Python and Applescript?

I'm trying to write a script where the user doesn't know what options exist in the menu bar so the script reads them from the application and displays them.

thanks in advance!


Solution

  • Knowing the name of application process, you can read menu bar options with simple apple-script like this:

    set processName to "Safari"
    
    tell application "System Events" to tell process processName
        tell menu bar 1 to set menuBarItemNames to name of menu bar items
    end tell
    
    choose from list menuBarItemNames
    

    NOTE: the application should be running. Or, activate it in the beginning of the script:

    tell application "Safari" to launch
    
    tell application "System Events" to tell process "Safari"
        repeat until menu bar 1 exists
            delay 0.1
        end repeat
        tell menu bar 1 to set menuBarItemNames to name of menu bar items
    end tell
    
    choose from list menuBarItemNames