Photos 5.0 on Catalina: How to get a list of all albums with an Apple Script
In Photos 1 to 4 it has been simple to get a list of all albums. The short Apple Script below sufficed, because the application itself has been a container of all albums, even the albums in nested folders.
tell application "Photos"
set allalbums to the albums
set theseNames to name of albums
end tell
return (the count of theseNames) & theseNames
This seems to be different in Photos 5. To access albums in subfolders, we have to ask the containing folder for the albums. We have to traverse the tree of nested folders recursively and collect all albums in the nested folders. I hope, it is just a temporary bug, that will be fixed soon and not a new feature.
The script below will search all folders for albums and return a list of all album names. Once the folders have been scanned, it will ask for the name of a text file to save a list of all albums and all folders. The textfile will be opened in TextEdit. Most of the work is done after opening the file. For a large library it may take a few minutes to write the result.
-- list albums in Photos version 1.01
global allalbums -- the list of all albums
global allfolders -- the list of all folders
on sortList(theList)
set theIndexList to {}
set theSortedList to {}
repeat (length of theList) times
set theLowItem to ""
repeat with a from 1 to (length of theList)
if a is not in theIndexList then
set theCurrentItem to item a of theList as text
if theLowItem is "" then
set theLowItem to theCurrentItem
set theLowItemIndex to a
else if theCurrentItem comes before theLowItem then
set theLowItem to theCurrentItem
set theLowItemIndex to a
end if
end if
end repeat
set end of theSortedList to theLowItem
set end of theIndexList to theLowItemIndex
end repeat
return theSortedList
end sortList
set allfolders to {}
tell application "Photos"
set allalbums to the albums -- collect all albums, get the top level albums
set level to 0 -- nesting level of folders
set nextlevelFolders to the folders -- get the top level folders
set currentLevelFolders to {}
repeat while (nextlevelFolders is not {})
set currentLevelFolders to nextlevelFolders
set nextlevelFolders to {}
repeat with fi in currentLevelFolders
tell fi
set ffolders to its folders
set falbums to its albums
set nextlevelFolders to ffolders & nextlevelFolders
set allalbums to falbums & allalbums
end tell
end repeat
set allfolders to currentLevelFolders & allfolders
set level to level + 1
end repeat
end tell
global outputFile
global nl
set nl to "
"
set allfoldernames to {} -- a list of all folder names
set allalbumnames to {} -- a list of all album names
set outputFile to choose file name with prompt "Output File name"
-- set the inf to ""
try
open for access outputFile with write permission
write ({"Number of albums:", the (count of allalbums)} as string) to outputFile
write nl to outputFile
write (" list of albums: ") to outputFile
write (nl) to outputFile
repeat with a in allalbums
tell a
set allalbumnames to {its name} & allalbumnames as list
write (((its name) as string) & nl) to outputFile
end tell
end repeat
write nl to outputFile
end try
try
write nl to outputFile
write ({"Number of folders:", the count of allfolders} as string) to outputFile
write nl to outputFile
write (" list of folders ") to outputFile
write (nl) to outputFile
write nl to outputFile
repeat with f in allfolders
tell f
set allfoldernames to {its name} & allfoldernames as list
write (((its name) as string) & nl) to outputFile
end tell
end repeat
write nl to outputFile
write nl to outputFile
end try
close access outputFile
tell application "TextEdit"
activate
open outputFile
end tell
-- set allalbumnames to sortList(allalbumnames)
--set the clipboard to allalbumnames
return ({"Number of albums:", the (count of allalbums), allalbumnames})
The global variable allfolders will be bound to the list of all folders, the global variable allalbums will be bound to all albums for further use. Uncomment the line "set allalbumnames to sortList(allalbumnames)", if you want the list to be sorted alphabetically. It will not change the sorting of the textfile, however.