Script: Find all Photos Taken at a Certain Hour

by: 
Last modified: Feb 7, 2022 9:33 PM
3 5269 Last modified Feb 7, 2022 9:33 PM

Sometimes we want to find all photos taken at a certain time of the day - early morning, midday, or similar. The following script will ask for a given hour (0 to 23), then take all selected photos and create a new album of all photos within the selection that have been taken in the interval of this hour plus 1.


The photos have to be selected in All Photos or a top level album, not in a smart album.


To use the script, copy the text below into a new Script Editor window and save it.

Select the photos in Photos, then click the run button ▶︎in Script Editor.

Enter the hour to search for as an integer number between 0 and 23.

The result will appear in a new top level album name like the hour in the Photos sidebar.


------ copy from here ------------

-- Find all photos taken at a certain hour, for example the hour of the current date.
-- The resulting photos will be added to a toplevel album named "On <Hour> ".
-- Author Léonie, Feb 21,2021



set thishour to 0 -- change this to the hour you want, for example 12
-- Ask for the start hour
set userCanceled to false

try
	set dialogResult to display dialog ¬
		"Enter the hour to search for" buttons {"Cancel", "OK"} ¬
		default button "OK" cancel button ¬
		"Cancel" giving up after 15 ¬
		default answer (thishour as text)
on error number -128
	set userCanceled to true
	return "user canceled"
end try
set thishour to (text returned of dialogResult) as number
set thisTimeMin to hours * (thishour - 1) -- change thise lines for a differnet time range
set thisTimeMax to hours * (thishour + 1)


set thisHourAlbum to "taken at " & thishour & "hour"
-- return thisHour -- for testing

tell application "Photos"
	try
		
		if exists container thisHourAlbum then
			delete the container thisHourAlbum
			delay 2 -- second try after delay
		end if
		make album named thisHourAlbum -- create a new, empty album
	on error errTexttwo number errNumtwo
		display dialog "Cannot create  album: " & thisHourAlbum & " " & errNumtwo & return & errTexttwo
		return
	end try
	
	set onthisHourFound to {}
	set onthisHourSkipped to {}
	
	
	set allTheItems to (get selection) -- get all selected photos
	--every media item -- get a list of all photos and videos, may take a while
	-- add all photos taken at this hour 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 time_i to the time of (date_i as date)
			if ((time_i ≥ thisTimeMin) and (time_i ≤ thisTimeMax)) then
				set end of onthisHourFound to thisMediaItem
				add {thisMediaItem} to container thisHourAlbum
				spotlight thisMediaItem
				
			end if
		on error errTexttwo number errNumtwo
			set end of onthisHourSkipped to thisMediaItem
		end try
	end repeat
end tell

set theresultmessage to "Found " & (the count of onthisHourFound) & " items and added them to the album " & thisHourAlbum & ".
  Skipped " & (the count of onthisHourSkipped) & " 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 ID.