ExifTool code works in Terminal but not in Hazel. Help Pls!

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Hello,

Can someone be kind enough to point me in the right direction, please. I am not coding literate (am an interior designer), so I am totally clueless.

After scavenging the web, I came up with the following concoction that works in the Terminal, but fails in Hazel:

Code: Select all
opentags=`echo $1 | sed 's/.*\[\([^]]*\)\].*/\1/g'`

for i in $opentags
do
    tag -a "$i" "$1"
done

assignedtags=`tag -tg $1|grep '^    '|cut -c5-|sort -u`

echo "$assignedtags" | while IFS= read -r line ; do /usr/local/bin/exiftool -overwrite_original_in_place -P -keywords+="$line" $1; done ; /usr/local/bin/exiftool -overwrite_original_in_place '-keywords<${keywords;NoDups(1)}' '-exif:xpkeywords<${iptc:keywords;NoDups(1)}' -P -d '%Y:%m:%d_%H:%M:%S' '-filename<${FileModifyDate;}_$FileName' '-filename<${GPSDateTime;}_$FileName' '-filename<${MediaCreateDate;}_$FileName' '-filename<${ModifyDate;}_$FileName' '-filename<${DateTimeOriginal;}_$FileName' "$1"


The first part works in Hazel. (I tried running it as a separate rule). Essentially, it grabs a part of the fileName[enclosed in sq. brackets] and passes it along to "tag" the file (https://github.com/jdberry/tag). As I am frequently required to also work in MS Windows, I made a habit of naming files like this: " fileName[tag1 tag2 tag3].ext "

The second part is supposed to take the file's macOS tags and write them as EXIF & IPTC tags to JPEG files. It also prepends the DateTimeOriginal to the fileName. It works on the command line but for some reason is failing in Hazel.

Please guide me.

Thanks so much, & to those reading this post, a Happy New Year!
Harshal
hshinde
 
Posts: 4
Joined: Sun Dec 29, 2019 11:20 am

Where did you get the script from? Also, can you go through the troubleshooting guide here? https://www.noodlesoft.com/kb/hazel-tro ... ing-guide/
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

hshinde wrote:It works on the command line but for some reason is failing in Hazel.


That almost certainly means that there is a tool being used which is not in the default PATH for macOS.

Unfortunately macOS no longer makes it easy to set the default PATH for GUI apps, so you have to do it in each script.

Try adding this as the first line of your shell script in Hazel:

Code: Select all
PATH=/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
luomat
 
Posts: 78
Joined: Wed Mar 10, 2010 3:57 pm

Hello,
Mr_Noodle wrote:Where did you get the script from?

I referred to some 5-6 different threads on stackoverflow.com & the forums here, and put it together to do what I want. Like I said, its a concoction :)

Mr_Noodle wrote:Also, can you go through the troubleshooting guide here? https://www.noodlesoft.com/kb/hazel-tro ... ing-guide/

I did go through the trouble shooting guide as recommended. The rules setup and other things seem alright. Here's how it looks: https://postimg.cc/8fPBJ5Gf

The first part works just fine. Specifically:
Code: Select all
theFile=$1
assignedtags=`echo $theFile | sed 's/.*\[\([^]]*\)\].*/\1/g'`
for i in $assignedtags
do
    tag -a "$i" "$theFile"
done


But when I add the succeeding ExifTool part, it doesn't seem to work. Specific line from Hazel.log:
Code: Select all
Shellscript exited with non-successful status code: 1
hshinde
 
Posts: 4
Joined: Sun Dec 29, 2019 11:20 am

luomat wrote:Try adding this as the first line of your shell script in Hazel:
Code: Select all
PATH=/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin


Thanks for offering to help, I added that line as 1st line of the script. This is the error shown in log file:
Code: Select all
2020-01-01 20:44:24.387 hazelworker[64252] Running worker (v4.4.2) for folder with identifier: 16777229-1638856.
2020-01-01 20:44:24.388 hazelworker[64252] ###main load address: 0x10ecff000
2020-01-01 20:44:24.388 hazelworker[64252] ###Noodle load address: 0x10ee1e000
2020-01-01 20:44:24.388 hazelworker[64252] ###CK load address: 0x10ede4000
2020-01-01 20:44:24.410 hazelworker[64252] Processing folder Iconic Chair Retro Feel
2020-01-01 20:44:26.494 hazelworker[64252] CHIGNON Voluptuous Iconic Chair With a Subtle Retro Feel [cat.furn.chair.accent id.material.steel style.retro] - 3.jpeg: Rule Assign [EXIF keywords] to JPG Photos matched.
2020-01-01 20:44:27.287 hazelworker[64252] [Error] Shell script failed: Error processing shell script on file /Volumes/Data/Library/Mobile Documents/com~apple~CloudDocs/Photos/Photos_cat/Iconic Chair Retro Feel/CHIGNON Voluptuous Iconic Chair With a Subtle Retro Feel [cat.furn.chair.accent id.material.steel style.retro] - 3.jpeg.
2020-01-01 20:44:27.287 hazelworker[64252] Shellscript exited with non-successful status code: 1
hshinde
 
