Skip to content

Instantly share code, notes, and snippets.

@joerick
Created July 22, 2023 09:09
Show Gist options
  • Save joerick/a68ecc083e4dbcedb8737f6a5b524ef5 to your computer and use it in GitHub Desktop.
Save joerick/a68ecc083e4dbcedb8737f6a5b524ef5 to your computer and use it in GitHub Desktop.
Fix Photos.app dates Applescript

Fix Photos.app dates

I recently had a situation where I needed to fix the dates on some photos imported from Lightroom into Photos.app. For some reason, Photos.app had chosen the file creation date (which was the Lightroom export date), rather than the capture date stored in embedded exif data.

This script works after importing the photos, if you've already done some organising.

Usage

Put the photos you want to adjust into an album called "DateFix".

You'll need exiftool installed.

brew install exiftool

Make a scratch dir as per the first line of the script.

Then run the script. The photos will be adjusted to match the date in the EXIF data.

-- create a folder for use by the script and set the path to it here
set thepath to alias "Macintosh HD:Users:joerick:Desktop:Scratch" as text
tell application "Photos"
set photos to every media item of container "DateFix"
repeat with mediaitem in photos
tell application "Finder" to delete every item of folder thepath
export {mediaitem} to thepath with using originals
tell application "Finder" to set theExport to item 1 of folder thepath
set theCommand to "/opt/homebrew/bin/exiftool -S -DateTimeOriginal " & (quoted form of POSIX path of (theExport as alias))
set exifOutput to do shell script theCommand
set resultDate to the current date
-- set theyear to (text 19 thru 22 of exifOutput)
-- set themonth to (text 24 thru 25 of exifOutput)
-- set theday to (text 27 thru 28 of exifOutput)
-- set thehours to (text 30 thru 31 of exifOutput)
-- set theminutes to (text 33 thru 34 of exifOutput)
-- set theseconds to (text 36 thru 36 of exifOutput)
--
-- return theyear & themonth & theday & thehours & theminutes & theseconds
set the year of resultDate to (text 19 thru 22 of exifOutput)
set the month of resultDate to (text 24 thru 25 of exifOutput)
set the day of resultDate to (text 27 thru 28 of exifOutput)
set the hours of resultDate to (text 30 thru 31 of exifOutput)
set the minutes of resultDate to (text 33 thru 34 of exifOutput)
set the seconds of resultDate to (text 36 thru 36 of exifOutput)
set the date of mediaitem to resultDate
end repeat
end tell
@mattvick
Copy link

mattvick commented Sep 17, 2024

Many thanks @joerick for this great script!

When running I had a bug with thepath, which I've fixed below. Also, text 36 thru 36 should be text 36 thru 37, and I've added a check for the EXIF output as I had some GIFs that did not have EXIF data.

-- create a folder for use by the script and set the path to it here
set thepath to alias "Macintosh HD:Users:joerick:Desktop:Scratch" as text

tell application "Photos"
	set photos to every media item of container "DateFix"
	repeat with mediaitem in photos
		tell application "Finder" to delete every item of folder thepath
		-- using `(thepath as alias)` fix:
		export {mediaitem} to (thepath as alias) with using originals
		tell application "Finder" to set theExport to item 1 of folder thepath
		
		set theCommand to "/opt/homebrew/bin/exiftool -S -DateTimeOriginal " & (quoted form of POSIX path of (theExport as alias))

		set exifOutput to do shell script theCommand

		if exifOutput is not "" then
			set resultDate to the current date
			
			
			-- --		set theyear to (text 19 thru 22 of exifOutput)
			-- --		set themonth to (text 24 thru 25 of exifOutput)
			-- --		set theday to (text 27 thru 28 of exifOutput)
			-- --		set thehours to (text 30 thru 31 of exifOutput)
			-- --		set theminutes to (text 33 thru 34 of exifOutput)
			-- --		set theseconds to (text 36 thru 36 of exifOutput)
			-- --		
			-- --		return theyear & themonth & theday & thehours & theminutes & theseconds
			
			set the year of resultDate to (text 19 thru 22 of exifOutput)
			set the month of resultDate to (text 24 thru 25 of exifOutput)
			set the day of resultDate to (text 27 thru 28 of exifOutput)
			set the hours of resultDate to (text 30 thru 31 of exifOutput)
			set the minutes of resultDate to (text 33 thru 34 of exifOutput)
			set the seconds of resultDate to (text 36 thru 37 of exifOutput)
			
			set the date of mediaitem to resultDate
		end if
		
	end repeat
end tell

@joerick
Copy link
Author

joerick commented Sep 17, 2024

Nice, thanks! Pleased it's helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment