Add Additional Downloaded File Metadata

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

Add Additional Downloaded File Metadata Mon Dec 31, 2012 5:50 pm • by a_freyer
I wanted more metadata for each of my downloaded files besides WhereFrom. Right now, I use these to sort files I download at work from files I downloaded at home, but I'm sure there are dozens of other uses. After all, the more data you have, the more organized you can be!

The rule looks like this:

Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     Passes shell script (embedded script)

Do the following to the matched file or folder:
     Run shell script (embedded script)

1. Condition Shell Script

This script tests for one of the kMDItems I added, so that it doesn't try and add incorrect data at a later date. Quite simply, it reads the current metadata from the file or folder, and exits with the number of lines which contain my special metadata item:

Code: Select all
exit $(mdls "$1" | grep -c "kMDItemWhereFromSSID")

2. Metadata Shell Script

This script adds new and interesting download metadata. You can add just about anything you want, but this is what I've done:
Code: Select all
#Adds the SSID as kMDItemWhereFromSSID (en0 for MBA, en1 for MBPs & MB w/ ethernet)
/usr/bin/xattr -w "$(/usr/sbin/networksetup -getairportnetwork en0 | awk -F": " '{print $2}')" "$1"

#Adds the local IP address as kMDItemWhereFromLocalIP (en0 for MBA, en1 for MBPs & MB w/ ethernet)
/usr/bin/xattr -w "$(ifconfig en0 | grep -Eow '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' | head -1)" "$1"

#Adds the external IP address as kMDItemWhereFromExternalIP (en0 for MBA, en1 for MBPs & MB w/ ethernet)
/usr/bin/xattr -w "$(curl -s" "$1"

#Adds the download name as kMDItemDownloadedAs (in case it was changed in the future, easier to find)
/usr/bin/xattr -w "$(basename "$1")" "$1"

Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Return to Tips & Tricks - DO NOT POST QUESTIONS