Created
August 9, 2012 07:16
-
-
Save rsms/3301917 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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: Ignoring 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'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment