An iPhoto library rotation script

Jan 11, '03 12:27:42PM

Contributed by: hmelton

My wife and I travel and photograph a lot. She is ahead of me with about 10,000 photos since I gave her a digital camera last year. iPhoto gets very slow with such large libraries. To allow easy switching FROM one library to another, I write a quick Perl script, named it photolib.command and put it in her dock. It is a non-GUI but relatively quick way have multiple libraries.

The first time it runs, you name your current library. FROM that point on "iPhoto Library" is just an alias to the real library folder.

To create a new blank library DELETE the "iPhoto Library" alias AFTER you have named your current library and re-run iPhoto. Duplicating and deleting library folders is done with with Finder.

Read the rest of the article for the script ... and note that this may very well not work with iPhoto2 when it's released in the near future!

Enter in your favorite editor:

#!/usr/bin/perl -w
use strict;

open(PS,"ps -auxjwww|");
my @processes=<PS>;
close PS;
if (grep(/iPhoto.app/,@processes)){
print "iPhoto is running. It is too dangerous to move the library.\n";
print "Quitting now.\n";
exit;
}
my $pictures="$ENV{'HOME'}/Pictures";
if (-l "$pictures/iPhoto Library"){
open(LS,"ls -l $pictures/iPhoto\\ Library|");
my $currentlib=<LS>;
close LS;
$currentlib =~ s/.*-> //;
chomp $currentlib;
print "The current library is : $currentlib\n";
} else {
if (-d "$pictures/iPhoto Library"){
print "The current library has not been named.\n";
print "Enter a one-word name for it: ";
my $newname=<STDIN>;
chomp $newname;
unless ($newname){
print "You chose no new name. Exiting.\n";
exit;
}

print "You have chosen to name the library \'$newname\'. Ok(yes/no)?\n";
my $response=<STDIN>;
if ($response =~ /yes/i){
if (-d "$pictures/$newname"){
print "A folder or library already exists with that name. Exiting.\n";
exit;
}
print "Now saving 'iPhoto Library' as \'$newname\'.\n";
system("mv $pictures/iPhoto\\ Library $pictures/$newname");
} else {
exec($0);
}
}
}

print "Available libraries are :\n";
open(DIR,"ls -l $pictures/*/Library.cache|");
my @librarylist=<DIR>;
close DIR;
my @libraries;
while(@librarylist){
my $lib=shift(@librarylist);
$lib =~ s|^.*$pictures/||;
$lib =~ s|/Library.cache||;
chomp($lib);
next if ($lib eq "iPhoto Library");
print "\t$lib\n";
push (@libraries,$lib);
}
print "Enter the library name to use, or just return to do nothing: ";
my $newlib=<STDIN>;
chomp($newlib);
if ($newlib){
if (-d "$pictures/$newlib"){
if (-l "$pictures/iPhoto Library"){
unlink "$pictures/iPhoto Library";
}
if (-d "$pictures/iPhoto Library"){
print "Sorry, current library was not yet named.\n";
exit;
}
system("cd $pictures;ln -s $newlib iPhoto\\ Library");
print "Library changed to $newlib\n";
} else {
print "Library $newlib does not exist.\n";
}
}
print "To rename, duplicate or DELETE a library, go to the Pictures\n";
print "folder with the Finder and do it.\n";
Name this with a ".command" extension and make sure it's executable.

Comments (3)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030111092742655