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.

How to find out the albums a photo is in: Catalina Version of a Script by Jacques Rioux

by: 
Last modified: Jun 19, 2020 5:01 AM
12 4408 Last modified Jun 19, 2020 5:01 AM

See this version for Photos 4 or older: How to find out the albums a photo is in:… - Apple Community


Photo for Mac can show you the "moment" a photo is in, but has no tools to find the albums, that contain the photo.

User Jacques Rioux posted an Apple Script, that can list all albums that contain a selected photo. See this link:


Re: Re: How can I identify which albums photos are assigned to in Photos.


Here is a version of Jacques script with a few error checks added by me, also the script is traversing the nested folders for all albums. Otherwise the script would only find top level albums in Photos 5.0. This recursive search through nested folders is making the script very, very slow. Be prepared to wait for several minutes, if your library is large.


-- Jacques Rioux's script  https://discussions.apple.com/message/29601534#29601534
-- modified by leonie for Catalina
--  version 1.01, changed a dialog to a notification

-- Select the photo in Photos, then run this script 
-- by pressing the "Run" button in the script editor 
-- or run it from the scripts menu.
-- The script will show first a panel with the filename of the photo it is searching for.
-- Then it will show a second panel with the list of albums. 
-- if you do not see the panels, click the script Editor icon in the Dock, if it is bouncing.

global allalbums -- the list of all albums
global allfolders -- the list of all folders


--part 1. get the selected photos
tell application "Photos"
	activate
	-- Add the photo you want to search for to a top level album as the first item in the album
	
	set resultcaption to "Searching for: "
	try
		
		set sel to selection
		if sel is {} then error "The selection  is empty" -- no selection 
		
	on error errTexttwo number errNumtwo
		display dialog "No photos selected " & errNumtwo & return & errTexttwo
		return
	end try
	
	set imagename to "unknown filename"
	try
		set target to item 1 of sel -- the image to seach for
		tell target
			set imagename to the filename of target
		end tell
	on error errTexttwo number errNumtwo
		display dialog "Cannot get the filename of the first image: " & errNumtwo & return & errTexttwo
	end try
	set resultcaption to (resultcaption & imagename)
end tell


display notification resultcaption subtitle imagename

-- Part 2: get a list of all albums by searching the nested folders
set allfolders to {}
set allalbums to {}


tell application "Photos"
	
	set allalbums to the albums --  collect all albums
	set allfoldernames to {}
	set allalbumnames to {}
	
	
	set level to 0 -- nesting level of folders
	
	set nextlevelFolders to the 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
	-- return allalbums --test
end tell

-- From Jacques Rioux's script:
tell application "Photos"
	if sel is {} then return -- no selection 
	try
		set thisId to id of item 1 of sel
	on error errText number errNum
		display dialog "Error: cannot get the image ID" & errNum & return & errText & "Trying again"
		
		try
			delay 1
			set thisId to id of item 1 of sel
		on error errTexttwo number errNumtwo
			display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo
			error "giving up"
			return
		end try --second attempt
	end try
	
	
	set theseNames to {}
	repeat with a in allalbums
		try
			tell a
				if ((the id of media items) contains thisId) then
					set theseNames to {the name of a} & theseNames
				end if
			end tell
			
			--set theseNames to name of (albums whose id of media items contains thisId)
		on error errText number errNum
			display dialog "Error: cannot get the albums" & errNum & return & errText & "Trying again"
			
			try
				delay 1
				tell a
					if ((the id of media items) contains thisId) then
						set theseNames to {the name of a} & theseNames
					end if
				end tell
				
			on error errTexttwo number errNumtwo
				display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo
				error "giving up"
				return
			end try
		end try
	end repeat
	
end tell

if theseNames is not {} then
	set {oTid, text item delimiters} to {text item delimiters, return}
	set {t, text item delimiters} to {theseNames as string, oTid}
	-- return oTid
else
	set t to "No album"
end if
activate

set resultcaption to resultcaption & ", found it in these albums:
" & t as string
set the clipboard to resultcaption
display notification resultcaption subtitle "Saved to the Clipboard"

display dialog resultcaption & ", Saved to the Clipboard" buttons {"OK"} default button "OK"
-- you can press the Enter key or the return Key to close the dialog
return resultcaption -- léonie



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



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