Adding parenthetical asides to photograph titles on macOS
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
-
if the name of selectedPhoto does not contain baseParenthetical then
- 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.
- 42 Astoundingly Useful Scripts and Automations for the Macintosh
- MacOS uses Perl, Python, AppleScript, and Automator and you can write scripts in all of these. Build a talking alarm. Roll dice. Preflight your social media comments. Play music and create ASCII art. Get your retro on and bring your Macintosh into the world of tomorrow with 42 Astoundingly Useful Scripts and Automations for the Macintosh!
More AppleScript
- Find all parent mailboxes in macOS Mail
- The macOS Mail app seems to want to hide the existence of mailboxes and any sense of hierarchical storage. These two AppleScripts will help you find the full path to a selected message and open the message’s mailbox.
- JXA and AppleScript compared via HyperCard
- How does JXA compare to the AppleScript solution for screenshotting every card in HyperCardPreview?
- Using version control with AppleScripts
- AppleScripts aren’t stored as text, which makes it impossible to track changes in AppleScript files using version control software such as Mercurial or Git.
- Save clipboard text to the current folder
- Use the Finder toolbar to save text on the current clipboard directly to a file in the folder that Finder window is displaying.
- AppleScript, variables, and dropped filenames in Automator
- Automator is a simple workflow system for Mac OS X. By its nature it is very procedural: one task follows another; workflows don’t loop and they don’t store variables for later. However, this is possible in Automator and while it adds complexities it can also solve problems such as wanting to save dropped filenames for later use.
- 17 more pages with the topic AppleScript, and other related pages
More macOS
- Catalina vs. Mojave for Scripters
- More detail about the issues I ran into updating the scripts from 42 Astounding Scripts for Catalina.