加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 22:06:19 所属栏目:大数据 来源:网络整理
导读:有谁知道是否可以在OS X上直接编辑文件元数据.特别是在perl中.我特意试图改变的参数是kMDItemFSLabel(文件的颜色).我已经进行了搜索,如果不使用诸如 Mac::Glue或外部应用程序(Finder)之类的模块,我似乎无法找到方法. 解决方法 kMDItemFSLabel属性是Finder的
有谁知道是否可以在OS X上直接编辑文件元数据.特别是在perl中.我特意试图改变的参数是kMDItemFSLabel(文件的颜色).我已经进行了搜索,如果不使用诸如 Mac::Glue或外部应用程序(Finder)之类的模块,我似乎无法找到方法.

解决方法

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

做这件事有很多种方法:

>新版本发布时使用CamelBones.这允许从Perl桥接到Objective C.然后,您将需要在Cocoa系统调用中使用Apple方法. Cocoa的陡峭学习曲线……
>如果您有开发人员工具,请使用/ Developer / Tools / SetFile(如果支持元数据项)
>使用osascript将消息发送到Finder以更改文件的颜色.您可以在this之前的SO帖子中查看有关这样做的提示.

不幸的是,大多数与Perl相关的Objective C/C++ocoa桥已经死亡. MacPerl自2005年以来一直没有更新.

几乎所有最简单的方法都需要知道至少最少量的Applescript并通过对osascript的插值类型调用来调用该脚本的文本.

在其1行形式中,osascript使Perl看起来很漂亮:

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

要使用Perl的osascript,大多数都使用HERE文档.我书中有一些例子叫做Applescript – The Definitive Guide和brian 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色.即,标签“orange”是标签索引1,kMDItemFSLabel = 7; label“red”是标签索引2,kMDItemFSLabel = 6;等等.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读