<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NSCoriolisBlog &#187; Perl</title>
	<atom:link href="http://blog.coriolis.ch/category/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.coriolis.ch</link>
	<description>Just another Coriolis weblog</description>
	<lastBuildDate>Sat, 13 Mar 2010 09:43:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Perl is fun</title>
		<link>http://blog.coriolis.ch/2008/02/19/perl-is-fun/</link>
		<comments>http://blog.coriolis.ch/2008/02/19/perl-is-fun/#comments</comments>
		<pubDate>Tue, 19 Feb 2008 22:46:31 +0000</pubDate>
		<dc:creator>Stephan</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[iphoto]]></category>
		<category><![CDATA[MacOSX]]></category>

		<guid isPermaLink="false">http://www.coriolis.ch/wp/?p=1</guid>
		<description><![CDATA[When I reinstalled my iMac 24&#8243;, I restored a backup of the iPhoto Library. But the software I&#8217;m working on (eXaPhoto Publisher) couldn&#8217;t import correctly an iPhoto album: a lot of pictures were missing. The imedia browser had the same problem, but iPhoto seems smart enough to find the pictures. When you manage yourself your [...]]]></description>
			<content:encoded><![CDATA[<p>When I reinstalled my iMac 24&#8243;, I restored a backup of the iPhoto Library.</p>
<p>But the software I&#8217;m working on (<a  href="http://exanetworks.com/" target="_blank" rel="external" title="eXaPhoto Publisher">eXaPhoto Publisher</a>) couldn&#8217;t import correctly an iPhoto album: a lot of pictures were missing. The <a  href="http://www.karelia.com/imedia/" target="_blank" rel="external" title="imedia browser">imedia browser</a> had the same problem, but iPhoto seems smart enough to find the pictures.</p>
<p>When you manage yourself your picture, iPhoto maintains a dummy structure inside the &#8220;iPhoto Library&#8221;. This structure contains links (aliases) to the original files.</p>
<p>I don&#8217;t know how it happened, but all the iPhoto aliases were marked as being on the backup volume. So instead of reimporting all my pictures and risk losing all my albums, I&#8217;ve written a Perl script to update all the aliases in &#8220;iPhoto Library/Originals&#8221; so they point now to the right location.</p>
<p><span id="more-1"></span></p>
<p>Below is the script: it shows how to manage files and aliases in Perl.</p>
<pre><code>
#!/usr/bin/perl

# script to update all iPhoto aliases so they point to the right location
# iPhoto Library was restored from a backup and all aliases were pointing to the bad volume ie.
# instead of "/Volumes/iMac/...", they were pointing to "/Volumes/Bk_Imac"
# Stephan Burlot, 2008-02-14

use Mac::Memory;
use Mac::Resources;
use Mac::MoreFiles;
use Mac::Files;
use Data::Dumper;

my $path;
my $alias;
my $alis;
my $bad_volume = "Bk_iMac";
my $good_volume = "iMac";
my $updated_files = 0;
my $total_files = 0;

sub update_alias {

  my ($path, $data) = @_;

  if (-d $path) {
    print "Parsing directory $path\n";
    return 0;
  }

  $total_files++;
  $finfo = FSpGetFInfo($path);
  if (($finfo-&gt;fdFlags &amp; 0x8000) == 0) {
    print "$spec is a file\n";
    return 0;
  }

  $RF = FSOpenResourceFile($path, "rsrc", fsRdWrPerm) ;
  if (!defined($RF)) {
    warn "$path has no resource";
    return 0;
  }

  if (defined($RF)) {

    # get resource by index; get first "alis" resource
    $alis = GetIndResource('alis', 1) ;
    if (!defined($alis)) {
      warn $Mac::Errors::MacError;
      return;
    }

    $fullpath = "";
    $index = 0;

    # construct the full path
    while ($info = GetAliasInfo($alis, $index++)) {
      $fullpath = "/" . $info . $fullpath ;
    }

    # add the volume name
    $volname = GetAliasInfo($alis, asiVolumeName);
    $fullpath = "/Volumes/" . $volname . $fullpath;

    # if the file already exists, the alias is OK
    if (-f $fullpath) {
      print "Alias $path is already OK\n";
    } else {

      # replace bad volume with the good one
      ($target = $fullpath) =~ s/$bad_volume/$good_volume/;

      # double check the new target exists
      if (-f $target) {

        $new_alias = NewAlias($target);
        $updated = UpdateAlias($target, $alis);
        print "$fullpath was updated to $target\n";
        if ($updated) {
          $updated_files++;
          ChangedResource($alis);
          WriteResource($alis);
          UpdateResFile($RF);
        }
      } else {
        print "target $target wasnt found\n";
      }
    }
    CloseResFile($RF);
  }

  return 0;
}

my $path = '/Users/stephan/Pictures/iPhoto Library/Originals/';

FSpIterateDirectory($path, 0, \&amp;update_alias, nil);

print "Updated $updated_files files of a total of $total_files\n";
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.coriolis.ch/2008/02/19/perl-is-fun/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk (enhanced)

Served from: www.coriolis.ch @ 2010-09-07 14:03:26 -->