我怎样才能在OS X中编辑文件元数据? [英] How can I edit file metadata in OS X?

查看:183
本文介绍了我怎样才能在OS X中编辑文件元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道是否有可能在Perl在OS X上直接编辑文件元数据具体。我特别想改变的参数是 kMDItemFSLabel (该文件的颜色)。我身边有一个搜索和我似乎无法找到一个方法来做到这一点,而无需使用一个模块,如苹果::胶水或外部应用程序(取景器)。

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).

推荐答案

kMDItemFSLabel 属性是Finder中的一个属性。你需要使用的方式与Finder中沟通,改变它的数据。据我所知,没有一点你可以用Perl玩弄改变Finder的数据,而不用通过取景器去。

The kMDItemFSLabel attribute 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:


  1. 使用 CamelBones 当新版本出来。这使得桥梁在Perl目标C。然后,你将需要使用苹果的方法,可可系统调用。可可陡峭的学习曲线...

  1. 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...

如果你有开发工具,使用/开发/工具/ SetFile(如果支持元数据项)

If you have developer tools, use /Developer/Tools/SetFile (if that supports the metadata item)

使用osascript将消息发送到Finder更改文件的颜色。你可以看一下<一个href=\"http://stackoverflow.com/questions/2435580/tagging-files-with-colors-in-os-x-finder-from-shell-scripts\">this早期SO张贴在这样做的提示。

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.

大多数Perl有关的目标C /可可桥梁已不幸夭折。 MacPerl尚未自2005年更新

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

几乎所有的最简单的方法需要知道的最起码的AppleScript量并调用脚本,虽然内插的呼叫类型的文本<一个href=\"http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/osascript.1.html\">osascript.

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.

在其1线形态,osascript令Perl看起来很漂亮:

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

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

要在Perl使用osascript,使用最多的一个HERE文档。有例子来自我的书,我已经叫<一个href=\"http://flylib.com/books.php?ln=en&n=4&p=105&c=207&p1=1&c1=1&c2=267&view=2\">Applescript - 在iTunes的控制权威指南并从布赖恩ðFOY用Perl

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.

下面是Perl脚本,我写了使用osascript设置文件的颜色:

Here is a script in Perl I wrote for setting file color 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);

如果在Finder颜色是0, kMDItemFSLabel 0。如果有任何颜色设置, kMDItemFSLabel 变为8 -颜色。例如,标签橙色为标记指数 1 kMDItemFSLabel = 7;标签红标记指数 2, kMDItemFSLabel = 6;等

If the Finder color is 0, kMDItemFSLabel is 0. If there is any color set, kMDItemFSLabel becomes 8-color. ie, label "orange" is label index 1, kMDItemFSLabel = 7; label "red" is label index 2, kMDItemFSLabel = 6; and so on.

这篇关于我怎样才能在OS X中编辑文件元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