使用 Automator/AppleScript/Perl 创建 Plist

发布于 2024-10-02 07:43:43 字数 586 浏览 4 评论 0原文

我有几个图像文件夹,并且想创建一个包含文件名及其所在文件夹名称的 plist。

例如:

YELLOW
    MARKIN.PNG
    MARKOUT.PNG
BLUE
    ARROW.PNG
    RAZOR.PNG

我想要一个带有数组根节点的 plist,其中包含这样的字典:

<plist version="1.0">
<array>
    <dict>
        <key>name</key>
        <string>MARKIN</string>
        <key>colour</key>
        <string>YELLOW</string>
    </dict>
</array>
</plist>

这可以使用 Applescript 或 automator 或其他编程语言来实现吗?我的 7 个文件夹中有大约 60 张图像,手动输入所有内容确实很痛苦。

谢谢

I have several folders of images and would like to create a plist containing the file's name and the name of the folder its in too.

For example:

YELLOW
    MARKIN.PNG
    MARKOUT.PNG
BLUE
    ARROW.PNG
    RAZOR.PNG

I would like a plist, with an array root node, containing dictionaries such that:

<plist version="1.0">
<array>
    <dict>
        <key>name</key>
        <string>MARKIN</string>
        <key>colour</key>
        <string>YELLOW</string>
    </dict>
</array>
</plist>

Is this achievable using Applescript or automator or other programming language? I have about 60 images in 7 folders, and to type everything manually would be a real pain.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世态炎凉 2024-10-09 07:43:43

系统事件库具有创建 plist 文件和添加元素的基本工具。示例代码取自 Mac OS X 自动化网站

tell application "System Events"
 -- create an empty property list dictionary item
 set the parent_dictionary to make new property list item with properties {kind:record}
 -- create new property list file using the empty dictionary list item as contents
 set the plistfile_path to "~/Desktop/example.plist"
 set this_plistfile to ¬
 make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
 -- add new property list items of each of the supported types
 make new property list item at end of property list items of contents of this_plistfile ¬
 with properties {kind:boolean, name:"booleanKey", value:true}
 make new property list item at end of property list items of contents of this_plistfile ¬
 with properties {kind:date, name:"dateKey", value:current date}
 make new property list item at end of property list items of contents of this_plistfile ¬
 with properties {kind:list, name:"listKey"}
 make new property list item at end of property list items of contents of this_plistfile ¬
 with properties {kind:number, name:"numberKey", value:5}
 make new property list item at end of property list items of contents of this_plistfile ¬
 with properties {kind:record, name:"recordKey"}
 make new property list item at end of property list items of contents of this_plistfile ¬
 with properties {kind:string, name:"stringKey", value:"string value"}
end tell

The System Events library has the basic tools to create a plist file and add elements. Sample code taken from the Mac OS X Automation website:

tell application "System Events"
 -- create an empty property list dictionary item
 set the parent_dictionary to make new property list item with properties {kind:record}
 -- create new property list file using the empty dictionary list item as contents
 set the plistfile_path to "~/Desktop/example.plist"
 set this_plistfile to ¬
 make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
 -- add new property list items of each of the supported types
 make new property list item at end of property list items of contents of this_plistfile ¬
 with properties {kind:boolean, name:"booleanKey", value:true}
 make new property list item at end of property list items of contents of this_plistfile ¬
 with properties {kind:date, name:"dateKey", value:current date}
 make new property list item at end of property list items of contents of this_plistfile ¬
 with properties {kind:list, name:"listKey"}
 make new property list item at end of property list items of contents of this_plistfile ¬
 with properties {kind:number, name:"numberKey", value:5}
 make new property list item at end of property list items of contents of this_plistfile ¬
 with properties {kind:record, name:"recordKey"}
 make new property list item at end of property list items of contents of this_plistfile ¬
 with properties {kind:string, name:"stringKey", value:"string value"}
end tell
迟月 2024-10-09 07:43:43

这里的大多数代码都是将目录结构调整为您需要的数据结构:

use strict; use warnings;
use Data::Plist::XMLWriter;

my @plist;

for my $root_dir (@ARGV) {
    opendir my $ROOT_DIR, $root_dir
        or die $!;

    my @dir_contents = map { s[\.\w\w\w$][]; $_ } 
                       grep { not -d } 
                       readdir $ROOT_DIR;

    push @plist, { colour => $root_dir, name => $_ }
        for @dir_contents;
}

my $writer = Data::Plist::XMLWriter->new;
my $str    = $writer->write( \@plist );

print $str;

这会导致:

$ ls -l YELLOW BLUE
BLUE:
total 0
-rw-r--r-- 1 psilva adm 0 2010-11-12 14:53 ARROW.PNG
-rw-r--r-- 1 psilva adm 0 2010-11-12 14:53 RAZOR.PNG

YELLOW:
total 0
-rw-r--r-- 1 psilva adm 0 2010-11-12 14:53 MARKIN.PNG
-rw-r--r-- 1 psilva adm 0 2010-11-12 14:53 MARKOUT.PNG
$ perl tmp.pl YELLOW BLUE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">
        <array>
                <dict>
                        <key>colour</key>
                        <string>YELLOW</string>
                        <key>name</key>
                        <string>MARKOUT</string>
                </dict>
                <dict>
                        <key>colour</key>
                        <string>YELLOW</string>
                        <key>name</key>
                        <string>MARKIN</string>
                </dict>
                <dict>
                        <key>colour</key>
                        <string>BLUE</string>
                        <key>name</key>
                        <string>ARROW</string>
                </dict>
                <dict>
                        <key>colour</key>
                        <string>BLUE</string>
                        <key>name</key>
                        <string>RAZOR</string>
                </dict>
        </array>
</plist>

Most code here is massaging your directory structure into the data structure you need:

use strict; use warnings;
use Data::Plist::XMLWriter;

my @plist;

for my $root_dir (@ARGV) {
    opendir my $ROOT_DIR, $root_dir
        or die $!;

    my @dir_contents = map { s[\.\w\w\w$][]; $_ } 
                       grep { not -d } 
                       readdir $ROOT_DIR;

    push @plist, { colour => $root_dir, name => $_ }
        for @dir_contents;
}

my $writer = Data::Plist::XMLWriter->new;
my $str    = $writer->write( \@plist );

print $str;

This results in:

$ ls -l YELLOW BLUE
BLUE:
total 0
-rw-r--r-- 1 psilva adm 0 2010-11-12 14:53 ARROW.PNG
-rw-r--r-- 1 psilva adm 0 2010-11-12 14:53 RAZOR.PNG

YELLOW:
total 0
-rw-r--r-- 1 psilva adm 0 2010-11-12 14:53 MARKIN.PNG
-rw-r--r-- 1 psilva adm 0 2010-11-12 14:53 MARKOUT.PNG
$ perl tmp.pl YELLOW BLUE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">
        <array>
                <dict>
                        <key>colour</key>
                        <string>YELLOW</string>
                        <key>name</key>
                        <string>MARKOUT</string>
                </dict>
                <dict>
                        <key>colour</key>
                        <string>YELLOW</string>
                        <key>name</key>
                        <string>MARKIN</string>
                </dict>
                <dict>
                        <key>colour</key>
                        <string>BLUE</string>
                        <key>name</key>
                        <string>ARROW</string>
                </dict>
                <dict>
                        <key>colour</key>
                        <string>BLUE</string>
                        <key>name</key>
                        <string>RAZOR</string>
                </dict>
        </array>
</plist>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文