macxo

Q: 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 "," using command 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

Close

Q: Batch Changing in Photos, Tweaks to other Scripts

  • All replies
  • Helpful answers

Page 1 Next
  • by léonie,Helpful

    léonie léonie Aug 4, 2015 12:47 PM in response to macxo
    Level 10 (105,183 points)
    iLife
    Aug 4, 2015 12:47 PM in response to macxo

    Great!

     

    I put a link to your scripts into the User Tip section: 

     

    Changing the Title to the Filename without Extension

     

    Please add comments to this link.

  • by Barry Wallack1,

    Barry Wallack1 Barry Wallack1 Aug 5, 2015 7:22 AM in response to macxo
    Level 1 (18 points)
    Photos for Mac
    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?

  • by macxo,

    macxo macxo Aug 5, 2015 9:01 AM in response to Barry Wallack1
    Level 1 (0 points)
    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:

    Change Name to File Name without the Extention.workflow 2015-08-05 at 11.14.49 AM.png

    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:

    Screen Shot 2015-08-05 at 11.46.36 AM.png

    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:

    Screen Shot 2015-08-05 at 11.22.13 AM.png

    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

  • by Barry Wallack1,

    Barry Wallack1 Barry Wallack1 Aug 5, 2015 10:47 AM in response to macxo
    Level 1 (18 points)
    Photos for Mac
    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.

  • by macxo,

    macxo macxo Aug 5, 2015 12:56 PM in response to macxo
    Level 1 (0 points)
    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

  • by macxo,

    macxo macxo Aug 5, 2015 12:57 PM in response to Barry Wallack1
    Level 1 (0 points)
    Aug 5, 2015 12:57 PM in response to Barry Wallack1

    Barry Wallack1,

     

    I fixed the script. Please give it another shot.

     

    - Ken

  • by Barry Wallack1,

    Barry Wallack1 Barry Wallack1 Aug 5, 2015 8:25 PM in response to macxo
    Level 1 (18 points)
    Photos for Mac
    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!!!!

  • by Barry Wallack1,

    Barry Wallack1 Barry Wallack1 Aug 6, 2015 4:30 AM in response to Barry Wallack1
    Level 1 (18 points)
    Photos for Mac
    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.

  • by macxo,

    macxo macxo Aug 6, 2015 7:52 AM in response to Barry Wallack1
    Level 1 (0 points)
    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

  • by Barry Wallack1,

    Barry Wallack1 Barry Wallack1 Aug 6, 2015 11:47 AM in response to macxo
    Level 1 (18 points)
    Photos for Mac
    Aug 6, 2015 11:47 AM in response to macxo

    Thanks again for all your help. I will make 2 new posts.

  • by deomedea,

    deomedea deomedea Dec 7, 2015 12:31 AM in response to macxo
    Level 1 (4 points)
    iLife
    Dec 7, 2015 12:31 AM in response to macxo

    Thank you very much to you and the others who have shared and improved the script!

     

    A big thanks too to Old Toad for converting the script into an application, which I find easier to use.

     

    You (plural) have solved a problem that was making me mad!

  • by TheWildRover,

    TheWildRover TheWildRover Jan 6, 2016 4:32 AM in response to macxo
    Level 1 (9 points)
    Jan 6, 2016 4:32 AM in response to macxo

    Brilliant stuff, many thanks, these have helped me out like I wouldn't have believed.

     

    Cheers.

     

    Andy.

  • by mspvenice,

    mspvenice mspvenice Feb 18, 2016 3:39 PM in response to macxo
    Level 1 (0 points)
    Feb 18, 2016 3:39 PM in response to macxo

    Yes, Leonie......you are the BEST!  Thanks so much...you fixed my problem.

  • by léonie,

    léonie léonie Feb 18, 2016 3:43 PM in response to mspvenice
    Level 10 (105,183 points)
    iLife
    Feb 18, 2016 3:43 PM in response to mspvenice

     

Page 1 Next