Create Plist using Automator/AppleScript/Perl
-
09-10-2019 - |
문제
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
해결책
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>
다른 팁
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
제휴하지 않습니다 StackOverflow