Can Hazel see iTunes metadata?

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

Moderator: Mr_Noodle

Can Hazel see iTunes metadata? Wed Aug 09, 2017 1:13 pm • by leftnotracks
I use Meta Z to tag ripped video files. Can Hazel see the metadata for the files, such as Video Type or Genre? I’d like to automatically sort video into it’s proper folder. Bonus if I can use Genre with Sort into subfolder.
leftnotracks
 
Posts: 23
Joined: Sat Oct 29, 2016 3:58 am

Re: Can Hazel see iTunes metadata? Wed Aug 09, 2017 4:19 pm • by Mr_Noodle
Its dependent on whether that metadata is indexed by Spotlight. One way to check is to select the file in Finder and do "Get Info". You should see the metadata under the "More Info" section.
Mr_Noodle
Site Admin
 
Posts: 11755
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Can Hazel see iTunes metadata? Tue Jan 28, 2025 5:40 pm • by cortig
Mr_Noodle wrote:Its dependent on whether that metadata is indexed by Spotlight. One way to check is to select the file in Finder and do "Get Info". You should see the metadata under the "More Info" section.


I’m coming back to this somewhat old thread because I have a similar issue.
I see now in Hazel 6.0.3 that we can use a Genre attribute. I tried to use this approach without any success.
Is Genre also taken from Spotlight metadata or is it coming from the video file metadata itself?
I could use a script to extract the genre information using ffmpeg, but it sounds like a lot of work—especially if I’m just not properly understanding where the Genre attribute in Hazel is extracted from…



Corentin
cortig
 
Posts: 25
Joined: Thu Jan 06, 2022 11:49 am

Re: Can Hazel see iTunes metadata? Wed Jan 29, 2025 10:42 am • by Mr_Noodle
All attributes (except one, which is labelled as being provided by Hazel) are from Spotlight. Note that it is the app(s) that handle certain files that determine what metadata to provide.
Mr_Noodle
Site Admin
 
Posts: 11755
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Can Hazel see iTunes metadata? Wed Jan 29, 2025 4:17 pm • by cortig
Mr_Noodle wrote:All attributes (except one, which is labelled as being provided by Hazel) are from Spotlight. Note that it is the app(s) that handle certain files that determine what metadata to provide.



Thanks for letting me know! I wasn't sure whether there might have been changes in that area, since Genre was listed directly within Hazel. I had hopes that Hazel could read the movie file metadata.

I can extract the Genre of the movie using the following shell script (with ffmpeg installed, of course):
Code: Select all
ffmpeg -i $1 2>&1 | grep -i "genre" | cut -d':' -f2 | tr -d '[:space:]'

But it doesn't look like I can use that approach to add a tag, even using the dynamic tags in Hazel. Is there a way to parse the result of the script and use it to add a tag?
cortig
 
Posts: 25
Joined: Thu Jan 06, 2022 11:49 am

Re: Can Hazel see iTunes metadata? Thu Jan 30, 2025 9:40 am • by Mr_Noodle
You can use AppleScript to create custom attributes which can be returned back to Hazel. The manual has more details on how to do that.
Mr_Noodle
Site Admin
 
Posts: 11755
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Can Hazel see iTunes metadata? Thu Jan 30, 2025 11:54 am • by cortig
Mr_Noodle wrote:You can use AppleScript to create custom attributes which can be returned back to Hazel. The manual has more details on how to do that.


I struggled with that a bit. I wasn't sure how to use the string returned by the script and use it as a tag.
As an alternate option, I created a shell script that extracts the Genre using ffmpeg (again, it requires that you have ffmpeg installed), then uses that to apply the tag directly in the Finder.
AppleScript is unable to properly manipulate tags apparently so I had to use xattr.
I asked ChatGPT for help (but the code failed!), and eventually got it to work after some significant editing. My solution is not elegant (I could have used the plist command, but it ended up easier to just do it the blunt way and type the plist structure in instead), and is only designed for files that only have one Genre (but from what I can tell they should not have more than one), but it works :-)
I'm not a good scripter so if anyone wants to revise it, I sure won’t take offense :-)
In case anyone needs it, here it is:

Code: Select all
#!/bin/bash
   
# Function to extract genre from video file
get_genre() {
    local file="$1"
    local genre

# Extract metadata, grep for genre, and remove unnecessary text
    genre=$(ffmpeg -i "$file" 2>&1 | grep -i "genre" | cut -d':' -f2 | tr -d '[:space:]')

    echo "$genre"
}

# Function to set Finder tag using xattr
set_finder_tag() {
    local file="$1"
    local tag="$2"

    if [[ -z "$tag" ]]; then
        echo "No genre found; no tag will be applied."
    else
   
        # Set the Finder tag
      TagCmd="xattr -w com.apple.metadata:_kMDItemUserTags"
      leadingStr="'<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"><plist version=\"1.0\"><array><string>"
      closingStr="</string></array></plist>'"
        eval "$TagCmd $leadingStr$tag$closingStr '$file'"
        echo "Set Finder tag: '$tag' for file: $file"
    fi
}

