gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:56:26 24 4
gpt4 key购买 nike

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

最佳答案

kMDItemFSLabel属性是 Finder 的一个属性。您需要使用某种方式与 Finder 进行通信以更改其数据。据我所知,不通过 Finder 就无法使用 Perl 来更改 Finder 的数据。

做这件事有很多种方法:

  • 使用CamelBones新版本出来的时候。这允许从 Perl 到 Objective C 的桥梁。然后,您需要将 Apple 方法与 Cocoa 系统调用一起使用。 cocoa 的陡峭学习曲线...
  • 如果您有开发人员工具,请使用/Developer/Tools/SetFile(如果支持元数据项)
  • 使用 osascript 将消息发送到 Finder 以更改文件的颜色。你可以看看this较早的 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'

    要使用 Perl 的 osascript,大多数使用 HERE 文档。我的书中有一些例子,我称之为 Applescript - The Definitive Guide来自 brian d foy on Controlling iTunes with Perl .

    这是我编写的使用 osascript 设置文件颜色的 Perl 脚本:
    #!/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;等等。

    关于perl - 如何在 OS X 中编辑文件元数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3799826/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com