How can I choose a menu item from dropdown menu using AppleScript

For Catalina I would only do something like this in Preview

click pop up button 3
click menu item "Customized Size" of menu 1 of pop up button 3

But for Big Sur this no longer works :frowning:

After adding splitter group which is now needed for Big Sur my code looks as follows:

tell sheet 1
	tell splitter group 1
		click pop up button 3
		click menu item "Customized Size" of menu 1 of pop up button 3
	end tell
	click menu button "PDF"
	repeat until exists menu 1 of menu button "PDF"
		delay 0.02
	end repeat
	click menu item "Save as PDF" of menu 1 of menu button "PDF"
end tell

But it is not able to find the menu item “Customized Size”

I can’t find Customized Size in my Preview menu, but I’ll use Close Window for the sake of example.

You can add this bit of code to make the syntax much simpler:


on menu_click(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 menu_click_recurse(r, ((process appName)'s ¬
		(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click

on menu_click_recurse(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 menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
		end if
	end tell
end menu_click_recurse

that will allow you to call menu_click with a list containing the application name, followed by the menu command.

So, to Close Window in Preview:

tell application "Preview" to activate
menu_click({"Preview", "File", "Close Window"})

All together for copying ease:


on menu_click(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 menu_click_recurse(r, ((process appName)'s ¬
		(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click

on menu_click_recurse(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 menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
		end if
	end tell
end menu_click_recurse

tell application "Preview" to activate
menu_click({"Preview", "File", "Close Window"})

I’m not on Big Sur, so I’m not sure it will, but I do hope it helps!