Print selected text from browser via AppleScript

Oct 12, '06 07:30:05AM

Contributed by: maczen

I have been reading MacOSXHints for quite some time. In an effort to contribute and not just leech, here is a hint that I find useful daily. I miss the "Print Selection" option found in some browsers on Windows. I find this useful to quickly print directions, recipes, or anything else where I want an unformatted plain text print out.

Combining some AppleScript and Terminal commands, I created an AppleScript addition that allows me to select text in my browser (currently Camino), click one button, and send it to my default printer. Two items need to be placed on your system to get this to work:

  1. Shell script to send the text string to the default printer.
    #! /bin/sh
    pbpaste | lpr
    I saved this as ~/Documents -> Scripts -> sel_lpr.sh
  2. An AppleScript to capture the highlighted text and send it to the clipboard:
    tell application "Camino"
      activate
      tell application "System Events" to keystroke "c" using command down
    end tell
    do shell script "~/Documents/Scripts/sel_lpr.sh"
    This is, of course, saved in ~/Library -> Scripts -> PrintSelected.app.
Now when I select some text, I just select this AppleScript and it shoots it to the default printer. A more elegant way would be to call the AppleScript from a bookmarklet, but I have not gotten around to that yet. Hope this helps someone else...

[robg adds: Remember to make the shell script executable with chmod a+x sel_lpr.sh from within the Scripts directory (or wherever you saved it).]

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20061002171102999