Testing if Exif DateTime Original is not empty

Started by deathrobot, February 08, 2015, 07:57:42 PM

Previous topic - Next topic

deathrobot

Hello. I'm working on an automatic photo management system and would like to run a shell script in Hazel to test if a photo contains any data in the Exif Date Time Original field. If it does, I'll run DIM to organize, but if it doesn't I just want to tag the file and move it to another folder for manual sorting. Having trouble figuring out what the shell script would be. This seems to be true whether the file has exif date created or not:

exiftool -if $datetimeoriginal $1

Thanks for any help with this. I know nothing about PERL, but am frustrated that Spotlight will only use the file Date Created info instead of extracting Exif info and would really like to get this to work. Any help is much appreciated!

Phil Harvey

You need to protect the first "$" against shell interpolation by quoting $datetimeoriginal:

exiftool -if '$datetimeoriginal' $1

This will return 0 if DateTimeOriginal exists, or 1 otherwise.  You might want to suppress other output by adding a dummy tag name:

exiftool -if '$datetimeoriginal' -dummy $1

- 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 ($).


deathrobot

Turns out this fails in the situation where there is a datetimeoriginal tag that is empty, as opposed to the tag not being there at all (in which case it works). Is there a shell script that could test for both situations, i.e.:

exiftool -if '$datetimeoriginal' -dummy "$1"
     or
-if [datetimeoriginal is empty] -dummy "$1"

Thanks,
Michael

Phil Harvey

It sounds like DateTimeOriginal is probably filled with spaces or something like that.  You can test to see if it contains any digit with this:

exiftool -if '$datetimeoriginal =~ /\d/' ...

- 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 ($).

deathrobot

Thanks, Phil. After quite a bit of looking through the forum posts, I was able to use this:

exiftool -if 'not defined $datetimeoriginal or $datetimeoriginal =~ /(^\s*$)/' "$1"

Is your version of the part of this statement that comes after the "or" more appropriate?

Phil Harvey

On Mac/Linux a condition should fail if the tag isn't defined.  (But for some reason I don't quite understand this isn't true in Windows, so here you must test for not defined.)

Your expression (after "or") returns true if DateTimeOriginal is empty or contains all spaces.  This will work fine.  (Mine was the opposite logical sense, so you would have to use $datetimeoriginal !~ /\d/ to have a similar effect.)

- 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 ($).