Shell script to assign file tags and EXIF keywords fails

My image files are generally named in this format: "filename [TAG1 TAG2 ... TAGn].xxx". The TAGS are separated by space and enclosed within square brackets. The file extension will vary depending on the image format. I have this script where it takes the TAG part of the file name enclosed within "[" and "]" so that it will the assign MacOS Tags of the same name to the file. I use "tag" command instead of "osascript". After that the script is supposed to assign the tags as EXIF and IPTC keywords using "exiftools".
I tried running it as an embedded script, and then as an external script. Hazel just throws "shell script failed" error message. [I am on Apple M1, MacOS Ventura 13.7.2] Can someone please help me fix it? I am an interior designer and have no coding knowledge and am just wading though google search etc.
I tried running it as an embedded script, and then as an external script. Hazel just throws "shell script failed" error message. [I am on Apple M1, MacOS Ventura 13.7.2] Can someone please help me fix it? I am an interior designer and have no coding knowledge and am just wading though google search etc.
- Code: Select all
#!/bin/zsh
theFile="$1"
tags=$(echo "$theFile" | sed -E 's/.*\[([^]]*)\].*/\1/')
if [[ -n "$tags" ]]; then
mac_tags="${tags// /,}" # Comma-separated for macOS tags
exif_tags="${tags// /,}" # Comma-separated for exiftool
# macOS Tags (using 'tag' command)
if ! tag -s "$mac_tags" "$theFile"; then # -s sets tags, overwrites existing ones
echo "Error setting macOS tags for: $theFile"
exit 1 # Indicate an error to Hazel
fi
# EXIF/IPTC Keywords
if ! exiftool -Keywords="{$exif_tags}" -IPTC:Keywords="{$exif_tags}" "$theFile"; then
echo "Error setting EXIF/IPTC keywords for: $theFile"
exit 1 # Indicate an error to Hazel
fi
exit 0 # Success!
else
echo "No tags found in filename: $theFile"
exit 0 # Technically successful (no tags to process), so exit 0
fi