Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Script to automate setting dates in Photos from the file name

I recently imported a bunch of videos in the Photos for Mac app that had been previously imported from my MiniDV and AVCHD video cameras using iMovie over ten years ago. I just don't use iMovie enough to warrant keeping those videos walled off in a separate app so I decided to bring them into Photos.


Of course, importing these files set the date/time to the date and time they were imported. All of the imported videos had filenames that incorporated the actual date and time from their original metadata but apparently when imported into iMovie, that metadata wasn't preserved in a way the Photos could import.


Now, I could go through the 500+ videos and manually use the Adjust Date and Time… command but that seemed silly on a modern computer.


So, AppleScript and Github to the rescue. I'm not an AppleScript expert but I've used a number of other languages and I found some sample scripts on Github that had some pieces of what I was trying to do. So I cobbled together a script and am sharing it here in case others might find it useful.


Specifically, this script will go through a selection of items in Photos for Mac and try to set their date and times to match the date and time that in in the filename.


This seems to only be useful for videos imported through iMovie (maybe Final Cut, not sure) which use a specific file name format of "clip-YYYY-MM-DD HH;MM;SS.ext" If your videos don't match this format, then you'll have to modify this script or find a different one. It performs almost no validation on the compatibility of the file name to this format and I didn't bake in any error handling so, USE IT AT YOUR OWN RISK.


Anyway, here's the script:


Posted on Sep 3, 2020 9:20 AM

Reply
Question marked as Best reply

Posted on Sep 3, 2020 9:21 AM

And just to increase the visibility of the script:


tell application "Photos"
	activate
	set imageSelection to (get selection)
	
	--cycle through selection
	repeat with i from 1 to count of imageSelection
		--get file name at each iteration
		set imageCurrent to item i of imageSelection
		set imageName to filename of imageCurrent
		
		--parse the file name into a datetime value
		-- clip-2018-09-28 08;10;36.mov
		
		if imageName begins with "clip-" then
			--grab the date string from the file name - since it's a fixed length, it's easy to grab
			set dateText to characters 6 thru 24 of imageName as string
			
			--manually parse the date string into a new date item
			set newDate to the current date
			set the month of newDate to (1 as integer)
			set the day of newDate to (1 as integer)
			set the year of newDate to (text 1 thru 4 of dateText)
			set the month of newDate to (text 6 thru 7 of dateText)
			set the day of newDate to (text 9 thru 10 of dateText)
			set the hours of newDate to (text 12 thru 13 of dateText)
			set the minutes of newDate to (text 15 thru 16 of dateText)
			set the seconds of newDate to (text 18 thru 19 of dateText)
			
			--set the datetimestamp of the current photo to that of the parsed datetime value
			tell imageCurrent
				set the date of imageCurrent to (newDate) as date
			end tell
		end if
		
	end repeat
	
	beep
	display dialog "Adjusted the date and time of " & (the length of imageSelection) & " items."
end tell

Similar questions

2 replies
Question marked as Best reply

Sep 3, 2020 9:21 AM in response to PhillipU

And just to increase the visibility of the script:


tell application "Photos"
	activate
	set imageSelection to (get selection)
	
	--cycle through selection
	repeat with i from 1 to count of imageSelection
		--get file name at each iteration
		set imageCurrent to item i of imageSelection
		set imageName to filename of imageCurrent
		
		--parse the file name into a datetime value
		-- clip-2018-09-28 08;10;36.mov
		
		if imageName begins with "clip-" then
			--grab the date string from the file name - since it's a fixed length, it's easy to grab
			set dateText to characters 6 thru 24 of imageName as string
			
			--manually parse the date string into a new date item
			set newDate to the current date
			set the month of newDate to (1 as integer)
			set the day of newDate to (1 as integer)
			set the year of newDate to (text 1 thru 4 of dateText)
			set the month of newDate to (text 6 thru 7 of dateText)
			set the day of newDate to (text 9 thru 10 of dateText)
			set the hours of newDate to (text 12 thru 13 of dateText)
			set the minutes of newDate to (text 15 thru 16 of dateText)
			set the seconds of newDate to (text 18 thru 19 of dateText)
			
			--set the datetimestamp of the current photo to that of the parsed datetime value
			tell imageCurrent
				set the date of imageCurrent to (newDate) as date
			end tell
		end if
		
	end repeat
	
	beep
	display dialog "Adjusted the date and time of " & (the length of imageSelection) & " items."
end tell

Script to automate setting dates in Photos from the file name

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