2010-11-12 71 views
1

我有几个图像文件夹,并且想创建一个包含文件名称和文件夹名称的plist。使用Automator/AppleScript/Perl创建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或自动机或其他编程语言?我在7个文件夹中有大约60个图像,并且手动输入所有内容将是一件非常痛苦的事情。

谢谢

回答

2

这里大部分代码按摩你的目录结构到你需要的数据结构:

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> 
+0

数据::的plist :: XMLWriter不会在我的系统上建立。使测试保持失败。我相信你的解决方案的作品我会继续尝试。 – joec 2010-11-16 10:36:29

4

系统事件库具有创建plist文件和添加元素的基本工具。从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