Script: Batch Change the Title of a Photo to the Filename Followed by the Existing Title
A frequent problem in Photos: If we want to sort the photos in an album by the filenames or just be able to see the filenames, it cannot be done in Photos 5 on Catalina. Photos 5 can sort the photos in an album by the title, but not by the filenames. So we have to copy and paste the filename into the Title field.u
I am using this script below to batch change the titles, so the existing title will be prefixed by the filename.
The script copies just the first ten characters of the filename before the existing title, so the title will not become too long. I am only interested in the image number part of the title.
Scroll down to Version 2, if you want to copy the full filename, with or without the filename extension
To run the script, copy and paste it into the script editor window.
Then select a few photos in Photos (in the All Photos View or a top level album) and click the Run button in Script Editor.
Test first with only a few photos, preferably in a different Photos Library for testing. As always make a current backup of your library before trying scripts - better safe than sorry.
-- batch append the title of images to the filename (only the first ten characters of the filename)
(* How to use this script:
Open this script in Script Editor. Launch Photos.
Select photos while viewing the "All Photos" album; this works better than Moments or smart albums
When all all photo are selected press the "Run" button in Scripteditor.
*)
set imageSel to {}
tell application "Photos"
activate
-- process the selected photos from the All Photos album
try
set imageSel to (get selection)
on error errTexttwo number errNumtwo
display dialog "Cannot get the selection: " & errNumtwo & return & errTexttwo
end try
-- check, if the album or the selected photos do contain images
if imageSel is {} then
error "Please select some images."
else
repeat with im in imageSel
set oldTitle to (the name of im) --as string
set oldTitleString to (the name of im) as string
set hasTitle to (oldTitle is not equal to missing value) and (the length of oldTitleString > 0)
if not hasTitle then
set oldTitle to ""
end if
set newtitle to characters 1 thru 10 of (the filename of im as string) as string
if hasTitle then
set newtitle to newtitle & " - " & oldTitleString
end if
-- return newtitle
try
tell im
set its name to newtitle
end tell
on error errText number errNum
display dialog "Error: " & errNum & return & errText & "Trying again"
try
delay 2
tell im
set its name to newtitle
end tell
on error errTexttwo number errNumtwo
display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo
end try
end try
end repeat
end if
end tell
-- display dialog "Done"
return "Done"
The length of the prefix with the filename will depend on this line:
set newtitle to characters 1 thru 10 of (the filename of im as string) as string
If you set it to "characters 1 trough 8 ..." instead of 10, you will get a shorter prefix.
The screenshot below is showing the result for just an eight character prefix:
------------------------------------------------------------------------------------------------------------------------------------------------
------------- Version 2 : This version will copy the full filename before the existing Title.
------------- You can select, if you want to keep the extension. By default, the filename extension will be dropped
------------------------------------------------------------------------------------------------------------------------------------------------
(*
Photos Title to Filename without Extension plus Title.scpt - special version for OT
This script will replace the titles of the selected photos by the filenames (without the extension), followed by the original title, if an older title exists
How to use this script:
- Open this script in Script Editor. Launch Photos.
- Select photos while viewing the "All Photos" album or a top level album, not an album inside a folder; this works better than Moments or smart albums
When all photo are selected press the "Run" button in Scripteditor.
*)
set imageSel to {}
-- set SkipExtension to false
set SkipExtension to true -- change this to false, if you want to keep the extension
tell application "Photos"
activate
-- process the selected photos from the All Photos album
try
set imageSel to (get selection)
on error errTexttwo number errNumtwo
display dialog "Cannot get the selection: " & errNumtwo & return & errTexttwo
end try
-- check, if the album or the selected photos do contain images
if imageSel is {} then
error "Please select some images."
else
repeat with im in imageSel
-- get the old title, if it exists
set oldTitle to (the name of im) --as string
set hasTitle to (oldTitle is not equal to missing value) and (the length of oldTitle > 0)
if hasTitle then
set oldTitle to " - " & oldTitle -- add a separator " - "
else
set oldTitle to ""
end if
-- this gets the name of the filename for the image im
set myfilename to filename of im as string
set myLength to (length of myfilename)
if (SkipExtension) then
-- (from https://discussions.apple.com/thread/7159846)
set tmpFileName to (the reverse of every character of myfilename) as string
-- this flips it around in a temp variable
set myLength to myLength - the (offset of "." in tmpFileName)
-- this subtracts the count of the extention
end if
set myfilename to characters 1 thru myLength of myfilename as text
-- This drops the extension (.jpg, .jpeg, .gif...etc)
set newtitle to myfilename & oldTitle
try
tell im
set its name to newtitle -- write the new tite to the image
end tell
on error errText number errNum
display dialog "Error: " & errNum & return & errText & "Trying again"
try
delay 2
tell im
set its name to newtitle
end tell
on error errTexttwo number errNumtwo
display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo
end try
end try
end repeat
end if
end tell
-- display dialog "Done"
return "Done"
The script has been copied from this discussion: https://discussions.apple.com/thread/251386600?answerId=252700155022#252700155022