Skip to content

iPhoto Library Manager (Revisited)

For anyone interested, attached below (in the Extended Entry) is the script I run nightly from cron that informs me by email when my current iPhoto Library is getting big enough to consider taking an action with iPhoto Library Manager to create a new one.

A couple of notes:

(1) Yes, the script uses the CLI version of PHP. I’m much more familiar with the PHP language than I am with tools like Perl.

(2) The script uses the knowledge that I name my iPhoto Libraries according to the form “iPhoto-Library-20030521”, so that the last entry in a directory listing is the currently used Library (and the one whose size should be calculated).

(3) I’ve removed the PHP delimiters from the script example, to prevent execution when you view it (since my blog is parsed by PHP).

#! /usr/local/bin/php
[BEGIN PHP]

$LibraryLocation  = '/Users/mhenders/Pictures';
$LibraryPrefix    = 'iPhoto-';
$MaxDesiredSize   = 600; // in megabytes
$to         = '[email protected]';
$subject      = 'iPhoto Library Size Warning [makalup1]';

function getSubDirs($dir_name)
{
  $subdirectories = array();
  $files = array();

  if(is_dir($dir_name) and is_readable($dir_name)) {
    $d = dir($dir_name);
    while (false !== ($f = $d->read())) {
      // skip . and ..
      if (('.' == $f) || ('..' == $f)) {
        continue;
      }
      if (is_dir("$dir_name/$f")) {
        array_push($subdirectories,"$dir_name/$f");
      } else {
        array_push($files,"$dir_name/$f");
      }
    }
    $d->close();
  }
  return $subdirectories;
}

$subdirectories = getSubDirs($LibraryLocation);

foreach( $subdirectories as $subdir ) {
    if(ereg($LibraryLocation."/".$LibraryPrefix,$subdir))
    {
      $output = exec("du -sk ".$subdir);
      ereg('([0-9]+)',$output,$size);
      $size = $size[0];
      $foundLibrary = $subdir;
    }
}

$checkSize = round($size/1000,0); // convert KB to MB

if($checkSize > $MaxDesiredSize)
{
  $message  = "iPhoto Library Size Warningn";
  $message .= "===========================nn";
  $message .= "Library: $foundLibraryn";
  $message .= "   Size: $checkSize MBnn";
  $message .= "(You specified to be notified when the ";
  $message .= "library size is greater than $MaxDesiredSize MB.)";

  $subject .= " [$checkSize MB]";

  mail($to,$subject,$message);
}

[END PHP]
Published inTechnology

Be First to Comment

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.