#!/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;和/或其他信息。
4条答案
按热度按时间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看起来很漂亮:
要使用Perl中的osascript,大多数人都使用HERE文档。我在书中提到了Applescript - The Definitive Guide和brian d foy on Controlling iTunes with Perl中的例子。
下面是我用Perl编写的一个脚本,用于使用osascript设置文件颜色:
如果Finder颜色为0,则
kMDItemFSLabel
为0。如果设置了任何颜色,则kMDItemFSLabel
变为8色。即,标签“橙子”为label index
1,kMDItemFSLabel
= 7;标记“红色”是label index
= 2,kMDItemFSLabel
= 6;和/或其他信息。fwzugrvs2#
Perl中没有内置函数来操作Mac文件系统元数据。要么你使用CPAN中的库,要么你自己写,可能不如CPAN中的作者写得好。
eni9jsuy3#
实际上实现起来并不复杂。你可以使用
xattr
命令,如下面的文档字符串所示......我已经将基本函数 Package 在一个python脚本中,该脚本将命名的颜色应用到一系列文件中......lmvvr0a84#
这个问题在很久以前就得到了回答.从那时起,世界发生了演变:
com.apple.metadata:kMDItemWhereFroms
到我下载的东西。