Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Friday, June 05, 2009

My 1mm-thick PDA backup system

You may have heard of the "Hipster PDA". It started as a joke that a bunch of notecards could be used like a PDA without all the electricity and technology. Then it caught on. There are elements of this that I like (particularly the creativity and do-it-yourself-ness), but it wasn't what I wanted.

The problem that motivated me: Every once in a while my PDA runs out of charge and/or loses all its data. As everything is backed up on my laptop, this is mainly just inconvenient. But when I suddenly need some vital bit of information that is on said PDA, it suddenly shifts to serious nuisance.

My first solution was to simply print out my calendar (which I keep in the plain text remind format) in 4-up form, fold it up, and keep it in my back pocket. And I found that I liked this because it is often easier to pull out and immediately access the information that I want. (It is particularly useful when going on a trip and having flights, times, hotels, and rental car places all listed on a little rectangle of paper.)

This eventually developed into a full script. This script currently collects the following things into a single text file. 0) The current date, to ensure freshness. 1) The output of my remind calendar, excluding boring things. 2) A little address book. I started by extracting specific contact information for specific people from the OS X Address Book via a little command line program called contacts. Then I wised up and started extracting everyone who has an asterisk after their name, allowing me to easily switch people in and out of my short list. (This is something I would like to automate someday.) 3) Some things that I have hard-coded into the program, including public transportation information, phone numbers for taxis, directions to places that I rarely go to, and business hours for a few places. 4) A tiny calendar. I initially thought of setting this up to grab the last, current, and next months from the UNIX cal program, but then I thought of the ingenious thumb calendar which allows a bunch of months to be compactly fit into a small area. And I realized that I could make my own. 5) And finally, my shopping list as extracted from my todo.txt to-do list.

