Script: Find all items in Photos for a given day and month

by: 
Last modified: Feb 16, 2021 3:31 AM
2 3910 Last modified Feb 16, 2021 3:31 AM

Photos on Big Sur does no longer seem to be able to find all photos for a given day and month across all years. The search for a day number and a month seems to default to the current year, not all days across all years past

Here is a quick and dirty Apple Script. It will ask for the number of the month and the number of day, then search the currently open Photos Library for all photos with this combination of month and day.

The items found will be returned in a new album, named like the date. The script is rather slow. For my test library with 50000 photos it will run for roughly five minutes, when running on macOS 11.2, Big Sur

There is an older version, more versatile version of this script here - written for Mojave or earlier, but the formatting has been broken by the update of the forum software - several blank characters vanished: old, broken version: Script: How to search for photos from yearly recurring events


---------- copy the script below into the Script Editor and save it ----

-- Find all photos taken on a certain day, for example the day of the current date.
-- The resulting photos will be added to a toplevel album named "On <day> <month>".
-- Author Léonie, March 8,2016



set thismonth to 1 -- change this to the month you want, for example 12
set thisday to 6 -- change this to the day you want, for example "25"

-- Ask for the month
set userCanceled to false
try
	set dialogResult to display dialog ¬
		"Enter the number of the month" buttons {"Cancel", "OK"} ¬
		default button "OK" cancel button ¬
		"Cancel" giving up after 15 ¬
		default answer (thismonth as text)
on error number -128
	set userCanceled to true
	return "user canceled"
end try
set thismonth to (text returned of dialogResult) as number

set thismonthname to item thismonth of {January, February, March, April, May, June, July, August, September, October, November, December}

-- Ask for the day
set userCanceled to false
try
	set dialogResult to display dialog ¬
		"Enter the number of the day " buttons {"Cancel", "OK"} ¬
		default button "OK" cancel button ¬
		"Cancel" giving up after 15 ¬
		default answer (thisday as text)
on error number -128
	set userCanceled to true
	return "user canceled"
end try
set thisday to (text returned of dialogResult) as number

set thisDayAlbum to "On " & thismonthname & " " & thisday as string

-- return {thisday, thismonth} -- for testing

tell application "Photos"
	try
		
		if exists container thisDayAlbum then
			delete the container thisDayAlbum
			delay 2 -- second try after delay
		end if
		make album named thisDayAlbum -- create a new, empty album
	on error errTexttwo number errNumtwo
		display dialog "Cannot create  album: " & thisDayAlbum & " " & errNumtwo & return & errTexttwo
		return
	end try
	
	set onthisdayFound to {}
	set onthisdaySkipped to {}
	
	
	set allTheItems to every media item -- get a list of all photos and videos, may take a while
	-- add all photos taken this day to the album
	
	repeat with i from 1 to the count of allTheItems
		-- check the dates of all photos and videos
		set thisMediaItem to item i of allTheItems
		-- spotlight thisMediaItem
		try
			set date_i to the date of thisMediaItem
			
			set day_i to the day of (date_i as date)
			set month_i to (the month of (date_i as date)) as number
			-- return {day_i, month_i, thisday, thismonth}
			if ((day_i = thisday) and (month_i = thismonth)) then
				set end of onthisdayFound to thisMediaItem
				add {thisMediaItem} to container thisDayAlbum
				spotlight thisMediaItem
				
			end if
		on error errTexttwo number errNumtwo
			set end of onthisdaySkipped to thisMediaItem
		end try
	end repeat
end tell

set theresultmessage to "Found " & (the count of onthisdayFound) & " items and added them to the album " & thisDayAlbum & ".
  Skipped " & (the count of onthisdaySkipped) & " items without date"

display dialog theresultmessage


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