Posts: 4
Joined: Sun Dec 29, 2019 11:20 am

Turn on debug mode as described here: https://www.noodlesoft.com/kb/hazel-debug-mode/

After that, any output from the script should appear in the logs which might help here.
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:Turn on debug mode as described here: https://www.noodlesoft.com/kb/hazel-debug-mode/

After that, any output from the script should appear in the logs which might help here.


Thanks for the "debug" tip. Seems like Exiftools is quite particular about how filenames (with spaces) are passed on to it.

Also, I had to include "set LANG=en_GB.UTF-8" at the beginning of the script to make it work. I don't know why that was need by Exiftools, though.

Solution (For now, this seems to work):
Code: Select all
theFile=$1
set LANG=en_GB.UTF-8
<<COMMENT
Most of my files are named like this: " fileName[tag1 tag2 tag3].ext "
Below I extract the tags enclosed in [square brackets] and apply as Mac File Tags
COMMENT

opentags=`echo "$theFile" | sed 's/.*\[\([^]]*\)\].*/\1/g'`
if [[ ! -z "$opentags" ]]
then
for i in $opentags
do
    tag -a "$i" "$theFile"
done
fi

<<COMMENT
Below I extract any Mac File Tags and inject them as EXIF & IPTC tags.
COMMENT
assignedtags=`tag -NG "$theFile"`
/usr/local/bin/exiftool -P -overwrite_original_in_place -m -sep "," -keywords=$assignedtags "$theFile"
/usr/local/bin/exiftool -overwrite_original_in_place -m -P -xmp:all= -tagsfromfile @ -xmp:all "$theFile" && /usr/local/bin/exiftool -overwrite_original_in_place -m -P -sep ', ' '-keywords<${keywords;NoDups}' "$theFile"
/usr/local/bin/exiftool -m -P -overwrite_original_in_place -sep ', ' '-exif:xpkeywords<${iptc:keywords;NoDups}' "$theFile"
<<COMMENT
Below extract the oldest available file date from EXIF and prepend it to the fileName. If EXIF date info is not available, we take the Birth Date from the system and use it.
COMMENT
fmDate=`exiftool -FileModifyDate -d '%Y%m%d_%H%M%S' "$theFile"` ; fmDate=${fmDate#*:}
gpsDate=`exiftool -GPSDateTime -d '%Y%m%d_%H%M%S' "$theFile"` ; gpsDate=${gpsDate#*:}
modDate=`exiftool -ModifyDate -d '%Y%m%d_%H%M%S' "$theFile"` ; modDate=${modDate#*:}
oDate=`exiftool -DateTimeOriginal -d '%Y%m%d_%H%M%S' "$theFile"` ; oDate=${oDate#*:}
bDate=`stat -f "%SB" -t "%Y%m%d_%H%M%S" "$theFile"` ; bDate=${bDate#*:}
availableDates=""
for i in "$fmDate $gpsDate $modDate $oDate $bDate" ; do if [[ ! -z "$i" ]] ; then availableDates+="$i" ; fi ; done ; prependDate=`echo "$availableDates" | xargs -n1 | sort -g | xargs | head -n1 | awk '{print $1;}'`

/usr/local/bin/exiftool -P -m -overwrite_original '-FileName<'"$prependDate"'_$FileName' "$theFile"



Please let me know if the above code can be made better. I am an Interior Designer, so this is NOT in my domain of understanding. But I entirely depend on Tagging for storing and retrieving my files (not just images), and I deal with a LOT of images. So I need this functionality. Hope it helps someone else too.

Thanks a lot.
- Harshal
hshinde
 
Posts: 4
Joined: Sun Dec 29, 2019 11:20 am


Return to Support