Skip to content

Instantly share code, notes, and snippets.

@nkalvi
Last active December 11, 2019 01:28
Show Gist options
  • Save nkalvi/0ff4c8b8dc83d14cb7ecc7f15ee861f1 to your computer and use it in GitHub Desktop.
Save nkalvi/0ff4c8b8dc83d14cb7ecc7f15ee861f1 to your computer and use it in GitHub Desktop.
AppleScript to set item titles based on album name - in Mac Photos
-- Purpose:
-- In Mac Photos, traverse selected albums and set title to the following format, preserving the order of items
-- [Album Name]-xx of xx (then export choosing titles as file names)
--
-- Why? - wouldn't export in Photos with 'Album Name with Number' work?
-- 2019-12-10
-- Fixed! in Catalina 10.15.2, Photos Version 5.0 (121.17.170)
-- 2019-12-09
-- This script is not needed if the export function in Photos preserved the order when selecting
-- sequential numbering or auto-numbering scheme; it worked when exporting unmodified originals.
-- Opened case with Apple on 2019-12-08; looks like it worked in Mojave, so they may fix it in a future release.
--
-- Tested on:
-- Catalina 10.15.1 and Photos Version 5.0 (111.16.180)
--
-- Caveats:
-- Existing titles will be overwritten.
-- If an item is included in multiple albums, the title will be based on the last album handled.
--
-- Used code/ideas from
-- https://gist.github.com/SidShetye/ddf9b9870ea0ddb4bf112a58b0b0fbcc
-- https://ashmug.com/2017/02/batch-change-photo-titles-in-photos-app-handout/
------------------------------------------------
-- Settings Start: Change these as needed
set allowUserToSelectAlbums to true as boolean
-- Settings End
------------------------------------------------
tell application "Photos"
set allAlbumNames to name of albums
if allowUserToSelectAlbums then
set albumNames to choose from list allAlbumNames with prompt "Select some albums" with multiple selections allowed
else
set albumNames to allAlbumNames
end if
if albumNames is false then
return
end if
end tell
activate -- to show progress bar, otherwise it will not be visible when invoked as an application
-- Update the initial progress information
set theAlbumCount to length of albumNames
set progress total steps to theAlbumCount
set progress completed steps to 0
set progress description to "Processing albums ..."
set progress additional description to "Preparing to process "
delay 1
set albumCounter to 0 -- for progress indicator
repeat with albumName in albumNames
-- Update the progress detail
set albumCounter to (albumCounter + 1)
set progress additional description to "Processing album '" & albumName & "' (" & albumCounter & " of " & theAlbumCount & ")"
delay 1
tell application "Photos"
set mediaItems to (get media items of album albumName) -- order of items within the album is preserved
end tell
my nameItemsInAlbum(albumName, mediaItems)
activate
-- Increment the progress
set progress completed steps to albumCounter
-- Pause for demonstration purposes, so progress can be seen
delay 1
end repeat
-- Reset the progress information
set progress total steps to 0
set progress completed steps to 0
set progress description to ""
set progress additional description to ""
display notification "Finished processing." with title "Setting Titles to Photos"
on nameItemsInAlbum(albumName, mediaItems) -- Set title to albumName-xx of xx
tell application "Photos"
set totalItems to count of mediaItems
set n_digits to length of (totalItems as string) -- leading zeros is based on digits total items
set counter to 1
repeat with mediaItem in mediaItems
-- add leading zeros
set ntext to the counter as text
repeat while (the length of ntext < n_digits)
set ntext to "0" & ntext
end repeat
-- set title (name)
-- Caveats:
-- If an item is included in multiple albums, the title will be based on the last album handled.
tell mediaItem
set the name to albumName & "-" & ntext & " of " & (totalItems as text) as text
set counter to counter + 1
end tell
end repeat
end tell
end nameItemsInAlbum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment