You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

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

Batch Changing in Photos, Tweaks to other Scripts

This is not a question but a possible answer(s).


First I must give credit to users of this forum, léonie and Old Toad for sharing their AppleScript for doing some of the things that were easily done in iPhoto. For those of you lamenting for iPhoto, keep in mind that they never have fixed the memory leak in that program but I do understand in the sense that Photos is missing many things that we used and liked. One of the most important was being able to batch edit images. The answer for the power users is AppleScript or Automator or the combination of the two. I ended up using a combination. Bear with me here... AppleScript has changed a lot since OS X and keeps on changing with new versions of the OS. I can't even write a script to open what is in the Clipboard (back in the day, when you did a cmd-c or cmd-x, it went to the clipboard and you could open a window of the clipboard). Needless to say, since it has been changing so much, some us who use AppleScript now have to adapt.


Automator is new, too new. It doesn't have nearly the capabilities as the old Applescript. Back in the day when I was learning AppleScript (in OS 9 BTW), I was able to create a Video Poker Game using only AppleScript!


I know some of you guys (I use guys to include gals) either like AppleScript or Automator and fight to use both. The one biggest thing I have noticed that when an AppleScript is run in using Automator, it seems to run smoother and more consistently. I made an old script a little newer and tried to run it via SriptEditor App. It would work once or even twice and then do strange things afterwards. If I run it via Automator, it works everytime.


léonie and Old Toad have been great in sharing and in that vane, I would like to share also. Here are three scripts that should be run through Automator as a Workflow. Workflow seems to me the most stable of all the choices given. I've tried Services and compiling to an App and it is buggy to say the least. So again, create a work flow, add the "Run AppleScript" via the Script Menu. If you don't have that menu, do this. Open "ScriptEditor." It is in your Utilities folder, which is in your Applications folder. Create a new script and copy and paste the following:

tell application "Script Editor"

activate

tell application "System Events"

keystroke "," usingcommand down

end tell

end tell


What should have happened the Preference window in ScriptEditor should have Opened up. Along the top "General" should be high-lited. Put a check mark in the box for "Show Script Menu in Menu Bar." You will see a new icon in your menu bar. Click on it an you can open Script folders. One for the app you are currently in and one for you only and one for all the users of the computer. I save my stuff into "User Scripts Folder." Within that folder, you will see another folder called "Applications." Open it and you will see specific Application folders. These are the places you put your workflow you make in Automator for specific applications. In this case, it would be a fold called "Photos." You should already have one if you've switched over to Photos. Go back to the ScriptEditor and you can quit it without saving. Remember that these scripts are run in Automator, not in ScriptEditor nor the older AppleScriptEditor. I am also running OS X 10.10.4. All this is important because Apple keeps on changing AppleScript and Automator behavior, so your milage may vary.


This one just changes your image's name to the image's file name without the extension. You select as many images as need, though I haven't test using thousands of images. Again, I've used much if not all of the scripts that léonie and Old Toad made. I've only added small amount to it and some more explanation as to what is happening

on run {input, parameters}

tell application "Photos"

activate

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

beep -- sometimes a noise before you walk away is a way to get your attention, you can delete this line if you get beeped too often

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 but don't delete the quote marks

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


-- At 'end run' is the end of the script. The next one does a bit more. It does the same thing above but also add trailing numbers at the end of the name. Unlike some of the other scripts, it will not ask you for how many trailing digits you want to start. It will always default to 2 digits (it will start at image - 01) unless you pick more than 100 images, then it will start with 001.


on run {input, parameters}

set n_digits to 2 -- how many digits for the appended number as a default as in 01, 02...etc. You can set this to 1 so there are no leading zeros

tell application "Photos"

activate

set counter to 1 -- This is the number used for the first image

set imageSel to (get selection) -- get selected images

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 and Number and re-start this Script" -- this string can be changed to whatever you need to say

end tell

else

if (count of imageSel) is greater than 99 then -- if more than 100. You can also removed this so there are no leading zeros.

set n_digits to 3 -- ... set digits to 3 as in 001, 002 ... etc.

-- you can add more if's or else if you want more than 3 digits but if you do nothing, it will give enough digits, it just won't show lower digits with leading zeros if you have over 999 images you want to number as in 0001.

end if -- delete this if you deleted the previous if statement. Of course you need to delete everything in-between.


-- I suggest that if you need to remove stuff, comment it out using the dashes just in case you want it back


-- This next part came with the help I got from "old toad" from this Internet page: https://discussions.apple.com/docs/DOC-8481


repeat with im in imageSel

set ntext to the counter as text -- This numbers the image

repeat while (the length of ntext < n_digits) -- add leading zeros for numbering

set ntext to "0" & ntext -- this adds leading zeros

end repeat


