如何在 OS X 中编辑文件元数据? [英] How can I edit file metadata in OS X?

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

问题描述

有谁知道是否可以在 OS X 上直接编辑文件元数据.特别是在 perl 中.我特别想改变的参数是 kMDItemFSLabel(文件的颜色).我已经搜索过了,我似乎无法找到一种方法来做到这一点,而不使用诸如 Mac::Glue 或外部应用程序 (Finder).

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 通信以更改其数据.据我所知,不通过 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 到 Objective C 的桥梁.然后您将需要使用 Apple 方法和 Cocoa 系统调用.可可的陡峭学习曲线...

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

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

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

使用 osascript 将消息发送到 Finder 以更改文件的颜色.你可以先看看这个因此,请发布有关这样做的提示.

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 相关的 Objective C/Cocoa 桥都不幸地死了.MacPerl 自 2005 年以来未更新.

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

几乎所有最简单的方法都需要至少知道最少数量的 Applescript 并通过对 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 文档.我的书中有一些例子,我称之为 Applescript - 权威指南 和来自 brian d foy 关于使用 Perl 控制 iTunes.

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 色.即标签orange"是label index 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天全站免登陆