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.

Script: Batch Change the Titles to a Given String

by: 
Last modified: Jun 13, 2016 7:14 AM
5 6999 Last modified Jun 13, 2016 7:14 AM

Currently (April 2015) does Photos not yet support batch changing of titles and descriptions.


I wrote two very simply Apple Scripts, that can be used to add a title and a description to several selected photos and run from an automator work flow:


This script will prompt for a new title to be applied to all photos and then change the title to the string, with an added number.

For example, if you enter "Birthday", the titles will change to "Birthday_0001", "Birthday_0002", etc. The number of digits is a parameter in the script.


A copy of this service is in my Dropbox: https://www.dropbox.com/sh/29exskbyuuffk5h/AAA-CDQ1fonab-TxSjvQBG-Ca?dl=0



on run {input, parameters}

-- batch change the title to the input


set n_digits to 4 -- how many digits for the appended 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 the name to input & "-" & ntext as text

-- set the title to the input plus number

set counter to counter + 1

end tell

end repeat

end if

end tell

return input

end run


User uploaded file

Launch Automator,

  • create a new service,
  • drag a "Ask for Text" action into the workflow.
  • drag a "Run Apple Script" action into the workflow.

Copy and paste the text above into the "Run Apple Script" action to replace the default text in the action.


Save this workflow with a suitable name. It will be installed in the services.


  • Now quit Photos, if it is running and launch Photos again and select a few test images.
  • Open the "Photos" Menu > Services".
  • The service should be shown in the menu - I saved my version as "BatchChangeTitles".
  • Select the service to let it run.
  • You will be prompted to enter a new title for the selected photos.
  • When you click the selected photos again, the titles should change.


Or simply run the service from the Automator window by pressing the "Run" button after selecting the images.

Comments

Apr 20, 2015 10:57 AM

lèonie:


Any way to let the user determine the number of digits in the sequential number at the time of use?

User uploaded file

Apr 20, 2015 10:57 AM

Apr 21, 2015 2:18 AM

You could add these lines at the beginning of the script to prompt the user for the length of number field. Originally I did not add this feature, because it might become tedious to have to enter too many parameters:


set n_digits to 4 -- 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 n_digits_text to the (text returned of answer)
set n_digits to n_digits_text as number


I changed the script in my Dropbox correspondingly. https://www.dropbox.com/sh/29exskbyuuffk5h/AAA-CDQ1fonab-TxSjvQBG-Ca?dl=0


User uploaded file

Apr 21, 2015 2:18 AM

May 2, 2015 10:23 AM

CHANGE TITLE TO A GIVEN STRING WITH PADDED SEQUENTIAL NUMBERS AT THE END


With léonie's help I've created an application rather than a service to provide a title with a padded sequential number at the end with the option to dynamically set the number of digits in the sequential number.


The script used is as follows:

on run {input, parameters}

-- batch change the title to the input, © léonie, version 1.0.1


set n_digits to 4 -- 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 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 the name to input & "-" & ntext 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


The Automator window looks like this:

User uploaded file

When run the first window is

User uploaded file

followed by this window to set the number of digits in the sequential number
User uploaded file

which results in this:

User uploaded file


NOTE: the reason I prefer to create an application is that I've activated the Script menu in the menu bar via the Applescript Editor General preferences:

User uploaded file

It will accept scripts as well as apps and is available quickly here:

User uploaded file


Thanks to léonie for her help in modifying her original service to this as it's way above my pay grade.

May 2, 2015 10:23 AM

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