Skip to content

Instantly share code, notes, and snippets.

@thedaviddias
Forked from redoPop/README.md
Created December 7, 2016 01:55
Show Gist options
  • Save thedaviddias/092528168a559cb5170ba135f15676d1 to your computer and use it in GitHub Desktop.
Save thedaviddias/092528168a559cb5170ba135f15676d1 to your computer and use it in GitHub Desktop.
Hazel: "photo taken" custom date attribute

At the time of writing, Hazel's default date attributes all refer to an image file's creation date rather than the date on which the photo was originally taken. The two dates may differ.

This script provides a custom date attribute reflecting the time the photo was actually taken. As written, it's intended to be added as an embedded script in a "Run JavaScript" rule action, so that it's custom attribute can be used in subsequent "Sort into subfolder" patterns.

The date this script exposes is obtained via sips -g creation [filename]. It's not clear to me exactly which EXIF attribute the sips "creation" property comes from, but it seems reasonable to assume it's either DateTimeOriginal or DateTimeDigitized.

var app = Application.currentApplication();
app.includeStandardAdditions = true;
var theDate = app.doShellScript(
'sips -g creation ' +
// Dragons below: Q&D shell escape:
('' + theFile).replace(/ /g, '\\ ')
)
// Parse SIPS output & extract date
.match(/(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})/)
.slice(1)
// Q&D numberification of stringy dates
.map(function (i) { return +i });
// Offset the month by 1 because JavaScript
theDate[1] -= 1;
return {
hazelOutputAttributes: [
// Apply array of values to Date constructor
new (Function.prototype.bind.apply(
Date, [null].concat(theDate)
))
]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment