Photos for Mac: An example for an Apple Script to Search for Keywords and Add Keywords to Selected Photos

by: 
Last modified: Aug 12, 2020 8:17 AM
2 484 Last modified Aug 12, 2020 8:17 AM

Usually the graphical user interface in Photos will suffice to search for photos with certain keywords and to add keywords. If possible, we should use smart albums or the filters (or ask Siri). But for more komplex tasks we may want to search for keywords in combination with other criteria, and then we can use an Apple script to get the keywords of a photo and add other keywords.


Here is a simple example, written for Photos 5 on Catalina: The script will search for the keyword specified by the variable "keyword_to_find" and add the keyword specified by "keyword_to_add".

The photos have to be selected in a top level album or "AllPhotos".

-- Find all photos woth a certain keyword and add another keyword
tell application "Photos"
	
	set imageSel to (get selection)
	set keyword_to_find to "Bird" -- change this to the keyword you want
	set keyword_to_add to "Animal" -- change this ..
	set kws to {}
	repeat with im in imageSel
		
		tell im
			-- retrieve the keywords
			set kws to its keywords as list
			if (exists kws) and (kws is not {}) and kws contains keyword_to_find and kws does not contain keyword_to_add then
				set kws to {keyword_to_add} & kws -- append to the list of keywords
				set its keywords to kws -- replace the keywords by the new list
			end if
		end tell
	end repeat
	return kws
end tell


First published here in the German forum: https://communities.apple.com/de/thread/251680924?answerId=253232596622#253232596622

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