Monday, July 18, 2016

osx command line power and sorting folders in perl

Talking with some fellow greybeard geeks, I realize I could be consider python instead of PHP as a scripting "light file management and text manipulation tasks" tool, or maybe node.js (native manipulation of JSON definitely appeals to me)

But PHP it is for me for now. I used that php recursive file iterator from the other week to do photo wrangling. I've been going through two decades of digital photos and picking out the dozen best photos from each year - it's a great way to regain a sense of time and place of those years that I might otherwise lose. I've never embraced a photo management program (I tried iPhoto for a while but it's really not geared for collections as big as mine) so my photos are in folders of the form "data/photos/YYYY/some_date_or_descriptor". When I copied each year's best into a new folder, the "metadata" of the folder descriptor was lost, so I made a kind of hacky PHP script to find files that had the matching name in the original folder (considering a matching file size a match) and renaming the chosen out file to embed a mangled form of the extra path info. (Most of the folders had some kind of sortable date at the head of the name, though the actual format varied from year to year.)

There were two small OSX tricks I used, nothing too obscure: one was leveraging how dragging files to terminal copies their full paths, which I would then use as arguments for my script. The other was using PHP's shell_exec() to call "open", which for an image file pops open preview. With those two things I was able to construct a crude UI, so for ambiguous file names (where file size didn't match, because I rotated things) it could open up the two images and say "are these the same file?" (Also, "open ." is the easiest way to pop up the current working directory in a Finder window)

Of course, folder and file name hackery is kind of an ugly way to deal with photo metadata, but over the years it's the only thing I've been able to trust. In a similar note, I've noticed a whole bunch of music missing from iTunes. Not sure what happened, but I think I may have to resort to similar easy-to-backup, possible-to-verify handling for my music and playlists, rather than relying on an endless "smart playlist" for getting back to my new music.

For my homebrew blog, I use a drag and drop photo uploader script combined with some ImageMagick resizing. I so wish I had written it many years ago; my blog is one of my most reliable archives, and my new scripts' method of generating viewable-sized versions - but with links to the fullsize originals - would have kept some big image files safe that I've since lost track of. C'est la vie.

Anyway, the wrapper script for the image processing and construction of img tags and links to fullsize versions is old Perl. Before now I didn't have much of a reason to sort file order (usually I'd upload just one or two images at once) but now I do, so I had to lookup code like the following:

$imagetempDir = ".";
opendir(DIR,$imagetempDir) or print "can't open $dirname";
my @files = sort { $a cmp $b } readdir(DIR);
while ( my $f = shift @files ) {
    if($f ne "." && $f ne ".."){
        print "look at $f<br>\n";

I'm putting the code here on the offchance someone else finds it useful. I'm also putting it on my old Perl cheatsheet... kept in a homebrew database system I've been using for over a decade. These days, Simplenote probably does a better job for this kind of stuff, but I didn't have Simplenote and Dropbox back then - they are pretty awesome though.

No comments:

Post a Comment