# Check for required commands
if ! command -v ffmpeg &> /dev/null; then
    echo "ffmpeg is required but it’s not installed. Exiting."
    exit 1
fi

if [[ $# -eq 0 ]]; then
    echo "Usage: $0 <video-file>"
    exit 1
fi

video_file="$1"

# Get genre of the video file
genre=$(get_genre "$video_file")

# Set Finder tag based on genre
set_finder_tag "$video_file" "$genre"


It echoes some of the results along the script, and if you ever need to troubleshoot, you can see the output in the Hazel logs.
cortig
 
Posts: 25
Joined: Thu Jan 06, 2022 11:49 am

Re: Can Hazel see iTunes metadata? Fri Jan 31, 2025 11:01 am • by Mr_Noodle
If you decide to revisit using AppleScript, you can have it call the shell script, put the output into a variable which it exports as a custom attribute to Hazel. Hazel can then use that in a dynamic tags using the Add tags action.
Mr_Noodle
Site Admin
 
Posts: 11755
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Can Hazel see iTunes metadata? Mon Feb 03, 2025 5:12 pm • by cortig
Mr_Noodle wrote:If you decide to revisit using AppleScript, you can have it call the shell script, put the output into a variable which it exports as a custom attribute to Hazel. Hazel can then use that in a dynamic tags using the Add tags action.



I tried, but I must be missing something with the way the script result gets returned to Hazle.
I switched to using exiftool instead of ffmpeg (exiftool is lighter and less prone to returning weird errors) and my embeded script looks like this:
Code: Select all
set theFilePath to POSIX path of theFile
set theGenre to (do shell script "/usr/local/bin/exiftool " & "'" & theFilePath & "'" & " 2>&1 | grep -i 'genre' | awk -F ': ' '{print $2}'") as text
return {hazelPassesScript:true, hazelOutputAttributes:{theGenre}}


I created a custom attribute for the script and used that as a dynamic tag to tag the file (that's a neat trick that I clearly wasn't aware of before I started wiying witht he embeded scripts), but the genre the script identifies never gets used to tag the file.
I suspect I got something wrong with:
Code: Select all
return {hazelPassesScript:true, hazelOutputAttributes:{theGenre}}
…but I can't figure out what despite my best efforts.
cortig
 
Posts: 25
Joined: Thu Jan 06, 2022 11:49 am

Re: Can Hazel see iTunes metadata? Tue Feb 04, 2025 10:13 am • by Mr_Noodle
You can try using "display dialog" in the AppleScript to display what the genre is to make sure that it was captured properly.
Mr_Noodle
Site Admin
 
Posts: 11755
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Can Hazel see iTunes metadata? Tue Feb 04, 2025 3:39 pm • by cortig
cortig wrote: …but I can't figure out what despite my best efforts.


OK, I'm confused…
I came back to it today and it simply worked! :-) Not sure why it wasn't working before. I haven't changed a thing!
So, in case someone needds it, in summary:

  1. Install ExifTool (from https://exiftool.org or through homebrew running brew install exiftool)
  2. Create a rule with the premise If Kind is Movie
  3. and Do the following to the matched file or folder: Run AppleScript | embedded script
    Code: Select all
    set theFilePath to POSIX path of theFile
    set theGenre to (do shell script "/usr/local/bin/exiftool " & "'" & theFilePath & "'" & " 2>&1 | grep -i 'genre' | awk -F ': ' '{print $2}'") as text
    return {hazelPassesScript:true, hazelOutputAttributes:{theGenre}}

  4. with a custom attribute exported by the script (I chose to name it myGenre)
  5. Add a second action: Add Tag > Dynamic tag > • myGenre
cortig
 
Posts: 25
Joined: Thu Jan 06, 2022 11:49 am

Re: Can Hazel see iTunes metadata? Tue Feb 04, 2025 3:45 pm • by cortig
Mr_Noodle wrote:You can try using "display dialog" in the AppleScript to display what the genre is to make sure that it was captured properly.


I did try that :-) That's how I troubleshooted the script in the Script Editor before putting it into Hazel. I was using:
Code: Select all
set theFilePath to POSIX path of theFile
set theGenre to (do shell script "/usr/local/bin/exiftool " & quoted form of theFilePath & " 2>&1 | grep -i 'genre' | awk -F ': ' '{print $2}'") as text
return {hazelPassesScript:true, hazelOutputAttributes:{theGenre}}

to make sure the script was properly identifying the Genre.

As I was saying in the other post, it's now working (and I still don’t know why :-) )
cortig
 
Posts: 25
Joined: Thu Jan 06, 2022 11:49 am


Return to Support