Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Question about using Shortcuts (or Automator) app with Photos app [cross posting]

[I originally posted an earlier version of this in the MacOS Monterey group & they suggested I post it here.]


Goal: I would like to create a script that can look at a photo in the Photos app, determine if the "Title" field is empty, and if so, copy the "Filename" field into the "Title" field. (Ideally it would be able to do this with a batch of photos without Titles, or recursively.)


I was pointed to the following AppleScript which I managed to plug into Automator, and it seemed to work but with an odd (& unsatisfactory) result (described below):


on run {input, parameters}

tell application "Photos"

activate

set imageSel to (get selection)

if imageSel is {} then

error "Please select an image."

else

repeat with im in imageSel

set title to the name of im

if not (exists (title)) then

set the name of im to the filename of im

end if

end repeat

end if

end tell

end run


So I managed to run this script in Automator [as a "Workflow" because I couldn't find the option to run it as a "Service" (I'm using MacOS 12.1)] but had a weird result. It appears to work OK, except that after it runs, in the Library window the title still appears gray, like nothing changed. The "Get Info" window shows something in the Title field (which wasn't there before running the script). BUT, when I click on the title area under the photo, it goes blank (as it does on a photo that hasn't been renamed) and then the next time I click on the photo's thumbnail, the Get Info window has lost its Title and once again says (in italics) "Add a Title." I exited Photos and relaunched it, and this weird behavior continued.


So the script does appear to change the title (it looks correct in the Get Info window at first) but it's very "fragile," disappearing as soon as I click in the title field under the thumbnail. Bizarre!


Ideas?


MacBook Air (2020 or later)

Posted on Feb 8, 2022 7:50 PM

Reply
Question marked as Best reply

Posted on Feb 10, 2022 7:37 PM

Edit: I marked this "Solved" a bit too soon, and it doesn't seem to let me delete or "Unsolve" it so I'll just put this here. The solution below works much better than what I'd tried earlier (above) but it seems to change the Title to the Filename EVEN IF THERE WAS ALREADY A TITLE THAT I WANTED TO PRESERVE. So it seems the error-checking for an existing title doesn't work quite right. Back to the drawing board, but at least I'm a big step closer.


Well, I found a solution. Someone else (Ken) had a script for changing the title to the filename AND removing the extension, and it works slightly differently, and so far this script (which I'll paste below) seems to create a new title that "sticks" (though I've only tested with a very few photos so far).


on run {input, parameters}


tell application "Photos"


activate


set imageSel to (get selection)


if imageSel is {} then -- forgetting to select image(s)


beep -- sometimes a noise before you walk away is a way to get your attention


tell application "Finder" -- got to go here to display a dialog


activate


display dialog "Please Select Images in Photos to Rename as the File Name without the Extension" -- this string can be changed to whatever you need to say


end tell


else


repeat with im in imageSel


set myFileName to filename of im as string -- this gets the name of the filename for the image


set tmpFileName to (the reverse of every character of myFileName) as string -- this flips it around in a temp variable


set myLength to (length of myFileName) - the (offset of "." in tmpFileName) -- this counts letters in the filename and then subtracts the count of the extention


set the name of im to characters 1 thru myLength of myFileName as text -- This sets the name in Photos to the file name minus the extention (.jpg, .jpeg, .gif...etc)


end repeat -- This is so that this script does all the Photos you selected.


end if


end tell


end run

Similar questions

4 replies
Question marked as Best reply

Feb 10, 2022 7:37 PM in response to smithchuck

Edit: I marked this "Solved" a bit too soon, and it doesn't seem to let me delete or "Unsolve" it so I'll just put this here. The solution below works much better than what I'd tried earlier (above) but it seems to change the Title to the Filename EVEN IF THERE WAS ALREADY A TITLE THAT I WANTED TO PRESERVE. So it seems the error-checking for an existing title doesn't work quite right. Back to the drawing board, but at least I'm a big step closer.


Well, I found a solution. Someone else (Ken) had a script for changing the title to the filename AND removing the extension, and it works slightly differently, and so far this script (which I'll paste below) seems to create a new title that "sticks" (though I've only tested with a very few photos so far).


on run {input, parameters}


tell application "Photos"


activate


set imageSel to (get selection)


if imageSel is {} then -- forgetting to select image(s)


beep -- sometimes a noise before you walk away is a way to get your attention


tell application "Finder" -- got to go here to display a dialog


activate


display dialog "Please Select Images in Photos to Rename as the File Name without the Extension" -- this string can be changed to whatever you need to say


end tell


else


repeat with im in imageSel


set myFileName to filename of im as string -- this gets the name of the filename for the image


set tmpFileName to (the reverse of every character of myFileName) as string -- this flips it around in a temp variable


set myLength to (length of myFileName) - the (offset of "." in tmpFileName) -- this counts letters in the filename and then subtracts the count of the extention


set the name of im to characters 1 thru myLength of myFileName as text -- This sets the name in Photos to the file name minus the extention (.jpg, .jpeg, .gif...etc)


end repeat -- This is so that this script does all the Photos you selected.


end if


end tell


end run

Feb 8, 2022 11:38 PM in response to smithchuck

Hi - Sorry not a scripting expert, so can't help you with yours, but there are scripts the community has already created to do what you want (I think):


Script: Batch Changing the Titles to the … - Apple Community

Script: Batch Change the Title of a Photo… - Apple Community

Script: Changing the Title to the Filenam… - Apple Community


For more, see the scripting section here:

https://discussions.apple.com/docs/DOC-250000226


I think in later versions of MacOS - many of the scripts can only work on the library, or a top level album.

Feb 9, 2022 2:07 PM in response to TonyCollinet

Thanks. I'm barely a scripting beginner, let alone an expert! So I was using one of the scripts that had already been posted on the site you pointed me to, although now it looks like I'll need to explore some of the others. And I was using this script on the library itself (I at least followed a tip I read here to create a second "test" library for this purpose, but it was the active library when I ran the script).

Question about using Shortcuts (or Automator) app with Photos app [cross posting]

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.