Moderator: Mr_Noodle
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.
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.
ffmpeg -i $1 2>&1 | grep -i "genre" | cut -d':' -f2 | tr -d '[:space:]'
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.
#!/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"
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.
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}}
return {hazelPassesScript:true, hazelOutputAttributes:{theGenre}}
cortig wrote: …but I can't figure out what despite my best efforts.
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}}
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.
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}}