How to edit file metadata in OS X?

Does anyone know if it is possible to directly edit file metadata in OS X. In particular, in perl. The parameter I'm specifically trying to change is kMDItemFSLabel (file color). I had a search around and I cannot find a way to do this without using a module like Mac :: Glue or an external application (Finder).

+6
perl applescript metadata macos
source share
3 answers

The kMDItemFSLabel attribute is a Finder property. You need to use the way to communicate with Finder to change its data. As far as I know, there is no bit with which you can twist Perl to change Finder data without wading through Finder.

There are several ways to do this:

  • Use CamelBones when a new version comes out. This allows you to use the bridge from Objective-C from Perl. Then you will need to use the Apple method with Cocoa system calls. A steep learning curve for Cocoa ...

  • If you have developer tools, use / Developer / Tools / SetFile (if this supports the metadata element)

  • Use osascript to send a Finder message to change the color of the file. You can see this earlier SO post for tips on this.

Most of the bridges associated with Perl Objective C / Cocoa died unfortunately. MacPerl has not been updated since 2005.

Almost all of the simplest methods require knowledge of at least a minimal amount of Applescript and calling the text of this script, although calling an interpolated osascript type.

In its 1-line form, osascript makes Perl beautiful:

 osascript -e 'tell application "Finder"' -e "activate" -e "display dialog \"hello\"" -e 'end tell' 

To use osascript from Perl, most use a HERE document. There are examples from a book I called Applescript - the ultimate guide and brian d foy on managing iTunes with Perl .

Here is the Perl script I wrote to adjust the color of the file using osascript:

 #!/usr/bin/perl use strict; use warnings; use File::Spec; use String::ShellQuote; sub osahere { my $rtr; my $scr='osascript -ss -e '."'".join ('',@_)."'"; open my $fh, '-|', $scr or die "death on osascript $!"; $rtr=do { local $/; <$fh> }; close $fh or die "death on osascript $!"; return $rtr; } sub set_file_color { # -- No color = 0 # -- Orange = 1 # -- Red = 2 # -- Yellow = 3 # -- Blue = 4 # -- Purple = 5 # -- Green = 6 # -- Gray = 7 my $file=shift; my $color=shift || 0; $color=0 if $color<0; $color=7 if $color>7; $file=File::Spec->rel2abs($file) unless File::Spec->file_name_is_absolute( $file ); $file=shell_quote($file); return undef unless -e $file; my $rtr=osahere <<"END_SET_COLOR" ; tell application "Finder" set f to "$file" set ItemToLabel to POSIX file f as alias set the label index of ItemToLabel to $color end tell END_SET_COLOR return $rtr; } set_file_color("2591.txt",2); 

If the Finder color is 0, kMDItemFSLabel is 0. If there is any color set, kMDItemFSLabel becomes 8-color. those. the orange label index is label index 1, kMDItemFSLabel = 7; the red label index is label index 2, kMDItemFSLabel = 6; etc.

+9
source share

Perl does not have a built-in feature for working with Mac file system metadata. Either you use the library from CPAN, or you write it yourself, perhaps not in the way that the author of what was in CPAN did.

+1
source share

This is actually not so difficult to implement. You can use the xattr command, as shown in the doc line below ... I wrapped the main function in a python script that applies named colors to a series of files ...

 #!/usr/bin/env python """ ================================== LABELCOLOR.PY - Colorize Finder labels of files Usage: labelcolor.py [color] *.jpg where color is a name or abbreviation as defined below: clear (c), gray (a), green (g), purple (p), blue (b), yellow (y), red (r), orange (o) The shell command used is: xattr -wx com.apple.FinderInfo \ 0000000000000000000400000000000000000000000000000000000000000000 myfile.txt where 04 in the middle of the zeroes is the color definition ================================== """ import sys import os import subprocess def colorizeFile(ColorName,FileName): ReverseTable = { "clear" : "01", "gray" : "03", "green" : "04", "purple" : "06", "blue" : "09", "yellow" : "0A", "red" : "0C", "orange" : "0E", "c" : "01", "a" : "03", "g" : "04", "p" : "06", "b" : "09", "y" : "0A", "r" : "0C", "o" : "0E", } HexString = 18*"0" + ReverseTable.get(ColorName) + 44*"0" Xcommand = 'xattr -wx com.apple.FinderInfo {0} {1}'.format(HexString,FileName) ProcString = subprocess.check_call(Xcommand, stderr=subprocess.STDOUT,shell=True) if __name__ == "__main__": if len(sys.argv)<3: sys.stderr.write(__doc__.format(sys.argv[0])) else: Cname = sys.argv[1] Flist = sys.argv[2:] for File in Flist: colorizeFile(Cname.lower(),File) sys.stderr.write("## Colorized {0} file(s) as {1}\n".format(len(Flist),Cname)) 
+1
source share

All Articles