How can I use AppleScript to find disabled menu items? - macos

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"} *)

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

Related

How can I make a menu item change its label when I press and hold the option key?

I want to be able to make a menu item a bit more powerful by having an alternate behavior when the option key is held and clicked. What I need is similar to when you press the option button in Finder, where you get the option "Copy 'seletedfile' as Pathname". How is this accomplished in AppleScript/Objective-C?
Here's my template code for creating regular menu items:
set menuItems to {"POC1", "POC2"}
repeat with i from 1 to number of items in menuItems
set this_item to item i of menuItems
if i is equal to selectedIndex then set this_item to this_item & " " & (emoji's CHECK)
set thisMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:this_item action:("someAction" & (i as text) & ":") keyEquivalent:"")
(newMenu's addItem:thisMenuItem)
(thisMenuItem's setTarget:me) -- required for enabling the menu item
end repeat

Pressing cancel in choose from list will cause a error

When I execute this script, everything works fine, but then when I press the cancel button, the script just give me a error, "Can’t get item 1 of false"
set mailList to {"Hide Applications", "Quit Applications", "Full Volume"}
set mailType to choose from list mailList
if item 1 of mailType is "Hide Applications" then
tell application "Finder"
set visible of every process whose visible is true and name is not "Finder" to false
set the collapsed of windows to true
end tell
else if item 1 of mailType is "Full Volume" then
set volume output volume 100
else if item 1 of mailType is "Quit Applications" then
tell application "System Events" to set the visible of every process to true
set white_list to {"Finder"}
try
tell application "Finder"
set process_list to the name of every process whose visible is true
end tell
repeat with i from 1 to (number of items in process_list)
set this_process to item i of the process_list
if this_process is not in white_list then
tell application this_process
quit
end tell
end if
end repeat
on error
tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try
end if
You add a check if mailType is a boolean which would happen if you press cancel. The check would be inserted between line 2 and 3 and would look something like this:
...
if class of mailType = boolean then return
...
choose from list returns boolean false when the Cancel button is pressed.
Just abort the script immediately in this case
set mailType to choose from list mailList
if mailType is false then return

How can I avoid hard coding the handlers for each menu item?

I have a stay open AppleScript app that creates a menu with many menu items. The handlers that is mapped are repetitive and only differs in its index. How can I refactor this code to dry it up?
on makeMenus(selectedIndex as integer)
newMenu's removeAllItems() -- remove existing menu items
set menuItems to {"POC1", "POC2"}
repeat with i from 1 to number of items in menuItems
set this_item to item i of menuItems
if i is equal to selectedIndex then set this_item to this_item & " " & (emoji's CHECK)
set thisMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:this_item action:("someAction" & (i as text) & ":") keyEquivalent:"")
(newMenu's addItem:thisMenuItem)
(thisMenuItem's setTarget:me)
end repeat
-- Create the Quit menu item separately
set thisMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:"Quit" action:("quitAction:") keyEquivalent:"")
(newMenu's addItem:thisMenuItem)
(thisMenuItem's setTarget:me)
end makeMenus
The repetitive handler is:
on someAction1:sender
updateStatus(1)
end someAction1:
on someAction2:sender
updateStatus(2)
end someAction2:
If I can derive the menu item name or index from a parameter in the someAction handler, that would let me have a single handler to take care of all the menu items.
The sender argument of an action handler will be the object that called the action. In this case the sender will be the particular menuItem, so a common action handler can use a property such as the sender's title.
Note that since it will be a Cocoa object, you will need to coerce the title to an AppleScript string, for example:
on doAction:sender
set menuTitle to (sender's title) as text
if menuTitle is "this" then
doThis()
else if menuTitle is "that" then
doThat()
else
doOther()
end if
end doAction:

Why is it reporting "Can't get value" when I ask for the title, not the value?

This is the same code as the application from my last few questions but this version is rewritten to run under "Script Editor" for debugging help.
The error is generated by this code's last line. Checking with Accessibility Inspector, ALL of the menu items have NIL values that is why I specifically reference the title instead.
-- `menu_click`, by Jacob Rus, September 2006
--
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item. In this case, assuming the Finder
-- is the active application, arranging the frontmost folder by date.
on menuClick(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menuClickRecurse(r, ((process appName)'s ¬
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menuClick
on menuClickRecurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menuClickRecurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menuClickRecurse
local destination, libraryName, choices
set destination to "/Users/bryandunphy/music"
set libraryName to "Testing.xml"
tell application "iTunes" to activate
menuClick({"iTunes", "File", "Library", "Export Library…"})
tell application "System Events" to set the value of the text field "Save As:" of window "iTunes" of process "iTunes" to libraryName
tell application "System Events" to tell process "iTunes" to tell its front window's group 1's pop up button 1 to click
tell application "System Events" to tell process "iTunes" to set choices to the title of every menu item of menu 1 of pop up button 1 of group 1 of its front window
repeat with ndx from 1 to count of choices
if the value of choices's item ndx is "" then
tell application "System Events" to tell process "iTunes" to select (the menu item of menu 1 of pop up button 1 of group 1 of its front window whose title is equal to item (ndx - 1) of choices)
end if
end repeat
Because the 'choices' variable contains a list of strings, not a list of menu items, so remove the value of.
You can use the index of the menu instead of the whose clause --> whose title is equal to (....).
repeat with ndx from 1 to count of choices
if choices's item ndx is "" then
tell application "System Events" to tell process "iTunes" to select (menu item (ndx - 1) of menu 1 of pop up button 1 of group 1 of its front window)
exit repeat
end if
end repeat

Why does this Applescript cause "Export Library.scpt: execution error: System Events got an error: Can’t get process "i". (-1728)"?

ƒIt's caused when run through osascript from terminal using the command line "osascript Export\ Library.scpt /Users/bryandunphy/Development/iTunesLibraryConsolidator testing.xml".
same script as my last (solved) question but posting it's entirety this time.
-- `menu_click`, by Jacob Rus, September 2006
--
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item. In this case, assuming the Finder
-- is the active application, arranging the frontmost folder by date.
on menuClick(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menuClickRecurse(r, ((process appName)'s ¬
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menuClick
on menuClickRecurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menuClickRecurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menuClickRecurse
-- Created by Bryan Dunphy during January of 2017
--
-- select the folder "directory" in Window "WinName" of Application "appName" and then clicks the default button if requested
-- if WinName is "" then it uses the frontmost window (to allow for unnamed windows)
-- REQUIRES "on handleDir" and "on findRoot" to work!
-- ONLY call switchDir
-- "createIt" is a boolean that will create any missing directories if it is set to "true".
-- "selectDefault" is a boolean indicating whether or not to click the window's default button after selecting the specified directory
-- returns "true" or "false" to indicate success.
-- clicks "Cancel" button on failure
-- Always returns "true" if "createIt" is set to "true"
on switchDir(directory, winName, appName, createIt, selectDefault)
local dirs, delim
set delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set dirs to every text item of directory
tell application "System Events" to tell process appName to set frontmost to true
my findRoot(appName, winName)
repeat with dir in dirs
if not handleDir(dir, winName, createIt) then
tell application "System Events" to tell process appName to tell button "Cancel" to click
return false
end if
end repeat
if selectDefault then keystroke return
return true
end switchDir
on handleDir(dir, winName, appName, createIt)
local foundIt
foundIt = false
local ndx
if winName is not "" then
repeat with ndx from 1 to (count of window winName's list 1)
if window winName's list 1's item ndx's value is equal to dir then
select window winName's list 1's item ndx
foundIt = true
exit repeat
end if
end repeat
else
repeat with ndx from 1 to (count of front window's list 1)
if front window's list 1's item ndx's value is equal to dir then
select front window's list 1's item ndx
foundIt = true
exit repeat
end if
end repeat
end if
if not foundIt then
if createIt then
if winName is not "" then
tell application "System Events" to tell process appName to tell window winName
tell button "New Folder"
click
repeat until window "New Folder" exists
delay 0.5
end repeat
set value of text field 1 of window "New Folder" to dir
tell button "Create" to click
return my handleDir(dir)
end tell
end tell
else
tell application "System Events" to tell process appName to tell its front window
tell button "New Folder"
click
repeat until window "New Folder" exists
delay 0.5
end repeat
set value of text field 1 of window "New Folder" to dir
tell button "Create" to click
return my handleDir(dir)
end tell
end tell
end if
end if
else
return foundIt
end if
end handleDir
on findRoot(appName, winName)
local rootName
if winName is not "" then
tell application "System Events" to tell process appName to tell window winName
tell pop up button 1
click
repeat until menu 1 exists
delay 0.5
end repeat
local ndx
repeat with ndx from 1 to (count of menu 1)
if the title of menu 1's menu item ndx is "" then
set rootName to the title of menu 1's menu item (ndx - 1)
select (menu 1's menu item (ndx - 1))
exit repeat
end if
end repeat
end tell
end tell
else
tell application "System Events" to tell process appName's front window
tell pop up button 1
click
repeat until menu 1 exists
delay 0.5
end repeat
local ndx
repeat with ndx from 1 to (count of menu 1)
if the title of menu 1's menu item ndx is "" then
set rootName to the title of menu 1's menu item (ndx - 1)
select (menu 1's menu item (ndx - 1))
exit repeat
end if
end repeat
end tell
end tell
end if
return rootName
end findRoot
on run (clp)
if clp's length is not 2 then error "Incorrect Parameters"
local destination, libraryName
set destination to clp's item 1
set libraryName to clp's item 2
menuClick("iTunes", "File", "Library", "Export Library…")
set value of parentObject's text field "Save As:" to (libraryName and ".xml")
tell pop up button 1 of group 1 of window "New iTunes Library" of process iTunes of application "System Events" to click
repeat with ndx from 1 to (count of parentObject's menu 1)
if title of menu item ndx is "" then
select menu item (ndx - 1)
exit repeat
end if
end repeat
my switchDir(destination, "iTunes", "iTunes", true, false)
set the value of text field "Save As:" of window "iTunes" to (libraryName + ".xml")
tell button "Save" of window "iTunes" to click
return (destination and "/" and libraryName and ".xml")
end run
Change:
menuClick("iTunes", "File", "Library", "Export Library…")
to:
menuClick({"iTunes", "File", "Library", "Export Library…"})
AppleScript's error reporting is truly awful (no tracebacks, for starters), and osascript is even worse than Script Editor for debugging. If you run the script in SE, it will at least highlight the line in your script where the error occurred. If that doesn't give you a clue, add log commands to report the script's progress. osascript will write logged messages to stderr. In SE, click the awful 'document' (show/hide log) icon at the bottom of the window, then select 'Messages'. If your time is worth more than $100, get yourself a copy of Script Debugger which also lets you add breakpoints and step through and inspect variables as the script runs.

Resources