ExifTool Script for use in Hazel

Started by NJPhillips, December 01, 2014, 09:27:49 PM

Previous topic - Next topic

NJPhillips

Greetings,

I have searched high and low, and have only found circular pointers to about four separate threads on the net discussing ExifTool scripts to use in the Hazel application for mac. As frustrating as it may seem, OS X Finder does not read EXIF information from photos. Since the popular Hazel automation utility utilizes the OS X Finder, the best it can do to gather information about a photo is to read the date created/date modified...as identified by Finder. A popular feature request on the Hazel forums is to rename photos in accordance with some kind of exif information. Of the hundreds of posts requesting this, only three or so talk about ExifTools...and only one of those gives a sample script. For most users, this type of scripting language is very difficult to understand, and I think they shy away from using it. What I would like to do, is create a posting of sample scripts for use in Hazel that can help users rename their OS X photos based on a different set of sample criteria... Why not post this on the Hazel forums? Well, because the ExifTool creator/master lives here... :-)

I am humbly requesting assistance in writing some sample scripts to use in Hazel that can help do some of the following types of renaming:

YYYY-MM-DD.jpg
YY-MMM-DD.jpg
MM-DD.jpg
MMM-dd.jpg

etc...

For those that might not know what Hazel is... it is a automation tool that can monitor folders/files and take actions based on set criteria. In this example, every time photos are put into a folder, Hazel can automatically rename them, and then sort them into a hierarchy of folders... very nice to have, but getting the metadata is not so easy.

Here is a sample script from the Hazel forum that uses ExifTool to rename photos...can anybody help me understand what this script is doing? Maybe once I get that, I can start getting on with the rest of my request.

exiftool -P -d '%Y%m%d' \
   '-filename<${FileModifyDate;}.%e' \
        '-filename<${GPSDateTime;}.%e' \
        '-filename<${MediaCreateDate;}.%e' \
        '-filename<${ModifyDate;}.%e' \
        '-filename<${DateTimeOriginal;}.%e' \
        "$1"


StarGeek

#1
Quote from: NJPhillips on December 01, 2014, 09:27:49 PM
Here is a sample script from the Hazel forum that uses ExifTool to rename photos...can anybody help me understand what this script is doing? Maybe once I get that, I can start getting on with the rest of my request.

exiftool -P -d '%Y%m%d' \
   '-filename<${FileModifyDate;}.%e' \
        '-filename<${GPSDateTime;}.%e' \
        '-filename<${MediaCreateDate;}.%e' \
        '-filename<${ModifyDate;}.%e' \
        '-filename<${DateTimeOriginal;}.%e' \
        "$1"

To break it down:
-P  Preserve the filesystem modification date/time of the original file.

-d '%Y%m%d' (this link is a bit off and links to the -D option, capital D, not small d, you'll have to scroll up just a bit to get the correct item) Set the format for date/time tag values.  All the following tags try to set the Filename to a Date/Time value and this option changes the format of how the Date/Time will appear.  In this case, the file name will be set to a four digit year, two digit month number, and a two digit day number.  In other words, in this case it would be something like 20141202.  You can find the codes you can use here.

Now, the rest is just looking for what date/time info to use and apply that to the filename.  It is processed from left to right (or top down in this case), using whichever tag is found last.  In this case, it will first want to use the FileModifyDate tag.  Then it will use the GPSDateTime tag if that exists, followed by the MediaCreateDate tag.  So in this case, the DateTimeOriginal tag, if it exists, will override all the previous tags and it will be used for the name.  This is basically someone trying very hard to make sure that the file gets renamed to a date.

The semicolon at the end of the tag, by itself like that with no perl command, makes the tag filename safe, removing things like colons, slashes, and backslashes.

Finally, the .%e reapplies the extension to the file.

Make sure and read over the docs, the FAQ and the doc on filename tags.

Edit: Fixed link
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

NJPhillips

This is great, Thanks!!

Can I assume then, the following also to be true:

For:

YYYY-MM-DD use:  exiftool -P -d '%Y-%m-%d' \

YY-MM-DD use:     exiftool -P -d '%y-%m-%d' \

Thanks!

StarGeek

"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

NJPhillips

OK, this is good stuff!! OK, the last portion here then is to do the sorting... Since the only thing Hazel can read is the File Created Date and Modified Date (through finder), how do I change either one of those to the same format we have discussed above? Can that be done in one go?

I tried removing the -P (assuming the Preserve meant DO NOT ALTER modified date), but there was no change to the modified date in finder. Am I missing something, or this just to preserve a tag within the EXIF data?

Phil Harvey

The -P applies only if the file was modified.  Changing the FileName doesn't actually modify the file's data, so -P has no effect.

You can write FileModifyDate and FileCreateDate just like you did FileName.  They are very flexible about the input format.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

NJPhillips

Because I am changing a date, do I need to set a specific format? Will this work as intended?

exiftool -d '%Y-%m-%d %H%M%S' \
    '-FileModifyDate<${DateTimeOriginal;}.%e' \
   

Phil Harvey

You don't need to reformat the date with -d.  When writing a date, the date format is already correct.
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

exif-newb

Quote from: NJPhillips on December 03, 2014, 08:40:21 PM
Because I am changing a date, do I need to set a specific format? Will this work as intended?

exiftool -d '%Y-%m-%d %H%M%S' \
    '-FileModifyDate<${DateTimeOriginal;}.%e' \


NJPhillips - I have tried sorting image files with Hazel. However, I seem to somehow have files where the exif date is in 2013, and file created date is 2015. Not all files are in that category - strangely. I use a Mac.

So for the reasons stated above, not that you need selling for why exiftool should be used, can I request you to please share the solution that you came up with? With Hazel rule and how you may have embedded the Exiftool script in the Hazel rule for time stamping.

Thanks!