I'm Brett Slatkin and this is where I write about programming and related topics. Check out my favorite posts if you're new to this site. You can also contact me here or view my projects.

27 October 2009

Leah's post got me thinking about the transient nature of the net. Hell-- my first website is long gone; wish I had even taken a screen shot of it. Which got me thinking, it seems like the most reliable way to preserve the net's history is with archival through pixels. Behold: My gross workflow for Mac OS X and Firefox that saves a website screenshot to any gallery via email posting. Use this for super-low-time cost website capture.
First, download the Snapper Firefox extension. Then create a new directory (say, /Users/you/Documents/Upload Images) where the images will go and configure Snapper to use it:

Now create a new Folder Action AppleScript using the Script Editor and save it in /Users/you/Library/Scripts/Folder Action Scripts, with this source code:

on adding folder items to this_folder after receiving added_items
    repeat with i from 1 to number of items in added_items
        set this_item to item i of added_items
        set this_item_posix to quoted form of POSIX path of this_item
        set result to do shell script ("/Users/bslatkin/Desktop/Projects/screenshot_archiver/screenshot_archiver.py " & this_item_posix)
        tell application "Finder"
            move this_item to trash
        end tell
    end repeat
end adding folder items to

This script will send any new images to a screenshot_archiver.py script (that we'll define next). Be sure to set up the path to that Python script correctly. Now, add a Folder Action to that directory and attach the script you just made:

Last, set up the Python script, adjusted with whatever settings you need to make it work with your posting system. In my case, I'm using it with Picasa Web Albums, which has an emailing interface; you could also use Posterous if that's your thing.

#!/usr/bin/python2.5

"""Emails a screenshot to an email address."""

import base64
import hashlib
import os
import sys
import subprocess
import time


MAIL_TEMPLATE = \
"""From: %(from_address)s
To: %(to_address)s
Subject: %(subject)s
Content-Type: multipart/mixed; boundary="%(boundary)s"

You should not see this.

--%(boundary)s
Content-Type: text/plain; charset=ISO-8859-1



--%(boundary)s
Content-Type: %(mimetype)s; name="%(filename)s"
Content-Disposition: attachment; filename="%(filename)s"
Content-Transfer-Encoding: base64

%(data)s
--%(boundary)s--
"""


def main(argv):
  params = {
    'from_address': 'youremail@example.com',
    'to_address': 'someemail.withapassword@picasaweb.com',
    'subject': 'The name of your album',
    'mimetype': 'image/png',
    'boundary': hashlib.sha1(str(time.time())).hexdigest(),
    'filename': os.path.basename(argv[1]),
    'data': base64.encodestring(open(argv[1], 'rb').read()),
  }
  message = (MAIL_TEMPLATE % params).replace('\n', '\r\n')
  proc = subprocess.Popen(['sendmail', params['to_address']], stdin=subprocess.PIPE)
  proc.communicate(message)
  print 'Completed with', proc.wait()


if __name__ == '__main__':
  main(sys.argv)
© 2009-2016 Brett Slatkin