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.

Append Keywords to Title Field - Léonie's Script

Last modified: Jul 1, 2018 10:36 AM
2 2228 Last modified Jul 1, 2018 10:36 AM

The following Applescript by léonie will append the keywords, separated by commas, assigned to an image to the contents of the Title field for that image.


I made 3 changes to loonie's original script (shown in bold red) that allow the script to work on images in the Photos/Moments window, All Photos window and in any album. The changes also separate the existing title from the keywords with a dash ( - ). Here's an example:


User uploaded file User uploaded file

If you have no title in the title field you'll get this:


User uploaded file

Additionally, if you has the iCloud Photo Library enabled you should change "false" to "true"in "set ReadFromAlbum to false".

-- batch change the title of images to the keywords

(* How to use this script:


Open this script in Script Editor. Launch Photos.


The photos can be passed to the script in two ways:

1. Either select photos while viewing the "All Photos" album; this works better than Moments or smart albums

2. Or collect the Photos in a top level defined album with a fixed name.


If you want to select the photos without collecting them in an album, set the variable "ReadFromAlbum" to false

If you want to pass the photos in a toplevel album, set ReadFromAlbum to true and change the variable "theAlbumName" to the name of the album you are using.


When all all photo are selected or in the album and all parameters set, press the "Run" button in Scripteditor.

*)

-- return AppleScript's text item delimiters


set ReadFromAlbum to false

-- set this to true, if you want to pass the photos in a toplevel album

set theAlbumName to "PhotoDropBox" -- change this to the name of the album you will use



set imageSel to {}

set saved_delimiter to AppleScript's text item delimiters --save the delimiter



tell application "Photos"

activate

if (ReadFromAlbum) then -- the photos will be passed in a toplevel album named "PhotoDropBox"


try


if exists container theAlbumName then


set thePhotosBuffer to container theAlbumName

set imageSel to every media item of thePhotosBuffer

else

error "Album " & theAlbumName & "does not exist"

end if


on error errTexttwo number errNumtwo

display dialog "Cannot open album: " & errNumtwo & return & errTexttwo

end try


else -- process the selected photos from the All Photos album

try

set imageSel to (get selection)

on error errTexttwo number errNumtwo

display dialog "Cannot get the selection: " & errNumtwo & return & errTexttwo

end try


end if


-- check, if the album or the selected photos do contain images

if imageSel is {} then

error "Please select some images."

else

repeat with im in imageSel


try


tell im

--set the text item delimiter

set AppleScript's text item delimiters to {", "} --use a space as the delimiter

-- retrieve the keywords


set kws to its keywords as string

if not (exists kws) then

set kws to ""

end if


--retrieve the old title

set thename to its name

if not (exists thename) then

set thename to ""

else

set thename to thename & " "

end if

set its name to thename & "- " & kws

end tell

on error errText number errNum

display dialog "Error: " & errNum & return & errText & "Trying again"

try

delay 2 -- wait and try again

tell im

set AppleScript's text item delimiters to {", "} --use a space as the delimiter

-- retrieve the keywords

set kws to its keywords as string

if not (exists kws) then

set kws to ""

end if


--retrieve the old title

set thename to its name

if not (exists thename) then

set thename to ""

else

set thename to thename & ", "

end if

set its name to thename & "- " & kws

end tell

on error errTexttwo number errNumtwo

display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo

end try

end try

end repeat

end if

end tell

-- display dialog "Done"

set AppleScript's text item delimiters to saved_delimiter --restore the delimiter


return "Done"


You can download a compiled version of the script as an application from this tutorial site: P01 - Applescripts from Photos’ User Tips Compiled as Applications


User uploaded file

Comments

Jun 13, 2016 7:58 AM

The script above will append the keywords to the existing title.


Here is a second version of the script, that uses a variable to decide, if the old title should be replaced completely:

There is a new variable "replace_old_title". If this is set to "true", any old title will be completely overwritten by the list of keywords.





I also added a bug fix to prevent "Missing Value" keywords as new titles.

, see this download link:

https://dl.dropboxusercontent.com/u/45503699/PhotosBatchChangeTitleToKeywordsAlb umReplace.scpt.zip



-- batch change the title of images to the keywords

(* How to use this script:


Open this script in Script Editor. Launch Photos.


The photos can be passed to the script in two ways:

1. Either select photos while viewing the "All Photos" album; this works better than Moments or smart albums

2. Or collect the Photos in a top level defined album with a fixed name.


If you want to select the photos without collecting them in an album, set the variable "ReadFromAlbum" to false

If you want to pass the photos in a toplevel album, set ReadFromAlbum to true and change the variable "theAlbumName" to the name of the album you are using.


By default the old title will be overwritten. If you want to append to the old title, set the variable replace_old_title to false.

When all all photo are selected or in the album and all parameters set, press the "Run" button in Scripteditor.

*)



set ReadFromAlbum to false

set replace_old_title to true-- change this to false, if you want to append to the previous title


-- set this to true, if you want to pass the photos in a toplevel album

set theAlbumName to "PhotoDropBox" -- change this to the name of the album you will use



set imageSel to {}

set saved_delimiter to AppleScript'stext item delimiters--save the delimiter



tell application "Photos"


activate

if (ReadFromAlbum) then -- the photos will be passed in a toplevel album named "PhotoDropBox"


try


if existscontainertheAlbumName then


set thePhotosBuffer to containertheAlbumName

set imageSel to every media item of thePhotosBuffer

else

error "Album " & theAlbumName & "does not exist"

end if


on error errTexttwonumbererrNumtwo

display dialog "Cannot open album: " & errNumtwo & return & errTexttwo

end try


else -- process the selected photos from the All Photos album

try

set imageSel to (get selection)

on error errTexttwonumbererrNumtwo

display dialog "Cannot get the selection: " & errNumtwo & return & errTexttwo

end try


end if



-- check, if the album or the selected photos do contain images

if imageSel is {} then

error "Please select some images."

else

repeat with im in imageSel


try


tell im


--set the text item delimiter

set AppleScript'stext item delimiters to {", "} --use a comma and space as the delimiter


-- retrieve the keywords


set kws to its keywords as string

if not (exists kws) then

set kws to ""

end if


if kws is "missing value" then

set kws to ""

end if


if (replace_old_title) then

set thename to "" -- the old title will be overwritten

else

set thename to its name

if not (exists thename) then

set thename to ""

else --retrieve the old title

set thename to thename & ", "

end if

end if -- (replace_old_title)

set its name to thename & kws

end tell

on error errText number errNum

display dialog "Error: " & errNum & return & errText & "Trying again"

try


delay 2 -- wait and try again

tell im

set AppleScript'stext item delimiters to {", "} --use a comma and space as the delimiter


-- retrieve the keywords

set kws to its keywords as string

if not (exists kws) then

set kws to ""

end if

if kws is "missing value" then

set kws to ""

end if



if (replace_old_title) then

set thename to "" -- the old title will be overwritten

else

set thename to its name

if not (exists thename) then

set thename to ""

else --retrieve the old title

set thename to thename & ", "

end if

end if

set its name to thename & kws

end tell

on error errTexttwonumbererrNumtwo

display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo

end try

end try

end repeat

end if

end tell

-- display dialog "Done"

set AppleScript'stext item delimiters to saved_delimiter--restore the delimiter


return "Done"

Jun 13, 2016 7:58 AM

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