1

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.

screenshot

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.

1 Answer 1

2

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.

enter image description here

You can then select one Album at a time and press ⌥⌘T followed by a down arrow and so on.

4
  • Wow, this is amazingly thorough – thanks so much! So it seems like the piece of the puzzle I was missing is that one explicitly needs to "click" a menu item for the next level to be scriptable.
    – Henrik N
    Commented Jun 25, 2021 at 20:42
  • 1
    @Henrik N, RE: "So it seems like the piece of the puzzle I was missing is that one explicitly needs to "click" a menu item for the next level to be scriptable." -- Yes and no, it depends on the menu layout and in this case it's a yes because of the way Photos renders the menu in this particular area of its menu system. Commented Jun 25, 2021 at 21:45
  • Running this now – one small gotcha to be aware of: The screen saver may start while this script is running, and will abort the run. I used a free app (apps.apple.com/app/theine/id955848755) to keep the Mac awake. Turning off the screen saver presumably also works.
    – Henrik N
    Commented Jun 25, 2021 at 22:30
  • @Henrik N, RE: "one small gotcha to be aware of: The screen saver may start while this script is running, and will abort the run." -- I never use screen savers and when I'm going to do something I don't want interrupted by power management, I set it so the system stays awake. Thanks for the link. I'll check out the app. Commented Jun 25, 2021 at 22:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .