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

May 15, 2017 8:17 PM in response to macxo

There are a couple of errors in the first script (which excludes the file extension in the name). First, part of a comment is appended to the first "end tell" statement without the comment syntax. Second, the imageSel variable is not defined. There needs to be a "set imageSel to (get selection)" statement before it is invoked. Otherwise, the script works great once these are fixed. Thanks for making it available.

Sep 11, 2017 6:32 AM in response to macxo

Hello


I did some other tweaking to batch-rename pictures to a sequentially numbered name and its original filename. I needed this as I got a couple of pictures from different people, ordered and selected them and wanted to export the pictures again (to send them to the fotographers), but without loosing neither the sorting nor the link to the original picture.


To be run directly in ScriptEditor:

set n_digits to 3 -- how many digits for the appended number

set answer to display dialog "Select the number of digits for the appended numbers (0 .. 10). Selecting '0' will suppress the leading zeros." buttons {"o.k"} default answer n_digits

set input to "YourFileNames" as text -- The text to prepend to the number



-- For 1-9 photos use 2. For 10 to 99 photos use 3 and use 4 for 100 or more photos.



set n_digits_text to the (text returned of answer)

set n_digits to n_digits_text as number



tell application "Photos"

activate

set counter to 1

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

if imageSel is {} then

error "Please select some images."

else

repeat with im in imageSel

set ntext to the counter as text

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

set ntext to "0" & ntext

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 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 tmpFileName 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)


set the name to input & "-" & ntext & "-" & tmpFileName as text

-- set the title to the input plus formatted number

set counter to counter + 1

end tell

end repeat

end if

end tell

return "Done. The titles of " & (length of imageSel) & " photos have been changed."

end run

Aug 5, 2015 7:22 AM in response to macxo

I am new to Automator and I am trying to use your Script to use my Photo's Filename as my photo's Title. I usually keep my Photos in organized files and each photo has a Filename. When I add these Photos into Iphoto, the Filename shows up as the Title of the imported photo. This is NOT true in the Photos Application. I want to have the Photos Application "move" my photos filename into the Title Field. Currently, the photos that I have imported into the Photos App have a Title of (Blank - Untitled).


I opened Automator and select Workflow. I drag your script into the Automator Window. When I select Run, it says it has Run but nothing happens. Obviously, I am doing something wrong. Can you help?

Aug 5, 2015 9:01 AM in response to Barry Wallack1

Sorry that you are having problems. I hope I can help. I am going to assume that you are up-to-date on all OS software.


Automator can look confusing with the Run AppleScript module. It has two "play/run" buttons. One is for the script itself and the other is to run the Automator Workflow. If you want to run the Script (either one of the above that I posted) out the Automator window, you will have to do a couple of things: One- Make sure you have the Photos App up and running with at least one Photo chosen. Second- make sure you copied the Script into Automator correctly and hit that hammer button at least once to make sure you got all of the script. It should look like this:

User uploaded file

Notice the "Hammer" button under "Run Apple Script" and notice that the script starts with "on run..." and ends with "end run." Make sure you don't have anything else before or after.


Now if that all looks good, then you can save the file to a place of your choice or into the scripts menu area (explained in first post and other places in this forum) or you can save it anywhere you like but when you run it, you will have to run it by launching Automator. Of course, since you saved it should look like the above with exception of all the text being green (that is an Automator thing and not a user thing). I always like the colors so I always hit the hammer button to make sure I have done anything odd to the file (I have MS, so I can twitch at odd times).


