Skip to content

Instantly share code, notes, and snippets.

@aslilac
Forked from rsms/extract-aperture-images.js
Created August 9, 2012 23:19
Show Gist options
  • Save aslilac/3308960 to your computer and use it in GitHub Desktop.
Save aslilac/3308960 to your computer and use it in GitHub Desktop.
Aperture library extractor -- copies all source images from a apimportgroup inside a aplibrary into any folder.
#!/usr/bin/env node
// Aperture library extractor -- copies all source images from a apimportgroup inside a aplibrary
// into any folder.
//
// Step 1. Create a new folder somewhere, for instance in ~/ap-rescue
//
// Step 2. Run this program, giving it the apimportgroup as the first argument and your
// destination directory as the second argument.
//
// e.g. node extract-aperture-images.js ~/'Pictures/Aperture Library 2000.aplibrary/Arkivet.approject/2006-01-15 @ 03:57:21 PM - 1.apimportgroup' ~/ap-rescue
//
// Step 3. Wait for the script to finish and watch for any errors.
//
var fs = require('fs'), Path = require('path');
var exec = require('child_process').exec;
if (process.argv.length < 4) {
console.error('usage: '+process.argv[1]+' <srcdir> <dstdir>');
process.exit(1);
}
function firstCharIsNotADot(fn) { return fn[0] !== '.'; }
function shellEscape(s) { return s.replace(/'/g, "\\'"); }
var basedir = process.argv[2];
var dstDir = process.argv[3];
var dirs = fs.readdirSync(basedir).filter(firstCharIsNotADot);
var apfnRE = /\.(?:apfile|apmaster|apversion)$/;
var filesToCopy = [];
dirs.forEach(function (dir) {
var dirname = basedir + '/' + dir;
var fns = fs.readdirSync(dirname);
var filenames = fns.filter(function (fn) {
return fn !== 'Previews' && fn !== 'Thumbnails' && !fn.match(apfnRE);
}).map(function (fn) {
return dirname + '/' + fn;
});
if (filenames.length !== 0)
filesToCopy = filesToCopy.concat(filenames);
});
function copyNext(callback, _state) {
if (_state === undefined) _state = {};
var filename = filesToCopy.shift();
if (filename === undefined)
return callback();
var dstFilename = dstDir + '/' + Path.basename(filename);
if (fs.existsSync(dstFilename)) {
dstFilename = dstDir + '/' + Path.basename(Path.dirname(filename)).toLowerCase() + Path.extname(filename);
if (fs.existsSync(dstFilename)) {
console.error('Warning: Ingoring already existing image \''+shellEscape(dstFilename)+'\'');
return process.nextTick(function () { copyNext(callback, _state); });
}
}
var shellCmd = "/bin/cp -nPp '"+shellEscape(filename)+"' '"+shellEscape(dstFilename)+"'";
exec(shellCmd, function (error, stdout, stderr) {
if (error !== null)
console.error('Error: ' + stderr + ' (err: '+error+
', filename: \''+shellEscape(filename)+'\')');
copyNext(callback, _state);
});
}
copyNext(function () {
console.log('done');
});
{
"name": "ap-extract",
"version": "1.0.0",
"description": "copies all source images from a apimportgroup inside a aplibrary into any folder",
"main": "extract-aperture-images.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"lib": {
"ap-extract": "extract-aperture-images.js"
},
"repository": {
"type": "git",
"url": "git://gist.github.com/3308960.git"
},
"keywords": [
"aperture",
"extractor"
],
"author": "rsms",
"license": "BSD"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment