Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Save selected browser text via AppleScript Web Browsers
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: And Firefox: 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.]
    •    
  • Currently 1.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (6 votes cast)
 
[28,822 views]  

Save selected browser text via AppleScript | 5 comments | Create New Account
Click here to return to the 'Save selected browser text via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Save selected browser text via AppleScript
Authored by: Old Toad on Feb 15, '08 09:54:11AM
For what it's worth here's a script I found out there in internet land that will copy text from both Firefox and Safari windows and add information at the top as to what site it came from:

**************************************************
-- 'Create Desktop Text Note From Selected Safari Text' Script
(* Safari doesn't appear to support an AppleScript command to Copy selected
text to the Clipboard, so this script relies on the user selecting only the
desired text, or using 'Select All' (Command-A), and then using 'Copy'
(Command-C) before executing this script.

Ideally, the user would use Keyboard Maestro, FruitMenu or Youpi Key (free)
to tell Safari to 'Copy' the selection and then execute this script.*)
global theLongDate, theDay, theMonth, theYear, theTime, theMeridian, theDateStamp, theName
(*Remember, you must first make your desired text selection in Safari...*)
get the clipboard
set theClipboard to the clipboard as text
(* See how big the file will be for naming purposes...*)
try
if the number of characters of theClipboard is less than 31 then
set theLimit to the number of characters
else if the number of characters of theClipboard is greater than 31 then
set theLimit to 31
end if
(* Large selections results in a 'stack overflow' error; simple error
handler here...*)
on error
set theLimit to 31
end try
(* Use this information to generate a file name...*)
set textString to characters 1 thru theLimit of theClipboard as string
(* Make sure the file name doesn't have any illegal characters... *)
set the textString to replaceCharsName(textString, "/", "-")
set the textString to replaceCharsName(textString, ":", ".")
on replaceCharsName(thisText, searchString, replacementString)
set AppleScript's text item delimiters to the searchString
set the itemList to every text item of thisText
set AppleScript's text item delimiters to the replacementString
set thisText to the itemList as string
set AppleScript's text item delimiters to ""
set theName to thisText as text
end replaceCharsName
(* To make sure the filename is unique, so it doesn't generate an error or
write appended information to an existing note, we're going to date-stamp
the file name... *)
set theLongDate to the (current date)
set the theYear to theLongDate as text
(*begin optional 'yy' format *)
set theYear to characters -13 thru -14 of theYear as text
(*end optional'yy' format *)

(* begin optional 'yyyy' format *)
--set theYear to characters -13 thru -16 of theYear as text
(* end optional 'yyyy' format *)

(* begin optional alpha month string; decomment to enable; be sure to
disable numeric option! *)
set theMonth to the month of theLongDate as text
set theMonth to characters 1 thru 3 of theMonth as text
(* end optional month string *)

(*begin optional numeric month string: *)
(*
copy theLongDate to theMonth
set the month of theMonth to January
set monthNumber to (1 + (theLongDate - theMonth + 1314864) div 2629728)
set theMonth to characters -2 thru -1 of ("0" & (monthNumber as string)) as
text
*)
(*end optional numeric month string *)
set dayNumber to day of theLongDate as number
set theDay to characters -2 thru -1 of ("0" & (dayNumber as string)) as text
set theTime to characters -4 thru -12 of (theLongDate as string) as text
(* optional -- look for illegal filename characters in the time;
change the separator to whatever you like (except ":" and "/") *)
set theTime to replaceChars(theTime, ":", ".")
on replaceChars(thisText, searchString, replacementString)
set AppleScript's text item delimiters to the searchString
set the itemList to every text item of thisText
set AppleScript's text item delimiters to the replacementString
set thisText to the itemList as string
set AppleScript's text item delimiters to ""
set theTime to thisText as text
end replaceChars
(* end optional illegal characters *)
set theMeridian to characters -1 thru -2 of (theLongDate as string) as text
(*change the output order below; add spaces, separators, etc.; remove input
as you wish*)
set theDateStamp to theYear & theMonth & theDay & theTime & theMeridian
(*Now we take the file naming parameters to Safari... *)
tell application "Safari"
(* Gather some more information to stamp the note internally...*)
set theWindowName to the name of the front window
set theURL to the URL of the front document
(* Name the file here; yes, I know, ugly use of file name limits and
formatting; the point was to create file names that gave hints to the
content; the unique date was required to prevent duplication and
overwrites...*)
set theFile to theName & "... " & theDateStamp
(* Set the filepath to the Desktop of the current user; some people may
wish to modify this path to a specific folder; e.g., "Web Clippings"; alter
as desired. *)
set pathstring to ((path to desktop) as text) & theFile
(*Generate the final content and write the note...*)
set writeString to "Source: " & theURL & return & "Page Title: " & theWindowName & return & "Date: " & theLongDate & return & return & (the clipboard)
set theNote to open for access file pathstring with write permission
write writeString to theNote
close access theNote
end tell
***************************************************

Again I don't remember where I got the script. The site information will be at the top of the document and looks like this:


Source: http://www.macosxhints.com/
Page Title: macosxhints.com - OS X tips and tricks!
Date: Friday, February 15, 2008 9:38:53 AM


I use a QuicKeys shortcut to run the script.

[ # ]
Save selected browser text via AppleScript
Authored by: bluedogranch on Feb 15, '08 12:35:23PM

That's a nice script by whoever wrote it.

I'm just learning Applescript and use this to copy text to a new BBEdit window and then clear the clipboard:

tell application "BBEdit"
make new document
paste selection
activate window
set the clipboard to ""

What I'd like to do is combine the way the URL, title and date are added to the file in the original script above, but have the script open a new file in BBedit and have it remain open so I can trim out some trailing text (links, "print this page", etc.) text before I save it.

So I changed

set theNote to open for access file pathstring with write permission
write writeString to theNote
close access theNote

to:

tell application "BBEdit"
make new document
write writeString to document

But I get an error when it runs: "Can't make document into type file."

Am in in over my head with this? I'm looking through the BBEdit scripting dictionary, but as I say, I'm just learning. Ideas?



[ # ]
Save selected browser text via AppleScript
Authored by: MrLucky on Feb 15, '08 10:26:46AM

Very elegant script. I've been using something similar, so I cannibalized bits of this one and put them into mine. I collect loads of news clippings, so my script copies the selected text to a unique document, saved as the first line of the selection (i.e. Man Bites Dog.txt).

tell application "Finder" to set dFolder to ((startup disk) as string) & "" --write the path to your preferred folder between these quotes, using colons to separate the folder names, i.e. Users:username:Documents:MyNewsClippings:

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 tSubject to first paragraph of (selecTxt as string)
if (tSubject contains ":") then
set {oAStid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set tSubject to text items of tSubject
set AppleScript's text item delimiters to "_"
set tSubject to tSubject as string
set AppleScript's text item delimiters to oAStid
end if
set dat to (current date) as text
set tContent to pgTitl & return & theurl & return & dat & return & return & selecTxt
set fRef to open for access file (dFolder & tSubject & ".txt") with write permission
write tContent as string to fRef
close access fRef
tell application "Safari" to activate


To use this effectively, highlight the text in Safari, such that the title of the article is the first line. The resulting .txt file will appear in your preferred folder with the filename set as the first line of selected text. I uses Fastscripts to run this one with a keystroke.



[ # ]
Save selected browser text via AppleScript
Authored by: amaloney on Mar 01, '08 08:16:59PM

MrLucky

How does one do this with Firefox?

Al Maloney



[ # ]
Save selected browser text via AppleScript
Authored by: bluedogranch on Feb 18, '08 10:33:41AM

Made progress on the learning curve to use BBEdit instead of the default text editor while also borrowing from other scripts in this thread, so this might be of use to others.

I will use this mostly to copy and save the "printer friendly" pages of news sites, as I save articles for research. Like the other scripts, this script gives me the date and the URL for future reference. At the end, I bring the new BBedit window forward so I can delete extra text and then save the file with my own title and/or date, depending on what I need for each research project.

This script selects all of the text in the front most Safari window or tab, converts the text to Unciode, and then makes a new BBedit document and pastes the clipboard, along with the date (from a shell script) and URL.

(Future enhancements might be adding "zap gremlins" to clean text or calling PlainClip to convert the clipboard to plain text before pasting.)


tell application "Safari"
activate
tell application "System Events"
tell process "Safari"
click menu item "Select All" of menu "Edit" of menu bar 1
click menu item "Copy" of menu "Edit" of menu bar 1
end tell
end tell
end tell

tell application "Safari"
set textclip to (the clipboard)
set theURL to (get URL of document 1)
end tell

try
set the clipboard to Unicode text of (the clipboard as record)
end try

set dat to (do shell script "date '+%m/%d/%y'") as text

tell application "BBEdit"
activate
make new document
set text of front document to (dat & return & return & (theURL) & return & return & textclip & return)
select insertion point 1 of contents of front document
end tell



[ # ]