Script: Batch Changing the Titles to the Filename w/Extension

by: 
Last modified: Oct 24, 2017 12:39 AM
99 71966 Last modified Oct 24, 2017 12:39 AM

In iPhoto the filenames have been as used as default titles and shown below the thumbnails in the browser. Photos does not show the filenames as titles, and if a photo has no title, there is no identification visible in the browser, only in the Info panel.

To batch change the titles of photos without title, to make them visible below the thumbnails, you could try an Automator script I wrote to bring the titles back:


Note: If you are using iCloud Photo Library scroll down to "Version2" - the Apple Script version. The Automator script version does not work well with iCloud Photo Library on Yosemite or El Capitan.



Create an Automator action, that executes an Apple Script to change the titles to the filename, if the title is empty:


The Apple Script:


on run {input, parameters}

tell application "Photos"

activate

set imageSel to (get selection)

if imageSel is {} then

error "Please select an image."

else

repeat with im in imageSel

set title to the name of im

if not (exists (title)) then

set the name of im to the filename of im

end if

end repeat

end if

end tell

end run

Launch Automator, create a new service, and 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.

User uploaded file

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

  • Now launch Photos and select a few test images.
  • Open the "Photos Menu > Services".
  • The service should be shown in the menu - I saved my version as "filename2title".
  • Select the service to let it run. When you click the selected photos again, the titles should change.


User uploaded file

After the Automator action completed:

User uploaded file



I put a version of the Automator workflow here in my Dropbox:

https://www.dropbox.com/sh/dzb49hg8rm8vjr8/AACvH-nRhY0B9su9PYR7em5Oa?dl=0


My script will just give you the general idea how to access filename and title in an Apple Script. For a more elaborate AppleScript, with more error checks and removing the filename extension, check out the script that NicFletcher posted:

A sample AppleScript and some technical observations on Photos


For a discussion on how to improve this script, see: Re: Re: Where are photo file names?

Re: Re: Where are photo file names?




-------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------

Version 2: To be used with iCloud Photo Library

-------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------


This version of the script is to be run directly from the Script Editor. The photos are not passed by selecting them in Photos but by collecting them in a top level album with the fixed name "PhotoDropBox". All photos in this album will get their titles changed, when you press the run button in Script Editor.



-- batch change the title of images to the filename

(* 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 photos are selected or in the album and all parameters set, press the "Run" button in Script Editor.


*)


set ReadFromAlbum to true

-- 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 {}


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 its name to its filename

end tell

on error errText number errNum

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

try

delay 2

tell im

set its name to its filename

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"

return "Done"

Comments

Apr 26, 2015 9:44 AM

I find that the script won't work if there's a title already in the field or there has been a title in the field and was deleted in Photos. How can we modify the script to that it overwrite whatever is currently in the field?

Apr 26, 2015 9:44 AM

Apr 27, 2015 10:24 AM

Hi, Old Toad.


It is intentionally, that the script does not overwrite titles, that have been added manually.

But if you want to force overwriting existing titles, delete a few lines and remove the test, if a title exists:


repeat with im in imageSel

set title to the name of im

if not (exists (title)) then

set the name of im to the filename of im

end if

end repeat

Just keep this in the repeat loop:

repeat with im in imageSel

set the name of im tothe filename of im

end repeat

Apr 27, 2015 10:24 AM

Apr 27, 2015 11:55 AM

I ended up with this which works :

tell application "Photos"

activate

set imageSel to (get selection) -- get a list of selected images

set currentfilename to ""

if imageSel is {} then

error "Please select an image."

else

repeat with im in imageSel

set currentfilename to the filename of im as text -- retrieve the filename of image "Im"

set newname to currentfilename

set the name of im to newname -- write the newname to the title field

end repeat

end if

return currentfilename -- return the filename of the last image

end tell

My earlier version gave me this when I did the entire library:

User uploaded file


with incredibly large sequential numbers appended at the end. The new version produced this:

User uploaded file

Thanks for the help.

Apr 27, 2015 11:55 AM

Apr 27, 2015 12:42 PM

Thanks Leonie & Old Toad for taking the time to post those scripts.


I've modified the filename script as Old Toad posted and it works great for using filename for titles. I've tried modifying it to change the title to the date instead of filename. I changed retrieve the filename to date and it worked. But it returns the long output of text (Monday, April 27, 2015 @ 4:00 PM) instead of short date & time output (4/27/15 @ 4:00 PM). How would I change the output to show the short version of the date & time?

Apr 27, 2015 12:42 PM

Apr 28, 2015 12:14 AM

Try the following, Glenn:


Apple Script will use the "Full date format" as set in the System Preferences > Language & Region > Advanced" to return a date string. But the "Date" class has a property to return the date string in a short format - use "the short date string of xxxdate". It will use the Short format you set in the System preferences: Click the arrows to the right of the format entries to switch between "=1" and "Jan" etc. If you changed the formats, restart the mac or running apps may crash!

User uploaded file


This script works for me with the System Preferences set as above:

User uploaded file




(* Batch change the titles of the selected photos to the capture time


How to use this script:

- select all photos you want to change the title in Photos

- Open this script and run it by pressing the "Run" button.

- The script will resd the capture date from the first image and create a short date string according to the system preferences for the shart date format

- The script will return the last date of the last photo it changed

- if you save this script as an Application you can add it to the Dock and run it from there

This script has been tested in Photos version 1.0, with MacOS X 10.10.3

© Léonie

*)

(* select at least 1 image in Photos *)

tell application "Photos"

activate

set imageSel to (get selection)

if (imageSel is {}) then

error "Please select at least one image."

else

repeat with i from 1 to count of imageSel

set next_image to item i of imageSel

set capture_date to (the date of next_image)

set short_capture_date_string to the short date string of capture_date

set capture_time_string to the time string of capture_date

set new_title to short_capture_date_string & " @" & capture_time_string

tell next_image

set the name of next_image to the new_title as text

end tell

end repeat

end if

return "Adjusted the titles of " & (the length of imageSel) & " photos. The last date is " & ((the date of next_image) as date)

end tell

Apr 28, 2015 12:14 AM

May 20, 2015 1:20 PM

I've created an application rather than an service which I find easier to use. Here's a link to download the already compiled app for those who would rather use an app to do the renaming rather than a service: PHOTOS File Name to Title

May 20, 2015 1:20 PM

May 20, 2015 1:39 PM

That is a good idea, an app is easier to handle. The advantage of the service is, that it can be bound to a keyword shortcut.

May 20, 2015 1:39 PM

Jun 22, 2015 11:31 AM

léonie wrote:


That is a good idea, an app is easier to handle. The advantage of the service is, that it can be bound to a keyword shortcut.

I forget about that since i use Keyboard Maestro to assign keyboard shortcuts to apps, posts here in the ASC, etc. I've become very dependent on KM for just the every day usage of my Mac. Could that be called an addiction? 😟

Jun 22, 2015 11:31 AM

Jun 22, 2015 1:49 PM

I used to be equally addicted to Launchbar, but now I use mainly Spotlight as launcher. I hope the new El Capitan version will not mess it up by presenting sporting results and shopping recommendations instead of launching the app I want.

Jun 22, 2015 1:49 PM

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