I’m looking for help with an Applescript problem for Photos. I’m very new to Applescript and can’t figure out how to solve it.
I would like to create a script that gets the name of the album, which has been selected in Photos (e.g. Holiday 2023).
Next, a dialog window should open up showing this name “Holidax 2023” predefined in the dialog or input field. The name can now be modified manually (to e.g. Holiday 2023.01).
I want to use this Input data for another script.
I would be very grateful for any suggestion or help.
Borima. I looked at the Photos app dictionary and could not find a simple way to do what you want. FWIW, I’ve included below a handler that worked in limited testing on my Sonoma computer. The handler could be slow if the number of photos in the app is large. BTW, it seems like you should be able to get the album directly from a media item, but I couldn’t get that to work.
try
set theAlbum to getAlbum()
on error
display dialog "An album containing photos was not found" buttons {"OK"} cancel button 1 default button 1
end try
display dialog "The selected album is:" default answer theAlbum
on getAlbum()
tell application "Photos"
set thePhotos to selection
set thePhotoID to id of (item 1 of thePhotos)
set theAlbums to every album
repeat with anAlbum in theAlbums
set albumPhotos to every media item in anAlbum
repeat with aPhoto in albumPhotos
if (id of aPhoto) is thePhotoID then return (name of anAlbum)
end repeat
end repeat
end tell
end getAlbum
One more question. How to get the number of medias (fotos, videos) within this routine above for this album but get as a result the number of digits? For excampel 10 fotos would lead to 2. 900 Fotos to 3 and so on.
Another way to get the name of the selected album is to force an error.
The error message which looks like
Photos got an error: Can’t get album “com.apple.Photos” of media item id “A925E14E-8E0D-4AA2-93AF-064234280755/L0/001” of album id “20C29387-0B69-4D0B-91EF-8ED4ADB4B359/L0/040”.
contains the id of the parent album, it can be simply extracted with text item delimiters
tell application "Photos"
set thePhotos to selection
try
set thePhotoID to album id of (item 1 of thePhotos)
on error e
if e contains "get item 1" then
display dialog "No album selected"
else
set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {quote}}
set albumID to text item -2 of e
set AppleScript's text item delimiters to saveTID
set albumName to name of album id albumID
display dialog albumName
end if
end try
end tell
That’s excellent Stefan. I had attempted to get the album id by coercing the media item id to a string but without success. Accomplishing that by forcing an error would never have occurred to me. BTW, your script’s timing result was 4 milliseconds (without the dialog).
The album stays unchanged. But all titles of medias (fotos, videos) shall be automatically renamed:
Album name : “20230120 London”.
When less then 100 fotos renameing of foto titles:
“20230120 London, 01” “20230120 London, 02” … “20230120 London, 99”
When less then 1’000 fotos renaming of foto titles:
“…, 001” “…, 002” → “…, 999”
Albums are located in folders. I use iCloud mediathek.
I have already got a script (see below) wich adds an incrementing number with leading “0”. But you must enter the digit number manually. The question for me is, how to replace this dialog by an automatc procedure (automatically getting max number of fotos of the album and from there getting the digit number as a result).
global albumName
-- get the name of the album
its getAlbumNameOfSelection()
on getAlbumNameOfSelection()
tell application "System Events" to tell application process "Photos"
set frontmost to true
tell group 2 of splitter group 1 of window 1
set albumName to first item of ((name of static texts) as list)
end tell
end tell
end getAlbumNameOfSelection
-- get numer digit number
set n_digits to 2
set answer to display dialog "Digit numbe:" 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 next_image in imageSel
-- the counter with padded numbers
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
-- set new_title
set new_title to albumName & ", " & ntext as text
tell next_image
set the name to new_title as text
set counter to counter + 1
end tell
end repeat
-- return new_title
end if
end tell
return new_title
Yes almost correct
The digit number could start also from 1 and end maybe at 5 (1 when tehre are <10 photos, and 5 when there are >= 10’000 photos).
My problem is, I am an absolut beginner with scripting and would just like to know how to replace that part of my existing script (see below) by a procedure, which which provides the variable “n-digits” with the digit number (automatically detected).
The rest of my script works fine.
-- get numer digit number
set n_digits to 2
set answer to display dialog "Digit numbe:" 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
Borima. The following script demonstrates how this issue might be addressed. My photos are in an album in a folder on my local computer, so this may not work for you. I was unable to get your script to work reliably on my Sonoma computer.
tell application "Photos"
set thePhotos to selection -- this section from Stefan
try
set thePhotoID to album id of (item 1 of thePhotos)
on error e
if e contains "get item 1" then
display dialog "Album not selected or no photos in selected album" buttons {"OK"} cancel button 1 default button 1
else
set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {quote}}
set albumID to text item -2 of e
set albumName to name of album id albumID
set AppleScript's text item delimiters to saveTID
end if
end try
set thePhotos to every media item in album id albumID
set photoCount to count thePhotos
display dialog "Change the title of " & photoCount & " photos in the " & quote & albumName & quote & " album?"
repeat with i from 1 to photoCount
set anImage to item i of thePhotos
if photoCount < 100 then
set photoTitle to albumName & ", " & text -2 thru -1 of ("0" & i)
else
set photoTitle to albumName & ", " & text -3 thru -1 of ("00" & i)
end if
set name of anImage to photoTitle -- set title of photo
end repeat
end tell
Perfect, this works fine at my Photos on iCloud! I might expand to 5 digits.
I’m wondering why my script is not working on your computer… Is it because of settings in the “operating assistance” in the system preferences? Or because of local database/ and icoud…?
As the number of leading zeros can vary this is an universal handler to add leading zeros depending on the maximum number of items
The handler pad takes a numeric value and a string containing the leading zeros
on pad(theValue, leadingZeros)
return text -(count leadingZeros) thru -1 of (leadingZeros & (theValue as text))
end pad
The number of leading zeros is created once from the number of items
set numberOfItems to 12345
set leadingZeros to ""
repeat while numberOfItems > 1
set leadingZeros to leadingZeros & "0"
set numberOfItems to numberOfItems / 10
end repeat
Then call the handler
set indexString to pad(45, leadingZeros) -- "00045"
Thanks everybody for your inputs which was great help for me. I could now stich my script together for my needs.
One more question not directly linked to this topic. Is the apple script editor the only one editor which can be used? I couldn’t figure out how to run the script “step by step” of each command line to check the variable values and changes.
Also I couldn’t figure out how to check what the handlers are doing.
I know it is possible to check the events in the apple script editor but from my point of view not every thing is shown as mentioned above.
Also how can you measure (as peavine wrote) the scripting timing results in milliseconds??