Page 1 of 1

Any way to get metadata out of a file with Applescript?

PostPosted: Wed Nov 07, 2012 7:08 am
by cstromme
So first of all Hazel absolutely rocks, have a pretty great setup of rules now that … well, automates some things I used to do manually. :P

I've set it up now to send me an iMessage when Hazel adds a file to my iTunes, but I've just been using theFile in the Applescript and it occurred to me that since the file has a lot of metadata it might be better to use that in the text message instead of the filename.

Been searching online, tried exiftool but that didn't seem to be able to get the metadata I wanted out of the m4v file. Tried AtomicParsley but it chokes on larger files.

So anybody here that knows any way of getting metadata out of a m4v container using Applescript or a shell script/command?

Re: Any way to get metadata out of a file with Applescript?

PostPosted: Wed Nov 07, 2012 11:25 am
by a_freyer
First, run mdls in terminal to find the name of the attribute that you're looking for:

Code: Select all
mdls /Users/andrewfreyer/myFileForHazelToMove.txt


You'll see all of the kMDItem_____ medadata element names. Find the one you want, and declare it with -n kMDItemPhysicalSize.

Code: Select all
mdls -n MDItemPhysicalSize /Users/andrewfreyer/myFileForHazelToMove.txt


Now you'll see this printed:

kMDItemPhysicalSize = 9865600


I'd just awk the third element like this:


Code: Select all
mdls -n MDItemPhysicalSize /Users/andrewfreyer/myFileForHazelToMove.txt | awk '{print $3}'


For applescript, wrap that line in a do shell script line and you're done!

Re: Any way to get metadata out of a file with Applescript?

PostPosted: Wed Nov 07, 2012 4:20 pm
by cstromme
Thank you for that! :)

mdls doesn't seem to be able to access the media metadata though. As an example from a m4v file (with all the right metadata set and readable by iTunes) I get:

kMDItemFSContentChangeDate = 2012-10-29 22:46:46 +0000
kMDItemFSCreationDate = 2012-10-29 22:44:03 +0000
kMDItemFSCreatorCode = ""
kMDItemFSFinderFlags = 0
kMDItemFSHasCustomIcon = 0
kMDItemFSInvisible = 0
kMDItemFSIsExtensionHidden = 0
kMDItemFSIsStationery = 0
kMDItemFSLabel = 0
kMDItemFSName = "name of file.m4v"
kMDItemFSNodeCount = 868616882
kMDItemFSOwnerGroupID = 20
kMDItemFSOwnerUserID = 501
kMDItemFSSize = (null)
kMDItemFSTypeCode = ""

Re: Any way to get metadata out of a file with Applescript?

PostPosted: Wed Nov 07, 2012 5:44 pm
by Mr_Noodle
If the only metadata you are seeing are kMDItemFS entries, that means that the file is not being imported by Spotlight. Check your Spotlight privacy settings to make sure that they are being indexed properly.

Re: Any way to get metadata out of a file with Applescript?

PostPosted: Wed Nov 07, 2012 5:52 pm
by cstromme
Oh, I have spotlight turned off on the server (where this is running). I want to access this shortly after Identify has set the metadata though, would spotlight be done indexing it that quickly?

EDIT: Indexed it now, but it doesn't seem to get all the tags like TV show.

Re: Any way to get metadata out of a file with Applescript?

PostPosted: Fri Nov 09, 2012 12:38 pm
by Mr_Noodle
Keep in mind that Spotlight attributes are more generalized so what you see as TV Show may be stored as a different attribute in Spotlight, since Spotlight has to handle a bunch of different file types. It may also be the case where Spotlight doesn't import certain fields in which case you'll need to find a better importer for those files.