If all looks good in Automator, then make sure you have Photos open with Photos selected that you want to have the names changed to their file names. Now go back to the Automator window (If you are in full screen, you can do a 'cmd-tab key' to get back to Automator. Now take a look at the top right of the Workflow Window. See the "Record," "Step," "Stop" and "Run" buttons? These are usually used to test your Workflow but you can run the Workflow by hitting the Run button. The Workflow with it's AppleScript should take you back to Photos and if you wait a moment (time will depend on how many images you picked in Photos) and be patient if you picked many (hundreds) and Automator should Rename or add a name to the selected Photos.


The other "Run" button, thought I do not recommend using it, seems to work for me but of course I cannot see your screen or the steps you are taking. Also, you state that you "dragged" my script to the Automator window. If you dragged directly from the browser window, you may have dragged some html coding without realizing it. Try this to make sure you are only getting the text: Copy the text from the browser window. Open TextEdit and make the TextEdit document as "Text Only." That choice is under the "Format" menu. Now paste it into the plain text window. It should look like this:

User uploaded file

Now, do an 'cmd-a' to select all and 'cmd-c' to copy. Go to Automator, pick Workflow. Find the "Run AppleScript" module and drag it to the right section of the window, which will create a window within a window. In that window within a window (make sure your curser is in there) do a 'cmd-a' and then a 'cmd-v.' What you've done is selected what is automatically placed in that window and replaced it with the plain text. The text will be all green in color. Now hit the hammer button, and it should look like the 1st image. Now you can try running the script again by hitting the upper right "Run" button. If you still are having problems, try this to see if the script is doing something: In Photos, don't have any photos selected. If the script is doing something, it should pop you back into the Finder and give you this message:

User uploaded file

If you don't get this message, then the Script is not running or running correctly.


Let me know how it goes. I hope it works...


Sincerely,

Ken

Aug 5, 2015 10:47 AM in response to macxo

Thanks for all the Info. Probably getting close but still having problems.


1. The first time I ran the script I got an error message that said expected End Tell instead found a But

I looked at the script and it has the line end tell but don't delete the quote marks. I edited this line and made it say

end tell -- but don't delete the quote marks

Not sure this is correct but it did eliminate that error message.


2. Now when I run the script I get "Run Apple Script encountered an error. Check the Actions Properties and try running it again". Not sure what this means but down at the bottom of the script window in the Log section I see

Run AppleScript failed - 1 error

The variable imageSel is not defined

I do have the Photos Application running and I do have pictures selected. I get the same error message ( as expected) even when I do not have images selected.

Aug 5, 2015 12:56 PM in response to macxo

Ok, I messed up! When I copied and pasted into this forum from ScriptEditor and Automator, it deleted some line... HEY APPLE, how about fixing that!


Here they are corrected via text edit to make it plain text... The first one corrected changes image names in Photos to the file name without the extension:


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


Here is the next one corrected that does the same as the above but adds numbers on the end:




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 ore else if you want more than 3 digits but if you do nothing, it will give enought digits, it just won't show lower digits with leading zeros if you have over 999 images you want to number.

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


-- 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 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 & " - " & 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


That should fix the above scripts. It is totally my fault for not getting someone else to proof read. My apologies to all those who tried to use it and got strange messages or nothing at all, especially to Barry Wallack1 !! If he hadn't tried to make this work, I would have never seen the errors.


- Ken

Aug 5, 2015 8:25 PM in response to macxo

I want to thank everyone for all the help you provided. The adjusted script works perfectly and I can now begin to migrate from Iphoto to Photos and have my photos display as desired. Again I want to thank everyone.


I am sure I will have additional questions as I continue to migrate over to Photos and I hope you all will still be willing to provide advice and suggestions. Thanks again!!!!

Aug 6, 2015 4:30 AM in response to Barry Wallack1

Here are a few more questions. Not sure if protocol requires me to make totally new Posts but you were all so knowledgeable that I thought I would ask here:


When I open my Photo Album - now with all the Photos Titled properly - I can view my Photos as a single large Album ( using ALL Photos). When viewed in this manner is there any way that the photos can sorted by Title instead of sorted by whatever property Apple is choosing? Can a Script be written to accomplish this? Same question is true for when photos are viewed under the Photos option. Within each date grouping, can the photos be sorted by Title?


Second question - Can the Geographical Title that is displayed in front of the Date (in the Photos Option) be changed. I assume this geographical name is being picked up via the metadata of the photo in some manner.

Aug 6, 2015 7:52 AM in response to Barry Wallack1

Barry,


I ran into both of those "walls" recently. As far as I can tell, the only way to sort is by date. You can move things around in an Album but you cannot tell Photos to sort by name or filename. I looked at the AppleScript Library of Photos and there is no way to tell it from the inside to sort images in a different way.


This goes the same for the GPS markers.


This would and should come in some kind of update, I would assume.


The work around that I use when needed, and you will hate this, is to bring the images into a new iPhoto Library. Do necessary changes and sorting there, then export the files. Don't forget to delete the originals IF you don't think you need them. Obviously do some test runs to make sure of Export Settings in iPhoto.


That when I ran into this names of files not showing in Photos.


I would also strongly suggest that you start two new topics on this. The more people question this, the more of a chance that Apple will notice.


Thanks for being patient with me,

Ken

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.