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

a0x5cqrl  于 2022-11-15  发布在  Perl
关注(0)|答案(4)|浏览(157)

有人知道在OS X上是否可以直接编辑文件元数据吗?特别是在perl中。我特别想修改的参数是kMDItemFSLabel(文件的颜色)。我已经搜索过了,我似乎找不到一种不使用Mac::Glue之类的模块或外部应用程序(Finder)的方法。

4xy9mtcn

4xy9mtcn1#

kMDItemFSLabel属性是Finder的一个属性。你需要使用一种方法与Finder通信来更改它的数据。据我所知,没有一种方法可以在不通过Finder的情况下使用Perl来更改Finder的数据。
有几种方法可以做到这一点:
1.当新版本出来时使用CamelBones。这允许从Perl到Objective C的桥梁。然后你将需要使用Apple方法和可可系统调用。Cocoa的学习曲线陡峭...
1.如果您有开发人员工具,请使用/Developer/Tools/SetFile(如果支援中继数据项目)
1.使用osascript将消息发送到Finder以更改文件的颜色。您可以查看this早期的SO post以获得相关提示。
大多数与Perl相关的Objective C /可可桥都不幸死亡了。MacPerl自2005年以来就没有更新过。
几乎所有最简单的方法都要求至少知道最少量的Applescript,并通过对osascript的插入类型调用来调用该脚本的文本。
osascript的1行形式使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编写的一个脚本,用于使用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);

如果Finder颜色为0,则kMDItemFSLabel为0。如果设置了任何颜色,则kMDItemFSLabel变为8色。即,标签“橙子”为label index 1,kMDItemFSLabel = 7;标记“红色”是label index = 2,kMDItemFSLabel = 6;和/或其他信息。

fwzugrvs

fwzugrvs2#

Perl中没有内置函数来操作Mac文件系统元数据。要么你使用CPAN中的库,要么你自己写,可能不如CPAN中的作者写得好。

eni9jsuy

eni9jsuy3#

实际上实现起来并不复杂。你可以使用xattr命令,如下面的文档字符串所示......我已经将基本函数 Package 在一个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))
lmvvr0a8

lmvvr0a84#

这个问题在很久以前就得到了回答.从那时起,世界发生了演变:

  • tag项目是一个二进制文件,允许您操作标记(以前称为label)。
  • xattr可以让你获得和设置各种各样的东西。这是我主要使用的。例如,与Mojolicious我喜欢添加com.apple.metadata:kMDItemWhereFroms到我下载的东西。

相关问题