Create a gist now

Instantly share code, notes, and snippets.

@redoPop /README.md
Last active Jan 3, 2018

Embed
What would you like to do?
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)
))
]
};
@redoPop

This comment has been minimized.

Show comment
Hide comment
@redoPop

redoPop Jun 10, 2015

Here are a couple of screenshots showing how I use this custom data attribute JS in Hazel:

After which I can refer to the pattern when sorting:

There are three "Sort into subfolder" steps because I sort my photos into a Y/M/D path structure. For example, here's the first sort action:

This was mostly an experimental foray into JXA and Hazel custom attributes, and to be honest the whole thing feels pretty awful and clunky to me. I plan to replace it with a more maintainable shell script that does the whole job of reading the photo date from sips, creating new folders based upon it, and moving the file into place.

Owner

redoPop commented Jun 10, 2015

Here are a couple of screenshots showing how I use this custom data attribute JS in Hazel:

After which I can refer to the pattern when sorting:

There are three "Sort into subfolder" steps because I sort my photos into a Y/M/D path structure. For example, here's the first sort action:

This was mostly an experimental foray into JXA and Hazel custom attributes, and to be honest the whole thing feels pretty awful and clunky to me. I plan to replace it with a more maintainable shell script that does the whole job of reading the photo date from sips, creating new folders based upon it, and moving the file into place.

@redoPop

This comment has been minimized.

Show comment
Hide comment
@redoPop

redoPop Jun 12, 2015

If anyone else ends up down this same path, eventually I replaced all of the above with a simple embedded shell script using exiftool:

exiftool "-Filename<DateTimeOriginal" -d "~/Pictures/%Y/%Y-%m/%Y-%m-%d_%H-%M-%S%%-c.%%e" "$1"

More information about its file and directory naming features can be found here.

Owner

redoPop commented Jun 12, 2015

If anyone else ends up down this same path, eventually I replaced all of the above with a simple embedded shell script using exiftool:

exiftool "-Filename<DateTimeOriginal" -d "~/Pictures/%Y/%Y-%m/%Y-%m-%d_%H-%M-%S%%-c.%%e" "$1"

More information about its file and directory naming features can be found here.

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