How do I access a nested menu item with AppleScript in Photos.app (to open each album in turn, to change sorting)?
My end goal is to change all Photos.app albums under a certain folder to "Keep Sorted By Title" (View > Sort > Keep Sorted By Title).
It doesn't seem possible to directly set the sort order of an album via AppleScript (couldn't find it in the Dictionary, anyway).
So I figured I could have AppleScript open each album in turn, then change the sort order.
First step is to open each album in turn, which I thought I could do by clicking them in the View > Albums menu. But I haven't been able yet to figure out how to do that with AppleScript.
This gets me the "Albums" menu:
tell application "Photos" to activate
tell application "System Events" to tell process "Photos"
menu 1 of menu item "Albums" of menu "View" of menu bar item "View" of menu bar 1
end tell
Result:
menu "Albums" of menu item "Albums" of menu "View" of menu bar item "View" of menu bar 1 of application process "Photos" of application "System Events"
But I don't know how to get from there to say the "Hushings 2020" album.
For example, the result of menu items of menu 1 of menu item "Albums" of menu "View" of menu bar item "View" of menu bar 1
is {}
.
The result of menus of menu item "Albums" of menu "View" of menu bar item "View" of menu bar 1
is just that single menu: {menu "Albums" of menu item "Albums" …}
The result of entire contents of menu bar item "View" of menu bar 1
is a lot of data, but doesn't seem to contain anything from the submenu under "Albums".
How would I actually find the "Hushings 2020" album programmatically in this situation?
Alternatively, can I achieve my goal in another way? I saw someone mention elsewhere they bound a keyboard shortcut to "Keep Sorted By Title" and then manually clicked into each event and hit the shortcut. I'm open to that (it's just 600 albums or so) but I'd love to figure out how to do it programmatically via AppleScript instead – or at least to understand how to navigate these menus.
Possible AppleScript solution:
The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
- 1 Assumes necessary and appropriate setting in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Example AppleScript code:
tell application "Photos"
activate
set albumNames to name of albums
end tell
repeat with thisAlbum in albumNames
tell application "Photos" to activate
tell application "System Events"
tell application process "Photos"
tell menu bar item "View" of menu bar 1
click
click menu item "Albums" of menu 1
click menu item thisAlbum of menu 1 of menu item "Albums" of menu 1
click
click menu item "Sort" of menu 1
click menu item "Keep Sorted By Title" of menu 1 of menu item "Sort" of menu 1
end tell
end tell
end tell
delay 0.5
end repeat
Notes:
In my Photos all of my entries under My Albums are a single level of Albums, so the example AppleScript code worked for me.
If you are trying to target Albums within a folder under e.g. "Hushings 2020" then:
Change:
set albumNames to name of albums
To, e.g.,:
set albumNames to name of albums of folder "Hushings 2020"
Note that because of the nature of how menu items are displayed in this case, the album names within the folder must be unique to the target folder.
You may need to add a delay
command in-between the various click
events.
Because of the kludgy nature of UI Scripting it is prone to issues/errors, and in this particular use case, once started you cannot do anything else on the computer and must allow the script to finish.
You could also break down the Albums into multiple lists and cycle thru a smaller number of items at a time, instead of trying to do all 600 at one time.
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.
Adding a keyboard shortcut for: Keep Sorted By Title
Go to System Preferences > Keyboard > Shortcuts > App Shortcuts and press the [+] button.
For Application: select [Photos].
For Menu Title: use Keep Sorted By Title
For Keyboard Shortcut: use: ⌥⌘T
Then click the Add button.
You can then select one Album at a time and press ⌥⌘T followed by a down arrow and so on.