Perlの練習にAppleScript+Rubyで書いた「IllustratorPackage」をPerlに書き直しているところ。
Macは素直に「IllustratorPackage」を使ってもらえばいいので、PerlではWindowsのことしか考慮してません。
ActivePerlなどをインストールしてからじゃないと動きません。
Macってその辺り楽ですよね。
いまのところエラーログも書き出さないし(コンソールには出ます)、1ファイルしか対応してないけど、順次更新していくつもり。
更新したコードは随時SourceForgeのPersonalForgeにコミットしていきますので、このブログのコードよりもそっちをダウンロードしたほうが幸せになれると思います。
Perl。
use strict;use warnings;
use utf8;
use Encode qw/decode encode/;
use File::Basename; #ファイルパス分割のため
use File::Copy; #ファイルコピーのため
# ファイルシステムの設定(デフォルト:Unix)
fileparse_set_fstype('MSWin32');
my $temp = "";
my $file = $ARGV[0];
my @linkfilepath = ($file); #収集するファイルのパスを入れる配列
my $errcount = 0;
#デスクトップに現在時刻名のディレクトリを作成
sub mkdir_localtime {
my ($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime();
$year += 1900;
$mon += 1;
my $time = "$year-$mon-$mday $hour-$min-$sec";
mkdir $ENV{USERPROFILE}.encode('CP932','/デスクトップ/').$time
or die "Can't make dir: $!";
return $ENV{USERPROFILE}.'/デスクトップ/'.$time; #戻り値はencodeしない
}
open my $fh, '<', $file
or die "Can't open $file: $!";
while (my $line = <$fh>) {
if ($line =~/Adobe Illustrator CS2/){
die "this file maked Adobe Illustrator CS2."
} elsif ($line =~ /^<\/x:xmpmeta>/ ){
last
} elsif ($line =~ /^\s+?<stRef:filePath>(.+?)<\/stRef:filePath>/) {
push(@linkfilepath,$1);
}
}
close $fh; #開いたら閉じる
if ($#linkfilepath > -1) { #配列が空じゃなかったら
my $newdir = mkdir_localtime();
my $newdir_CP932 = encode('CP932', $newdir); #encodeした変数を準備
foreach my $item (@linkfilepath){
my $item_CP932 = encode('CP932', $item); #encodeした変数を準備
if (-f $item_CP932){ #encode_CP932した結果ファイルが存在していたら(-fでファイルの存在確認)
unless (copy($item_CP932, $newdir_CP932)){ #copyに失敗したら
print "Can't copy from \"$item_CP932\" to \"".$newdir_CP932."\": $!";
}
} elsif (-f $item){ #encode_CP932しないでファイルが存在していたら
unless (copy($item, $newdir_CP932)){ #copyに失敗したら
print "Can't copy from \"$item\" to \"".$newdir_CP932."\": $!";
}
} else { #ファイルが存在しなかったらIlustratorファイル直下から探す
my $filedir = dirname($file); #Illustratorファイルの親ディレクトリ取得
my $linkpath = basename($item); #配置ファイルの名前を取得
my $linkfile = $filedir."/".$linkpath; #結合してファイルパスにする
$linkfile = decode('utf8', $linkfile); #内部文字列(utf8)にdecodeする
my $linkfile_CP932 = encode('CP932',$linkfile);
unless (copy($linkfile_CP932, $newdir_CP932)){
print "Can't copy from \"$linkfile_CP932\" to \"$newdir_CP932\" ";
$errcount ++;
}
}
}
}
if ($errcount == 0){
print "Successful!";
} else {
print "Done! but can't collected files : $errcount files";
}
PersonalForgeはここ。