[The only thing that I would like to get on there that I haven't figure out yet is a ruler, along the edge. This will likely require monkeying with PostScript or something.]

Here's the pared down script:

#!/usr/bin/perl -w

$fname="/Users/Surly/remind.txt";
open(INFO,$fname);
@lines = ;
close(INFO);
$outfile="hpda.txt";

open(OUTFO, ">$outfile");

$datestamp=`/bin/date "+%y%m%d"`;
print OUTFO $datestamp;


$i=0;
while($lines[$i])#assuming we initially have a numbered subtitle
{ $rem=(($lines[$i]=~/^rem/)|($lines[$i]=~/^REM/));
$pass=((($lines[$i]!~/^[;#\n]/)&$rem)&($lines[$i]!~/growlnotify/));
$pass=$pass & ($lines[$i]!~/boring/);

if ($pass)
#ignore comments and growlnotifys and commands and only pass REMs
{$lines[$i]=~s/^rem //;$lines[$i]=~s/^REM //;
$lines[$i]=~s/ msg/:/;$lines[$i]=~s/ MSG/:/;
print OUTFO $lines[$i];
}
$i++;
}

print OUTFO "===contacts=========\n";
$a1=`contacts -H -f "%n %mp %wp %hp" Cleo`;
$a1=~s/-/./g; print OUTFO $a1;
$a1=`contacts -H -f "%n %wp %op" Popeye`;
print OUTFO $a1;
$a1=`contacts -H -f "%n %mp %e" Betty`;
print OUTFO $a1;
$a1=`contacts -H -f "%n %mp %wp %op %a" Snidely`;
$a1=~s/\n\n/\n/g; print OUTFO $a1;
$a1=`contacts -H -f "%n %mp" "*"`;
$a1=~s/-/ /g;print OUTFO $a1;
$a1=`contacts -H -f "%n %mp %a" God`;
$a1=~s/\n\n/\n/g; print OUTFO $a1;

print OUTFO "===random stuff====\n";
print OUTFO "The Hamburglar's place: Practice, practice, practice, and take a left.\n";
print OUTFO "Library: open until 8pm.\n";
print OUTFO "===thumb calendar=====\n";#thumbcalendar.com
print OUTFO " Su Mo Tu We Th Fr Sa JUL (31)\n";
print OUTFO " Su Mo Tu We Th Fr Sa SEP (30)\n";
print OUTFO " 01 02 03 04 05 06 07\n";
print OUTFO "01 02 03 04 05 06 07 08 09 10 11 12 13 14\n";
print OUTFO "08 09 10 11 12 13 14 15 16 17 18 19 20 21\n";
print OUTFO "15 16 17 18 19 20 21 22 23 24 25 26 27 28\n";
print OUTFO "22 23 24 25 26 27 28 29 30 31\n";
print OUTFO "29 30 31\n";
print OUTFO " Su Mo Tu We Th Fr Sa JUN (31)\n";
print OUTFO " Su Mo Tu We Th Fr Sa AUG (31)\n";
print OUTFO "===\@shopping========\n";

$fname="/Users/Surly/todo.txt";
open(INFO,$fname);
@lines = ;
close(INFO);

$i=0;
while($lines[$i+1]){$i++;}

while($i>=0)
{ $pass=(($lines[$i]!~/^x /));
$pass=$pass & (($lines[$i]!~/\@office/));
$pass=$pass & ($lines[$i]=~/buy/);
$pass=$pass & ($lines[$i]!~/\@online/);

if ($pass)
{$lines[$i]=~s/ \@shopping//;
$lines[$i]=~s/^buy //;
print OUTFO $lines[$i];
}
$i--;
}

close(OUTFO);

Tuesday, November 14, 2006

pluck - A perl script to simplify saving web pages for offline browsing on a Palm

Plucker Desktop, the program to convert web pages and text files into a format readable on the Palm (using the Plucker viewer), is purported to not work on Intel Macs. Whatever - I always found it a very clicky program and have wanted for years to install a command-line equivalent. Stymied by problems with compiling the Plucker Python distiller, I looked briefly at payware iSilo, but it, too, is not yet working on Intel. I dug around in the Plucker Python distiller and found a version of the Python scripts which can be run directly by just plopping them wherever.

Then I wrote a simple perl script for processing those .webloc files that I drag off the [Safari|Shiira] address bar:

#!/usr/bin/perl -w
@files = ;
foreach $file (@files) {
if ($file=~/\s/)
{$newfile=$file;
$newfile=~s/\s/_/g;$newfile=~s/\'//g;$newfile=~s/\#//g;
$newfile=~s/\&//g;$newfile=~s/\://g;$newfile=~s/\;//g;
$newfile=~s/\|//g;$newfile=~s/\?//g;$newfile=~s/\"//g;
$newfile=~s/\(//g;$newfile=~s/\)//g;$oldfile=$file;
$oldfile=~s/ /\\ /g;$oldfile=~s/\'/\\'/g;$oldfile=~s/\;/\\\\;/g;
$oldfile=~s/\&/\\\&/g;$oldfile=~s/\:/\\\:/g;
$oldfile=~s/\|/\\\|/g;$oldfile=~s/\?/\\\?/g;
$oldfile=~s/\"/\\\"/g;$oldfile=~s/\(/\\\(/g;
$oldfile=~s/\)/\\\)/g;
`mv $oldfile $newfile`;
print "mv $oldfile $newfile\n";
}
}

$i=0;
@files = ;
foreach $file (@files) {
$URL=`strings $file/rsrc | grep http | sed '/^.http/s//http/' | head -1`;
$URL=~s/\s+//g;
$URL=~s/\//;
$URL=~s/\<\/string\>//;

print $URL;print "\n";
$originalfile=$file;
$justfilename=$file;
$justfilename=~s/\/Users\/Surly\/plucker\/to-pluck\///;# WITH .webloc
$pdbname=$justfilename;
$pdbname=~s/\.webloc//;
$pdbname=substr($pdbname,0,20);
$p='/Users/Surly/Documents/Palm/Users/Surly/Files\\ to\\ Install/';
`/Users/Surly/bin/PyPlucker/Spider.py --maxdepth=1 --home-url=$URL -f $p$pdbname`;
`mv $originalfile /Users/Surly/plucker/plucked/$justfilename`;
}

Yeah, it's ugly, but it works.

Then it was necessary to use Fink to install netpbm for processing some of the image files. This failed on jpegs, and even though I had set up ImageMagick (twice, since Fink also did an install) and had set image_parser = ImageMagick in .pluckerrc Spider.py was seemingly still defaulting to netpbm. Anyhow, I noticed that there were some more image processing settings under the [POSIX] section of the .pluckerrc file so I installed libjpeg and then in the .pluckerrc file set djpeg_program = djpeg. And now it all seems to work.

What's left to do: devise a scheme for handling the more complicated plucking jobs: like those that require altering the default maxdepth value (1 seems _so much_ better than 2) or those that need special URL matching; Folder Actions for automating the plucking; catching errors in parsing and not moving the .webloc file under those circumstances.

The bottom line: Plucking on an Intel Mac is not that hard to set up.