如何在OS X中编辑文件元数据?

时间:2010-09-26 21:06:46

标签: perl macos applescript metadata

有没有人知道是否可以在OS X上直接编辑文件元数据。特别是在perl中。我特意尝试更改的参数是kMDItemFSLabel(文件的颜色)。我已经进行了搜索,如果不使用Mac::Glue或外部应用程序(Finder)等模块,我似乎无法找到方法。

4 个答案:

答案 0 :(得分:10)

kMDItemFSLabel属性是Finder的属性。您需要使用与Finder通信的方式来更改其数据。据我所知,你无法通过Perl来改变Finder的数据,而无需通过Finder。

有几种方法可以做到这一点:

  1. 新版本发布时使用CamelBones。这允许从Perl桥接到Objective C.然后,您将需要在Cocoa系统调用中使用Apple方法。 Cocoa的陡峭学习曲线......

  2. 如果您有开发人员工具,请使用/ Developer / Tools / SetFile(如果支持元数据项)

  3. 使用osascript将消息发送到Finder以更改文件的颜色。您可以查看this之前的SO帖子,了解相关提示。

  4. 不幸的是,大多数与Perl相关的Objective C / Cocoa桥已经死亡。 MacPerl自2005年以来一直没有更新。

    几乎所有最简单的方法都需要知道至少最少量的Applescript并通过对osascript的内插类型调用来调用该脚本的文本。

    在其1行形式中,osascript使Perl看起来很漂亮:

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

    要使用Perl的osascript,大多数都使用HERE文档。我在书中提到了Applescript - The Definitive Guidebrian d foy on Controlling iTunes with Perl

    这是我用Perl编写的用于设置文件颜色的Perl脚本:

    #!/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);
    

    如果Finder颜色为0,则kMDItemFSLabel为0.如果有任何颜色设置,kMDItemFSLabel将变为8色。即,标签“orange”是label index 1,kMDItemFSLabel = 7;标签“red”是label index 2,kMDItemFSLabel = 6;等等。

答案 1 :(得分:1)

Perl中没有内置函数可以在Mac文件系统元数据上运行。您可以使用CPAN中的库,也可以自己编写,可能不如CPAN的作者那样。

答案 2 :(得分:1)

实施起来并不复杂。您可以使用xattr命令,如下面的doc字符串所示... 我已经将基本函数包装在python脚本中,该脚本将命名颜色应用于一系列文件......

#!/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))

答案 3 :(得分:0)

这个问题很久以前就回答了。从那时起,世界

  • tag项目是一个二进制文件,可让您操作标签(以前称为标签)。

  • xattr可让您获取并设置各种东西。这是我主要使用的。例如,使用Mojolicious,我想在下载的内容中添加com.apple.metadata:kMDItemWhereFroms

相关问题