Assign a photo’s Title field based on its filename in Photos
My migration from iPhoto to Photos continues, and today's snafu was my discovery that I couldn't rename files in Photos. This is something I've long done in iPhoto—not for every file, but for ones I'd like to group together using something other than Keywords.
For example, I have a collection of iOS wallpapers, for both the home and lock screens. I name each with either "H_" or "L_" as the start of the filename, which let me create this Smart Album to see them all together:
The inability to rename files isn't critical, in particular because the Title field can be used for the same functionality. But I had a problem: When I migrated, Photos created Titles for some, but not all, of my custom-named images. In particular, it missed all of the iOS wallpapers. I'm not sure if this is because these aren't photos in the traditional sense—they're edited photos I export as PNGs. But whatever the reason, I had hundreds of images that needed a Title that was equal to their Filename.
AppleScript to the rescue…this simple bit of code acts on the selection in Photos, and sets the Title equal to each image's filename.
1 2 3 4 5 6 7 8 9 10 11 12 13 | tell application "Photos" activate set mySelection to (get selection) if mySelection is {} then error "Select photos before using script." else repeat with thePhoto in mySelection tell thePhoto set the name to the filename end tell end repeat end if end tell |
To use this bit of code, just select the photos you'd like to modify in Photos, then run the script. You can make it a bit easier by saving the script into your user's Library > Scripts > Applications > Photos folder (create as many of those as don't exist). It will then be available in the AppleScript menu in the menu bar, assuming you've enabled that in the AppleScript Editor's preferences.
This saved me literally hours of work, copying and pasting filenames to the Title field. (I was surprised this worked, but it did—you can't change the filename, but you can select and copy it.)
April 28 2019 Update:
In the comments, Daryle W. asked about using this script when there wasn't a modified filename—he'd like to use the filename, but drop the extension. I did a quick bit of searching, and came up with the following, which seemed to work in my limited testing. Please have a good backup before trying this, as it's not nearly as well-tested as my original version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | tell application "Photos" activate set mySelection to (get selection) if mySelection is {} then error "Select photos before using script." else repeat with thePhoto in mySelection tell thePhoto set OriginalName to the filename set cutPosition to (offset of "." in OriginalName) - 1 set ModifiedName to text 1 thru cutPosition of OriginalName set the name to ModifiedName end tell end repeat end if end tell |
This should work for any filename extension; I tested with JPG and HEIC and it worked fine. (I originally tried this without using a new variable, but it didn't work—I wasn't able to use the filename in the set cutPosition... line, for example.)