Page 1 of 1

Add Additional Downloaded File Metadata

PostPosted: 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 com.apple.metadata:kMDItemWhereFromSSID "$(/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 com.apple.metadata:kMDItemWhereFromLocalIP "$(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 com.apple.metadata:kMDItemWhereFromExternalIP "$(curl -s http://www.icanhazip.com)" "$1"

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