tell im

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 reverses the file name and puts a temp variable, a fancy way of saying holder

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 & " - " & ntext as text -- This sets the name in Photos to the file name minus the extention (.jpg, .jpeg, .gif...etc) adds " - " plust leading zeros if any and then the number of the Photo

set counter to counter + 1 -- Adding one will make sure that the next image is labled 1 more than the previous

end tell

end repeat

end if

end tell

end run


-- At 'end run' is the end of the script. Please note that I've only tweaked whatléonie and Old Toad have already created. Also note that with the second script, if your file is names "My Son with fishing.jpg"... etc., name in Photos will be "My Son with fishing - 01" and not "My Son with fishing-01", the difference being the space in front and in the back of the dash. You can changed the delimiter, the space dash space, to any thing you want. Look in the second and find: & " - " &

You must keep the &'s and " marks but you can change what ever is inside the quote marks.


I hope there aren't any mistakes in here and if there is, please let me know to change it. Look at léonie and Old Toad's Scripts and Automator Workflows and you may become the next expert.


If you need further explanation on these scripts or anything Macintosh related, feel free to contact me.


I hope I help some one.


From a retired TechGuy - Ken

Mac Pro, OS X Yosemite (10.10.4)

Posted on Aug 4, 2015 12:30 PM

Reply
34 replies

Mar 6, 2016 4:02 PM in response to léonie

I just upgraded my iMac from Mavericks to El Cap the other day and am beginning to regret it... especially the iPhotos to Photos change.


I've tried the script above (the corrected version) but I keep getting the message,

error "" number -1721

from Script Editor. I'm using the code below:

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

I can see no obvious errors. I have Photos open, and have tried selecting "Photos" and "All Photos" with all my pictures in these folders selected also. I keep getting the error message.


Any suggestions?


Thanks!

Mar 7, 2016 11:29 AM in response to léonie

H Leonie - no, I am not using iCloud.


I'm running OS X El Capitan, with about 82,000 photos that have been migrated from iPhoto 09 to Photos.It's a late 2009 27" iMac - i5 with 12GB RAM.


To say I'm not a fan of Photos is an understatement, but unfortunately there is no choice in the matter.


To migrate, I had to manually find and open the iPhotos Library as I had moved it to an external drive due to its size. The El Cap/Photos upgrade wasn't smart enough to see the path change in iPhoto 09 to find the library by itself.


It ran overnight to convert the library before it showed the library contents. 2 days later I see that Photos is still consuming huge amounts of CPU power (150%-180%, so almost 2 full cores) and Photos is sluggish to say the least.


When I tried running the script in Script Editor I didn't get anything other than a blink of the Script Editor screen and then 'Error "" number -1721' - yes, those are a pair of blank double-quotes after Error. I am unable to figure out what is causing this error or where it is occurring in the script.


Tonight I'll restart the iMac and try again. I'll let you know if anything changes.


Thanks!


-Allan

Mar 11, 2016 8:37 PM in response to aaoyama

The high CPU usage and disk activity seemed related to some processes Photos was running in the background on my pictures - possibly face recognition (though I turned it off) or generating thumbnails. Who knows. After over 80 hours of high processing usage it finally stopped.


I just tried again after rebooting and restarting Photos.Still get the same script error:


error "" number -1721


Even when I don't select any images, I get the same error. No warning dialog to select images etc. It's behaving as though it's erroring out before it even gets to activating Photos.


To recap: I start Photos. I select the Photos library, Select All images and then switch to Script Editor where the script is loaded. I click the Run icon. Nothing seems to happen but I get the error.


I have also tried selecting the All Photos album and then selecting all images. Similar result.


Any suggestions?


Failing that, is there any commercial utility that will do this? I don't have a lot of coding skill or time and to be honest, I'd rather just have something fix it than fight with it...


Thanks


-Allan

Apr 5, 2016 9:49 AM in response to aaoyama

Ok, just realized I should update my issue.


It turns out I didn't read the instructions completely and was trying to run this in Script Editor rather than Automator. After loading it up into Automator it still wasn't behaving properly, but after a reboot I finally was able to select a group of photos in Photos and run the script in Automator successfully.


I was able to run it on over 80,000 photos - it took about an hour (Late 2009 iMac 27" with i5/2.8 and 24GB RAM). Since then all is running well.


Thanks to all for sharing the scripts to make this work! Now if only Apple would update Photos to provide an option within the app instead of having to do this manually after every import...


-Allan

Dec 21, 2016 2:23 AM in response to dmoffat322

Hi! This is the script which I use, which was given me by others. There does seem to be a slight difference on how it works. If you're adding photos to an existing album or event, the script works best in the Latest Import section before adding the photo to the album or event. All the best with this.


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

Batch Changing in Photos, Tweaks to other Scripts

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