iView home page
iView Multimedia Forum Index

FAQ FAQ     SearchSearch     MemberlistMemberlist     UsergroupsUsergroups    RegisterRegister  
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Script to rename sidecars

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    iView Multimedia Forum Index -> AppleScript
View previous topic :: View next topic  
Author Message
jrgeoffrion



Joined: 21 Sep 2004
Posts: 9

PostPosted: Tue Aug 30, 2005 2:40 pm    Post subject: Script to rename sidecars Reply with quote

This was posted in the VB Script forum... Stupid me, it looks like the Mac doesn't support VB Script. Anyone can help me.

I have both CaptureOne LE and iView Media Pro (both for Mac). Because CaptureOne LE doesn't have a batch rename function, I use iView to sort and batch rename the images. HOWEVER, when the RAW files are adjusted (exposure, WB, etc.) in CaptureOne LE,
the software creates a sidecar setting file with the same name as the RAW file but ending in .plist located in a sub-sub folder.

For example, if I have a Canon 20D RAW file is called F510b.cr2, the sidecar setting file will be:
[current RAW directory]/Capture One Settings/ImageSettings/F510b.cr2.plist.

This means that if my files are corrected in CaptureOne LE and renamed in iView, I lose the settings. Even worse, settings of one images could be applied to the wrong image if the old sidecar has the same name as a new and different RAW file.

I would like a VBScript that batch renames (preferably string prefix + counter) both the RAW file (.cr2) AND sidecar (.plist) located in the sub sub directory. Does something like this already exists? If not, the script should be pretty simple to write (I think).
Back to top
View user's profile Send private message
Mike



Joined: 28 Feb 2005
Posts: 94
Location: Vancouver BC Canada

PostPosted: Tue Aug 30, 2005 5:02 pm    Post subject: Will perl do? Reply with quote

I have a perl script that I use for this purpose. You just fire up Terminal, cd to the appropriate directory and type renumber -n 1000 (or whatever number you want the series to begin with). It keeps the existing file prefix and renumbers the files. It can even handle files in subdirectories without creating duplicate numbers. The downside is that you have to run it from Terminal, not select it from the AppleScript menu. If this will meet your needs I'll be happy to post it -- will it do the job for you?
_________________
iView 3.0.2 and Photoshop CS2 (9.0) on Mac OS X 10.4.5.
Back to top
View user's profile Send private message Visit poster's website
jrgeoffrion



Joined: 21 Sep 2004
Posts: 9

PostPosted: Tue Aug 30, 2005 5:13 pm    Post subject: Reply with quote

Mike,

Thanks a bunch. That would work. Do you have the Capture One settings directory and file extension (.plist) hard coded in the Script? Will this rename using the order in which the image are in the catalog?

Can't wait to try it out.
Back to top
View user's profile Send private message
Mike



Joined: 28 Feb 2005
Posts: 94
Location: Vancouver BC Canada

PostPosted: Tue Aug 30, 2005 6:11 pm    Post subject: Reply with quote

Here it is. Note that this script knows nothing at all about iView, Capture One, photos, or anything else... it is a generic utility for recursing folder trees and closing the gaps in numbered file sequences. In particular, since the script knows nothing about iView, if you run it on a batch of photos that have already been catalogued iView will get hopelessly confused and not know what thumbnails go with what files. The way that I deal with this is to export my annotations, remove the images from the catalog, renumber them with my script, and re-import.

All files remain in their original folders and the new number sequence is based on the old number sequence, not the folder structures. In other words, if before renumbering you had

Photos/alpha/CRW_0157.CRW
Photos/alpha/CRW_0157.THM
Photos/alpha/IMG_0165.JPG
Photos/beta/IMG_0164.JPG

and you run renumber -p CRW -p IMG Photos then you will end up with

Photos/alpha/CRW_0157.CRW
Photos/alpha/CRW_0157.THM
Photos/alpha/IMG_0159.JPG
Photos/beta/IMG_0158.JPG

If you wanted all the files to have the same prefix, use the capital-P option (renumber -p CRW -P IMG Photos) for the prefix that you want to use.


To 'install' the script, copy it to the pasteboard, fire up Terminal, and type the following:

pbpaste > /usr/bin/renumber
chmod +x /usr/bin/renumber
rehash

And always remember that shellscripts are powerful and there is no 'undo' feature. If you run this script from the wrong directory the file renumbering can have unexpected consequences, including messing up your catalogue or rendering your entire computer system unusable!

Sorry for all the warnings, but I'd really hate for a novice to try this out thinking "what can go wrong?" only to discover that the answer is "a LOT". Unix is not for the faint of heart.

Code:
#!/usr/bin/perl

#
# This script is Copyright (C) 2005 Mike Gobbi
# it may be freely copied and modified so lon as
# (1) this copyright notice remains intact, and
# (2) any changes are clearly identified
#
# This script comes with no warranties or guarantees.
# Use at your own risk, and if you have any concerns
# then back up your data before running it.
#
# for usage instructions run "renumber -h"
#

$verbose = 0;
$test = 0;
$digits = 4;
$initial = -1;
@prefixes = ();

while ($ARGV[0] =~ /^-(.*)/) {
  shift;
  if ($1 eq "h") { printUsage(); }
  elsif ($1 eq "d") { $digits = shift; }
  elsif ($1 eq "n") { $initial = shift; }
  elsif ($1 eq "p") { push @prefixes, shift; }
  elsif ($1 eq "P") { $newPrefix = shift; push @prefixes, $newPrefix; }
  elsif ($1 eq "t") { $test = 1; }
  elsif ($1 eq "tu") { $test = 1; $undo = 1; }
  elsif ($1 eq "v") { $verbose = 1; }
  else { printUsage(); }
}
$prefixPattern = "[^-_]+";
$prefixPattern = join "|", @prefixes if $#prefixes > -1;

