Question about menus and. menu items of Photos.app

Hi,
I have a piece of code that calls a procedure that traverses the View menu in Photos.app. At least that’s what I am trying to do. I can get the View Menu, click on the Collections menu item. When I go about to clicking on the Albums menu item under that, I have the following code :slight_smile:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Photos" to activate
set root_menu_item to search_from_view_menu("Collections", true)
if root_menu_item = null then
	display dialog "Collections not found"
end if
tell application "System Events" to tell process "Photos.app" to click root_menu_item
-- tell application "System Events" to tell process "Photos.app" to click menu 1 of menu_item

delay 2
set album_menu to search_menu_items("Albums", menus of root_menu_item, false)
if album_menu = null then
	display dialog "Albums not found"
end if
tell application "System Events" to tell process "Photos.app" to click album_menu
-- tell application "System Events" to tell process "Photos.app" to click menu 1 of album_menu_item

on search_from_view_menu(search_name, return_item)
	tell application "System Events" to tell application process "Photos"
		set the_menu to item 1 of UI elements of menu bar item "View" of menu bar 1
		set sub_search to my search_menu_items(search_name, the_menu, return_item)
		if sub_search ≠ null then
			return sub_search
		end if
	end tell
	return null
end search_from_view_menu

on search_menu_items(searched_name, a_menu, return_item)
	tell application "System Events"
		set menu_items to menu items of a_menu
		set child_count to count menu_items
		if child_count > 0 then
			set idx to 1
			repeat count of menu_items times
				set menu_item to item idx of menu_items
				set item_name to name of menu_item
				if searched_name is equal to item_name then
					if return_item then
						return menu_item
					else
						return menu 1 of menu_item
					end if
				else
					repeat with element in menus of menu_item
						set sub_search to my search_menu_items(searched_name, element, return_item)
						if sub_search ≠ null then
							return sub_search
						end if
					end repeat
				end if
				set idx to idx + 1
			end repeat
		end if
	end tell
	return null
end search_menu_items

My current difficulty is that when I am trying to do “menus of root_menu_item”, “menus” is considered a variable and not the highlighted to blue keyword that allows me to get the menus node under the “Albums” menu item.

If you look at the last repeat in the procedure code that does the traversal, I have a similar situation, and in that case the word “menus” is highlighted to blue and does what is expected.

Thanks,
Regards

i don’t have Photos so I can’t run your script but I would guess that the issue is that your first use is outside of any System Events tell block whereas the second use is within.

The term menus is reserved to System Events but not generally within applescript. As such, it is treated as a variable, since symantically, that is how it is used.

Have a search for Menus / Menu items

There many examples in this forum.
Many of them use repeat loops which
Waits for a sub mentions to actually appear
After clicking on them. Then traversing the menu.

UIBeowser

is also a great tool that you can you to locate your exact menu item “Albums” and get a reference to its Menu Items.

So you don’t have to do all of that traversing and checking for the Album menu item.

If you have ScriptDebugger you can open the dictionaries and launch system events and in the explorer look in the application process for“Photos” and then look thru the UIElements available. Go thru the main window and traverse thru

In addition to Mockman’s correct assertion that you’re targeting any process that understands what a ‘menu’ is as a class/object. Putting it inside a ‘tell application “System Events”’ block should fix that.

However, I question to what end?

At least OMM (macOS Tahoe 26.1, Collections doesn’t have a submenu, so there are no menus to traverse:

As such, the script is doomed to fail.

1 Like

I am on Sonoma. I ended up learning how to traverse the menus, with the help of a script I have that prints out the object hierarchy.
In my case the View menu has the Collections sub menu that has several items, one them being the Albums item, where are the albums I am trying to access.

Thanks,
Regards