Does anyone know if it is possible to directly edit file metadata on OS X. Specifically in perl. The parameter I’m specifically trying to change is kMDItemFSLabel (The color of the file). I’ve had a search around and I can’t seem to find a way to do this without using a module such as Mac::Glue or an external application (Finder).
Does anyone know if it is possible to directly edit file metadata on OS
Share
The
kMDItemFSLabelattribute is a property of the Finder. You need to use a way to communicate with the Finder to change its data. As far as I know, there is no bit you can twiddle with Perl to change the Finder’s data without going through the Finder.There are several ways to do this:
Use CamelBones when the new version comes out. That allows a bridge to Objective C from Perl. Then you will need to use the Apple method with Cocoa system calls. Steep learning curve for Cocoa…
If you have developer tools, use /Developer/Tools/SetFile (if that supports the metadata item)
Use osascript to send the message to the Finder to change the color of the file. You can look at this earlier SO post for hints on doing that.
Most of the Perl related Objective C / Cocoa bridges have died unfortunately. MacPerl has not been updated since 2005.
Almost all the easiest methods require knowing at least minimal amount of Applescript and calling the text of that script though an interpolated type call to osascript.
In its 1 line form, osascript makes Perl look beautiful:
To use osascript from Perl, most use a HERE document. There are examples from I book I have called Applescript – The Definitive Guide and from brian d foy on Controlling iTunes with Perl.
Here is a script in Perl I wrote for setting file color using osascript:
If the Finder color is 0,
kMDItemFSLabelis 0. If there is any color set,kMDItemFSLabelbecomes 8-color. ie, label “orange” islabel index1,kMDItemFSLabel= 7; label “red” islabel index2,kMDItemFSLabel= 6; and so on.