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

7 macos perl applescript metadata

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

daw*_*awg 10

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

做这件事有很多种方法:

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

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

  3. 使用osascript将消息发送到Finder以更改文件的颜色.你可以看一下这篇早期的SO帖子,看看有关方面的提示.

不幸的是,大多数与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'
Run Code Online (Sandbox Code Playgroud)

要使用Perl的osascript,大多数都使用HERE文档.我的书中有一些例子,我称之为Applescript - The Definitive Guide,以及bri d foy用Perl控制iTunes.

这是我用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);
Run Code Online (Sandbox Code Playgroud)

如果Finder颜色为0,kMDItemFSLabel则为0.如果有任何颜色设置,则kMDItemFSLabel变为8色.即标签"orange"为label index1,kMDItemFSLabel= 7; 标签"red"是label index2,kMDItemFSLabel= 6; 等等.