Script: Add the Album Name as a keyword to selected Photos 2019

by: 
Last modified: Mar 10, 2019 5:56 AM
5 983 Last modified Mar 10, 2019 5:56 AM

For Photos 4.0 on Mojave. This is a follow up to 4711's script: How to add Album name to keywords - Apple Community


To use this script, select the photos you want to tag with the album name as a keyword, then run this script.

It will be awfully slow, if you have many smart albums, because it takes a very long time to check all photos against the smart albums.



tell application "Photos"
	activate
	try
		set sel to selection
		if sel is {} then error "The selection  is empty, please select some photos" -- no selection 
		
	on error errTexttwo number errNumtwo
		display dialog "No photos selected " & errNumtwo & return & errTexttwo
		return
	end try
	
	-- repeat for all selected images
	repeat with target in sel -- target:  the image to seach for
		
		try
			set thisId to id of target
		on error errText number errNum
			display dialog "Error: cannot get the image ID" & errNum & return & errText & "Trying again"
			
			try
				delay 2
				set thisId to id of target
			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 {} -- a list of the album names
		try
			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 2
				set theseNames to name of (albums whose id of media items contains thisId)
			on error errTexttwo number errNumtwo
				display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo
				error "giving up"
				return
			end try
		end try
		
		-- adding albums names to keywords, only for photos found in albums
		
		if (theseNames is not {}) and (exists (the keywords of target)) then -- image is in albums
			set (the keywords of target) to (the keywords of target) & theseNames
		else if (theseNames is not {}) and not (exists (the keywords of target)) then
			set (the keywords of target) to {} & (the keywords of target)
		end if
	end repeat
	
end tell


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