Mac Script to Update Photos Dates to match EXIF

Because of various excavations of photos from platform to platform to platform (Flickr, OVI, Google Photos, iPhoto) over the years, now that I’ve settled on Apple’s ecosystem to maintain my photos, I’ve ended up with a bunch of photos that have the wrong date.

By “have the wrong date,” I mean, for example, “Photos thinks they’re from May 2020 when, in fact, I took them in March 2013.”

The thing is, the right date is burned into the EXIF data in the photo itself, so I needed a way to say to all the affected photos “change your date to match what’s in the EXIF.”

I wasn’t up to mystically incanting this in AppleScript, but I figured that I could give JavaScript a try, now that JavaScript is an equal scripting partner under macOS.

So here’s a try at a JavaScript that does just that:

var Photos = Application("Photos");
var path = Path("/Users/peter/Desktop/photos");

for (var photo of Photos.selection()) {
	var filename = photo.filename();
	Photos.export([photo], {to: path, usingOriginals: true});
	var tmpfile = Path("/Users/peter/Desktop/photos/" + filename);
	app = Application.currentApplication();
	app.includeStandardAdditions = true;
	var newDate = app.doShellScript("/usr/local/bin/exiftool -T -DateTimeOriginal '" + tmpfile + "'");
	if (newDate != '-') {
		var d = new Date(newDate);
		photo.date = d;
	}
}

With that script in Script Editor, I can select one or more photos in Photos, run the script, and have those photos with an EXIF date get their date updated.

You’ll notice that there’s a kludgey export of the original photo to a working directory; that serves two purposes:

  1. I couldn’t for the life of me figure out how to identify the original path for a photo in Photos, so as to run exiftool against it; this saves me the trouble.
  2. I end up with a working directory filled with the original photos should something go horribly wrong with the script.

To get this to work you’ll need to install exiftool, and adjust the path to it in the script as necessary.

Your mileage with the script may vary, but it worked for my purposes, and that glut of May 2020 photos are now safely back in March 2013.

Comments

Claus's picture
Claus on November 17, 2020 - 16:54 Permalink

Can you program JavaScript in Script Editor?
Is there documentation for this?

I tried [something similar the other day] (http://www.planetwater.org/2020/10/04/yay-to-automation-on-the-mac/) -- and was shocked that I was not able to prescribe the filename of a photo exported from Photos in AppleScript, which your script might be able to do (except that you seem to be choosing its filename from Photos as its filename)

Peter Rukavina's picture
Peter Rukavina on November 17, 2020 - 17:06 Permalink

Yes, you can code in JavaScript in Script Editor! Just select the language from the drop-down menu at the top of the editor:

Selecting JavaScript in Script Editor