Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, Swift, BASIC, and whatever else I happen to feel like hacking at.

Adding parenthetical asides to photograph titles on macOS

Jerry Stratton, February 12, 2020

Mustang sales photos: Photos of my 1984 Mustang convertible prepping for sale.; Ford Mustang; Photos

Sadly, the only photos I have of my old Mustang are of getting ready to sell it. Which doesn’t merit a “Mustang” keyword.

In 42 Astoundingly Useful Scripts and Automations for the Macintosh I have a script for changing the titles of all selected photographs to a sequential title. That is, “Yosemite — 1”, “Yosemite — 2”, “Yosemite — 3” and so on. I use this regularly after coming home from a vacation. But recently I’ve had a need for leaving the title as is but adding a parenthetical. So that if I have a collection of photos that I didn’t take, I might want to add “ (photo by dad)” to the end of each, changing, for example, “Flag in the Snow” to “Flag in the Snow (photo by dad)”.

This is a much simpler task. It doesn’t require sorting the photos, because it isn’t adding a sequential number to the title. Nor does it require appending a different text (the sequence number) to each photo. The same text is appended to all photos.

Except that it makes no sense to change, for example, “Jerry at Adams Avenue Street Fair 2003” to “Jerry at Adams Avenue Street Fair 2003 (Adams Avenue Street Fair)”. So the script does check to make sure that the text being added does not already exist in the title.

[toggle code]

  • -- add a parenthetical to selected photo titles
  • -- Jerry Stratton astoundingscripts.com
  • tell application "Photos"
    • set selectedPhotos to selection
    • set photoCount to count of selectedPhotos
    • --change this number if you regularly change more than 50 photos at a time
    • if photoCount > 50 then
      • display alert "There are " & photoCount & " photos selected." buttons {"Change", "Cancel"} cancel button "Cancel"
    • end if
    • set baseParenthetical to the text returned of (display dialog "Text for parenthetical" default answer "")
    • set parenthetical to " (" & baseParenthetical & ")"
    • repeat with selectedPhoto in selectedPhotos
      • if the name of selectedPhoto does not contain baseParenthetical then
        • set the name of selectedPhoto to name of selectedPhoto & parenthetical
      • end if
    • end repeat
    • say "Finished!"
  • end tell

Save this script as something like “Add parenthetical to title” in the Photos Scripts Folder. If you don’t see a curlicue scripts folder icon in the upper right of your menu bar when in Photos, enable it inside of Script Editor by going to Preferences, General, and check “Show Script menu in menu bar”.

I created this script because I’ve decided to reduce the number of keywords I’m tagging my photos with. The problem with lots of keywords is that I end up forgetting to add them, because they’re lost in the wall of text on the keyword manager. This makes them far less useful. Some of the keywords never made sense to begin with. In 2002, for example, I visited Hearst Castle, and for some reason I gave that a keyword. I had only five photos tagged as “Hearst Castle”, for the obvious reason that it was a one-time visit. They were all in one event to begin with. One of them already had “Hearst Castle” in the title and the other four should have.

I have a lot of keywords like that, and I’m going to cull them out. This script will make it easier for me to do so.

When entering the text for the parenthetical, leave the parentheses out. Just enter the text; this allows the script to check whether or not the title already contains that text.

As in the batch title change in 42 Astounding Scripts, this script pops up an alert if more than fifty photos are selected. This is to reduce the chance of accidentally changing too many titles. In this case, however, it would be possible to revert the change if you accidentally do so. For extra practice, you might consider writing a script to remove parentheticals from the titles of selected photographs. Hint: Look at using “ends with” instead of “contains” to see if the title has a (or the) parenthetical.

  1. <- Merry Christmas!
  2. Tandy CoCo Tools ->