AppleScript to title selected images in Photos with sequential numbers

I was writing something about batch changes to image metadata in Photos, and I discovered that Photos never brought back a feature that had been in iPhoto: sequential titling of images.

Let’s say you take 40 photos on a trip and want to title them Seattle Trip 1, Seattle Trip 2, and so on. In iPhoto, there was a built-in command for setting the title of a selected set of photos to the same text and a checkbox for adding a sequential number to it. In Photos, you can still change the title of a set of selected photos to the same text (select them, press Command-I, and change the Various Titles field), but there’s no option for adding those sequential numbers.

I wondered if Shortcuts could do it but I couldn’t see any commands for writing photo metadata. Next, I thought about AppleScript, and a few quick Web searches revealed that it should be possible. But I suck at AppleScript and other scripting languages—I can figure out the necessary logic, but the syntax and precise commands take me hours to look up and arrange correctly.

So I decided to ask ChatGPT, and since it was being slow, I fed the same prompt to Google’s Bard. Neither came up with a working script on the first few tries, but I provided the errors I got and tweaked the requests, and after four or five more back-and-forths with Bard, it came up with a simple script that worked. (I didn’t keep pushing ChatGPT after I had the script working.) I tweaked it just slightly to add an instruction dialog at the start.

My initial prompt was:

Write an AppleScript to change the titles of images selected in Apple’s Photos app to a user-specified string with a sequential number appended to the title.

And the key correction from the error message (even though I have no idea what it means) was:

The problem is setting the title to type specifier.

So here’s the script. Launch Script Editor from /Applications/Utilities, and paste the script in. Then click the Play button.

display dialog "Select the desired images in Photos, then click OK." buttons {"OK"} default button 1

tell application "Photos"
	set theSelection to selection
	set theUserInput to text returned of (display dialog "Enter the desired title: " default answer "")
	set theCounter to 1
	repeat with im in theSelection
		set name of im to theUserInput & "-" & theCounter
		set theCounter to theCounter + 1
	end repeat
end tell

If you wanted to make it snazzier, you could probably get Bard or ChatGPT to adjust the script to put in dates or pick up the title automatically from the location or something like that.


Reference Alternative Approach: An app that handles the details of ‘scripting’ filename changes when using apps other than Photos.

All of my shared images and other files involve placing shared images in a Finder Folder which may include files from other apps besides Photos. “A Better Finder Rename” renames the files to be shared with sequence numbers and does not require changing any script as macOS automation support evolves. The source files are untouched.

ABFR sequence tag control is quite powerful, including padding sequence numbers to fixed length with leading "0"s as required to avoid problems with sorting using less intelligent applications. You may also use Roman Numerals or Alphabetic sequences, but the concept of a fixed character length for Roman Numeral indexes is not applicable. Setting the position to apply a sequence, date. or other strings, is another supported user interface item.

I have been using ABFR (and ABFA) since 2007 to adjust filenames and metadata for a range of object types, not just images. It has become part of my mandatory tool kit installation just like CCC, Simple Floating Clock, and other such.

Name Mangler will do that…

I don’t think either of the suggested alternative utilities, Name Mangler or A Better Finder Rename, will do what Adam’s script does. Adam is not renaming files, he is setting Titles. Unless these utilities have hooks into Photos, they won’t be able to set Titles.

@jajvj1 is correct—there are lots of solutions for batch-renaming files in the Finder, including the Finder’s own File > Rename command. This script sets the title metadata within Photos, which is separate from the filename.