Shell script to assign file tags and EXIF keywords fails

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

Moderator: Mr_Noodle

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.

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
hshinde
 
Posts: 7
Joined: Sun Dec 29, 2019 11:20 am

I think you can do this totally within Hazel without a script. Look up match patterns in the manual. You can use a list attribute to match the stuff inside the square brackets. This will create a list which you can then use in a dynamic tag in the Add Tags action to assign that list as tags to the file.
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

If this helps anyone, I got it working with the following shell script:

Code: Select all
#!/bin/zsh
## WORKING EXIF & MacOS TAGS

# Check if a file path is provided
if [[ -z "$1" ]]; then
    echo "Usage: $0 <file>"
    exit 1
fi

# Get the absolute file path
theFile="$1"

# Extract the TAGs inside square brackets using sed
tagString=$(echo "$theFile" | sed -n 's/.*\[\(.*\)\].*/\1/p')

# If no tags are found, exit
if [[ -z "$tagString" ]]; then
    echo "No tags found in filename."
    exit 1
fi

# Convert space-separated tags into an array
tags=("${(@s: :)tagString}")  # Zsh array split by space

# Clear all pre-existing EXIF keywords
exiftool -keywords= -iptc:keywords+= -exif:xpkeywords= -xmp= -overwrite_original -r "$theFile"

# Assign EXIF and IPTC keywords iteratively
for tag in "${tags[@]}"; do
    exiftool -overwrite_original -keywords+="$tag" -iptc:keywords+="$tag" -api nodups "$theFile"
done

# Clear all pre-existing macOS tags using xattr
xattr -w com.apple.metadata:_kMDItemUserTags "" "$theFile"


# Assign macOS tags iteratively
for tag in "${tags[@]}"; do
    tag -a "$tag" "$theFile"
done

exit 0

hshinde
 
Posts: 7
Joined: Sun Dec 29, 2019 11:20 am


Return to Support