sub printUsage() {
  print "usage: renumber [flags] directory ...\n";
  print "flags:\n";
  print "  -d NUM  Use NUM digits (i.e., -d 4 gives IMG_0001.JPG)\n";
  print "  -n NUM  Start numbering with NUM\n";
  print "  -p PRE  Include files with prefix\n";
  print "  -P PRE  Include files with prefix AND force all files to use this prefix\n";
  print "  -t      Test (print commands but do nothing)\n";
  print "  -tu     Test undo (print commands that will undo a execution)\n";
  print "  -v      Verbose (print commands as they are executed)\n";
  print "examples:\n";
  print "  renumber .\n";
  print "  renumber ~/Pictures\n";
  print "  renumber -p IMG -p CRW ~/Pictures\n";
  print "  renumber -P IMG -p CRW ~/Pictures\n";
  print "caveats:\n";
  print "  Files to be renumbered MUST include hyphen or underscore\n";
  print "  followed by a number (i.e., IMG_1024.JPG).  Files not\n";
  print "  matching this pattern will be silently ignored\n";
  exit;
}

@directories = @ARGV;
#@directories = (".") if $#directories == -1;
printUsage() if $#directories == -1;
@files = ();
%prefixesFound = ();
foreach (@directories) {
  &scanDir($_);
}
exit 1 if $abort;
@files = sort numericComparison @files;
@found = keys %prefixesFound;
if ($#prefixes > -1) {}
elsif ($#found == 0) { @prefixes = @found; }
else {
  print "multiple file prefixes found (";
  print join ", ", sort @found;
  print ")\n";
  print "use -p to explicitly name prefixes to merge\n";
  exit(1);
}
if ($initial < 0) {
  $initial = $1 if $files[0] =~ /[^-_]*[-_] *([0-9]+)/;
}
if ($undo) { print "# execute these commands to undo the renumbering\n"; }
&renumberList(@files);

sub scanDir($) {
  my $dir = shift;
  opendir DIR, $dir;
  my @files = readdir DIR;
  my $flag = 0;
  foreach (@files) {
    if (/^\./) {
      # do nothing -- skip hidden files
    } elsif (-d "$dir/$_") {
      &scanDir("$dir/$_");
    } else {
      $flag = 1 if &scanFile("$dir/$_");
    }
  }
  if ($flag && ! -w $dir) {
    print STDERR "error: $dir is read-only\n";
    $abort = 1;
  }
  closedir DIR;
}

sub scanFile($) {
  my $file = shift;
  return 0 if !($file =~ /(.*)\/([^\/]*)/);
  my $reversed = "$2:$1";
  return 0 if !($reversed =~ /^($prefixPattern)[_-][ 0-9]+/);
  return 0 if !($reversed =~ /^([^-_]+)[-_][ 0-9]+/);
  $prefixesFound{$1} ++;
  push @files, $reversed;
  return 1;
}

sub numericComparison($$) {
  my ($a, $b) = (shift, shift);
  return $a cmp $b if !($a =~ /([^0-9]*)([0-9]+)(.*)/);
  my ($ah, $an, $at) = ($1, $2, $3);
  return $a cmp $b if !($b =~ /([^0-9]*)([0-9]+)(.*)/);
  my ($bh, $bn, $bt) = ($1, $2, $3);
  my $t = $ah cmp $bh;
  return $t if $t;
  $t = $an <=> $bn;
  return $t if $t;
  return &numericComparison($at, $bt);
}

sub renumberList(@) {
  my $previousNumber = -1;
  my $current = $initial - 1;
  foreach (@_) {
    next if !(/([^-_]*[-_]) *([0-9]+)(.*)/);
    my ($prefix, $number, $trailer) = ($1, $2, $3);
    $current++ if $previousNumber != $number;
    my $newName = sprintf("%s%0${digits}d%s", $prefix, $current, $trailer);
    &renameFile($_, $newName);
    $previousNumber = $number;
  }
}

sub renameFile($$) {
  my ($old, $new) = (shift, shift);
  die "internal error" if !($old =~ /([^:]+):([^:]+)/);
  $old = "$2/$1";
  die "internal error" if !($new =~ /([^:]+):([^:]+)/);
  $new = "$2/$1";
  $old =~ s/([ *'"\[\]\{\}\|\?<>])/\\$1/g;
  $new =~ s/([ *'"\[\]\{\}\|\?<>])/\\$1/g;
  $old =~ s/^.\///;
  $new =~ s/^.\///;
  $new =~ s/\/[A-Za-z0-9]+([_-][^\/]+)/\/$newPrefix\1/ if -n "$newPrefix";
  $new =~ s/^[A-Za-z0-9]+([_-][^\/]+)/$newPrefix\1/ if -n "$newPrefix";
  return if $old eq $new;
  if ($undo) { print "mv $new $old\n"; }
  elsif ($test || $verbose) { print "mv $old $new\n"; }
  return if $test;
  if (-f $new) { die "fatal error: $new already exists"; }
  elsif (!rename($old, $new)) { die "fatal error: unable to rename $old to $new"; }
}


_________________
iView 3.0.2 and Photoshop CS2 (9.0) on Mac OS X 10.4.5.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    iView Multimedia Forum Index -> AppleScript All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



  © iView Multimedia Ltd. | All Rights Reserved Privacy Policy | Copyright | Site Map