these temp nib files are annoying
[AutomatorExifMover.git] / main.command
blob8e04ba2dacb010c7b3ef4735cdf16d6838e3ccfa
1 #!/usr/bin/perl
3 # main.command
4 # AutoExifMover
6 # Created by Pierre Andrews on 01/07/2007.
7 # Copyright 2007 Pierre Andrews. All rights reserved.
9 #use lib "/usr/bin/lib/";
11 # add our 'lib' directory to the include list BEFORE 'use Image::ExifTool'
12 my $exeDir;
13 BEGIN {
14 # get exe directory
15 $exeDir = ($0 =~ /(.*)[\\\/]/) ? $1 : '.';
16 # add lib directory at start of include path
17 unshift @INC, "$exeDir/lib";
20 use Image::ExifTool;
21 use File::Path;
22 use File::Basename;
23 use File::Copy;
25 while(<>) {
26 chomp;
28 if(-e $_) {
29 my $exifTool = new Image::ExifTool;
30 my $pattern = $ENV{'pathPattern'};
31 if(!$pattern) {
32 $pattern = "%Y_%m_%d/";
34 $exifTool->Options(DateFormat => $pattern);
36 my $file = $_;
37 my $name;
38 my $dir;
39 my $suffix;
40 my $with_basename=0;
41 ($name,$dir,$suffix) = fileparse($file,qr/\.[^.]*$/);
42 my $destPath = $ENV{'directoryPath'};
43 if(!$destPath) { $destPath = $dir; }
44 my $info = $exifTool->ImageInfo($file, 'DateTimeOriginal');
45 my $path = $$info{'DateTimeOriginal'};
46 if(!$path && $pattern !~ /%[A-Za-z]/) {
47 $path = $pattern;
49 if($path) {
50 while($path =~ /:([a-zA-Z]+):/g) {
51 if($1 =~ /basename/i) {
52 $with_basename=true;
53 $path =~ s/:basename:/$name/g;
54 } elsif($1 =~ /ext/i) {
55 $path =~ s/:ext:/$suffix/g;
56 } else {
57 my $info = $exifTool->ImageInfo($_, "$1");
58 if($$info{"$1"}) {
59 my $i = $$info{"$1"};
60 my $x = $1;
61 $i =~ s/ /_/g;
62 chomp($i);
63 $path =~ s/:$x:/$i/g;
64 } else {
65 $path =~ s/:$1://g;
69 $path =~ s/[^A-Za-z0-9_\/.-~]/_/g;
70 $path = $destPath.'/'.$path;
72 $homedir=`ksh -c "(cd ~ 2>/dev/null && /bin/pwd)"`;
73 chomp($homedir);
74 $path =~ s/^~/$homedir/;
76 ($new_name,$new_dir,$new_suffix) = fileparse($path,qr/\.[^.]*$/);
77 if($new_name && !$with_basename) {
78 $path = $new_dir.'/'.$new_name.$new_suffix;
80 if(!$new_name) {
81 $path .= $name.$suffix;
82 $new_name = $name;
83 $new_suffix = $suffix;
85 if(!$new_suffix || $new_suffix!=$suffix) {
86 $path .= $suffix;
89 if(!$ENV{'test'}) { mkpath($new_dir); }
90 if(!$ENV{'overwrite'}) {
91 if(-e $path) {
92 if($path !~ /:cnt:/i) {
93 $path =~ s/(\.[^.]*)$/_:cnt:$1/;
95 my $local_cnt = 1;
96 $new_path = $path;
97 $new_path =~ s/:cnt:/$local_cnt/g;
98 while(-e $new_path) {
99 $local_cnt++;
100 $new_path = $path;
101 $new_path =~ s/:cnt:/$local_cnt/g;
103 $path = $new_path;
105 $path =~ s/_+/_/g;
106 $path =~ s/_:cnt://g;
109 if(!$ENV{'test'}) {
110 if($ENV{'action'} == 'move') {
111 move($file,$path);
112 } else {
113 copy($file,$path);
116 print $path."\n";