Search code examples
macosapplescript

How can I use AppleScript to find disabled menu items?


I have an AppleScript script that will find all of the menu items from all of the menu bar menus. I've filtered out each separator, so I have just the commands, and now I'd like to find out how to find out which menu items are disabled. Does anybody know how I could do that?

Update:

Here's the code I have to get the names of each menu command. It's working fine, and I tried to get the menu items that were enabled in the getAppMenuItems, but it just returned all the menu items, missing values too.

set appName to "Script Editor"
set allMenus to {}
set everything to {}
set onlyEnabled to {}


tell application "System Events" to tell process appName
    set allMenus to name of every menu of menu bar 1
end tell



repeat with menuName in allMenus
    set the end of everything to strings of getAppMenuItems(appName, menuName, onlyEnabled)
end repeat



on getAppMenuItems(appProcess, appMenus, enabledItems)
    tell application "System Events" to tell process appProcess
        
        (*
        # Get all menu items
        set theItems to every menu item of menu appMenus of menu bar 1
        
        
        repeat with i from 1 to number of items in theItems
            set thisItem to item i of theItems
            
            if thisItem is not enabled then
                log ("Item:          " & name of thisItem & " is enabled")
                set the end of enabledItems to the name of thisItem
            end if
        end repeat
    *)
        
        return name of every menu item of menu appMenus of menu bar 1
    end tell
end getAppMenuItems


# From the commented part of the 'getAppMenuItems' function above. 
# When that part of the function is uncommented, I just get everything, 
#   missing values and all
onlyEnabled



# Edit Menu
item 4 of everything

(* Outputs: Half of these items are not enabled

    {"Undo", "Redo", "Cut", "Copy", "Paste", "Paste and Match Style", "Paste Reference", "Delete", "Select All", "Complete", "Find", "Spelling and Grammar", "Substitutions", "Transformations", "Speech", "Start Dictation…", "Emoji & Symbols"} *)

Solution

  • In order to get the properties of a menu item, you first need to get a reference to that menu item. You would then need to go through the various menu items and build lists or records of those that meet your criteria, so that the references can be used as needed.

    From the comments, general-purpose handlers to click at or get menu references by using a list of the menu hierarchy would serve your purpose, so those would be something like:

    on run -- examples
       #clickMenu at {"Finder", "View", 10, "Name"} -- note that there are 2 "Clean Up" items
       set menuRef to getMenuReference for {"Script Editor", "File", "New"}
       log result
       clickMenu at menuRef -- clickMenu at {"Script Editor", "File", "New"} can also be used
    end run
    
    
    to getMenuReference for menuList -- Get a reference to a menu item in an application's main menu.
       (*
          The menu hierarchy is described in the menuList parameter.
          The last menu item doesn't need to be exact, just close enough for a match.
          Menu items can also be integer indexes (separators are counted).
          When using an index, note that menus can contain dynamic or hidden items.     
             parameters: menuList [list] -
                            item 1 [text]: the application name
                            item 2 [text]: the menu bar item name
                            item(s) 3+ [text or integer]: the (sub)menu item(s)
             returns the reference or missing value
          *)
       try
          set itemCount to (count menuList) -- needs to be at least {application, menu, item}
          if itemCount < 3 then error "menuReference handler:  the menu item list contains too few items"
          set {appName, menuName} to items 1 thru 2 of menuList
          set {menuItem, found} to {last item of menuList, false}
          tell application appName to activate
          tell application "System Events" -- set up the menu path
             set appName to (name of application processes whose frontmost is true) as text -- handle different process names
             set menuPath to menu menuName of menu bar item menuName of menu bar 1 of application process appName
             if itemCount > 3 then repeat with i from 3 to (itemCount - 1) -- add sub menu items
                set menuName to item i of menuList
                if class of menuName is integer then set menuName to item menuName of (get name of menu items of menuPath)
                set menuPath to menu (menuName as text) of menu item (menuName as text) of menuPath
             end repeat
             if class of menuItem is not integer then -- the target menu item
                repeat with anItem in (get name of menu items of menuPath)
                   if anItem begins with (menuItem as text) then -- match a partial string (also 'contains', etc)
                      set {menuItem, found} to {anItem, true}
                      exit repeat
                   end if
                end repeat
                if not found then error "menuReference handler:  menu item " & quoted form of menuItem & " not found"
             end if
             return menu item menuItem of menuPath
          end tell
       on error errorMessage number errorNumber
          log errorMessage
          # error errorMessage number errorNumber -- uncomment to pass error up the chain
       end try
       return missing value
    end getMenuReference
    
    
    to clickMenu at menuItem -- Click a menu item in an application's main menu.
       (*
          The menuItem can be a reference to the menu item or a list of the menu hierarchy.
             parameters: menuItem [reference or list] - see the getMenuReference handler
             returns true if successful, false otherwise
       *)
       try
          if class of menuItem is list then -- get a reference
             set menuItem to getMenuReference for menuItem
             if menuItem is missing value then error "clickMenu handler:  the menu item was not found"
          end if
          tell application "System Events"
             # log (get properties of menuItem)
             if (enabled of menuItem) then -- filter for desired property
                click menuItem
                delay 0.25
                return true -- success
             end if
          end tell
       on error errorMessage number errorNumber
          log errorMessage
          # error errorMessage number errorNumber -- uncomment to pass error up the chain
       end try
       return false -- failure
    end clickMenu