Save selected browser text via AppleScript

Feb 15, '08 07:30:05AM

Contributed by: musselrock

I occasionally find myself wanting to save some text from a browser window. Usually I want to know where the text came from as well, so just dragging out a text clipping to the desktop is not a good solution.

I wrote two AppleScripts (one for Safari and one for Firefox) to make saving a selected text, URL, and page title from browser windows easier. The scripts write the browser's selected text and other info to a text file that it creates on the user's desktop. (I don't like clutter on the desktop, so on my Mac, this file gets created in another folder. However, I wanted to provide example scripts that worked, so I chose the desktop).

Below is an example of the script's output with the date, the URL, the page title and some text from a selection:

Wednesday, February 6, 2008 8:40:11 PM
http://some-url.com
Photo of the Day
-------------------
A deep-blue sky sets off a mass of yellow wildflower blooms along ... [snip] ... magazine
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Further use of the script will append data to the same file, and keep the text neatly formatted and properly separated.

Here are the scripts; first, Safari:

tell application "Safari"
  set selecTxt to (do JavaScript "(getSelection())" in document 1)
  set theurl to (get URL of document 1)
  set pgTitl to (do JavaScript "document.title" in document 1)
end tell

set dat to (current date) as text
set clipFil to (path to desktop folder as text) & "BrowserClips.txt"
try
  close access file clipFil
end try
set filRef to open for access file clipFil with write permission
write (dat & return & (theurl) & return & (pgTitl) & return & "-------------------" & return & selecTxt & return & "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & ¬
  return & return) to filRef starting at eof
close access filRef
tell application "Safari" to activate
And Firefox:
tell application "Firefox"
  activate
  set theurl to «class curl» of window 1
  set pgTitl to «class pTit» of window 1
end tell
tell application "System Events" to keystroke "c" using {command down}
tell me to activate
set selecTxt to the clipboard as text
tell application "Firefox" to activate
set dat to (current date) as text
set clipFil to (path to desktop folder as text) & "BrowserClips.txt"
try
  close access file clipFil
end try
set filRef to open for access file clipFil with write permission
write (dat & return & (theurl) & return & (pgTitl) & return & "-------------------" & return & selecTxt & return & "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & ¬
  return & return) to filRef starting at eof
close access filRef
These could be Script Menu scripts, but I use them as applications and position them on my desktop at the extreme top left. A double click just to the left of my browser window gives me a shorter mouse trip to activate them.

[robg adds: Both scripts worked in testing. I also tested the Safari script in Butler, assigning it (as a new AppleScript item) to a hot key that only works in Safari. This also worked, and makes it really simple to snag the selected text to a file.]

Comments (5)


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