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

Save selected text from Safari to a text file Web Browsers
This AppleScript will save selected text from the current Safari window to a text file on your desktop. It uses UI Scripting, so it will only work with "enabled" versions of Panther (see Activating UI Scripting). You can use the script repeatedly and new text will be appended to the existing file. The URL and a timestamp are included to help you keep track of where the snippets came from.

tell application "Safari"
  activate
  set myURL to URL of document 1
end tell

tell application "System Events"
  tell process "Safari"
    keystroke "c" using {command down}
    delay 2
    set myData to (the clipboard) as text
  end tell
end tell

set myDate to current date
set fPath to path to desktop
set fName to (do shell script "date \"+%Y-%m-%d\"") & ".txt"
set myFile to open for access file ((fPath as string) & fName) 
 with write permission
write "=== " & return & "Date: " & myDate & return & 
 "Text from: " & myURL & return & return & myData to myFile starting at eof
close access myFile
Save it to ~/Library/Scripts/Applications/Safari/ so that you can access it easily while browsing.
    •    
  • Currently 1.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[12,348 views]  

Save selected text from Safari to a text file | 25 comments | Create New Account
Click here to return to the 'Save selected text from Safari to a text file' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Save selected text from Safari to a text file
Authored by: Anidel on Feb 23, '04 11:16:36AM

how this is different from simply selecting the text and dragging it onto the desktop?

---
Anidel



[ Reply to This | # ]
Time stamp
Authored by: hedgeman on Feb 23, '04 12:09:17PM

It is easier then dragging some text and it provides a time stamp and URL, which dragging does not.



[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: lipids on Feb 23, '04 03:50:30PM

dragging selected text to the desktop makes a "clipping" which is not an editible text file.



[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: Fofer on Feb 23, '04 12:11:13PM

Well, as the hint nicely explains, it saves it all to one clean text file, with URL and timestamp, and appends any future text clippings to the same file.

Text clippings are nice and underappreciated (methinks because you have to wait 2-3 seconds before dragging the text from Safari, lest the selection gets deselected.) Still, some folks don't want 34 text clippings on their desktop. This script can help.

Nice hint, BTW, thanks!



[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: Lankhmart on Feb 23, '04 12:31:41PM
You can grab the selection in Safari without using UI Scripting or the clipboard with a "do JavaScript" command like this (also eliminating the 2 second delay):

tell application "Safari"
	activate
	set myURL to URL of document 1
	set myData to do JavaScript "getSelection();" in document 1
end tell

set myDate to current date
set fPath to path to desktop
set fName to (do shell script "date \"+%Y-%m-%d\"") & ".txt"
set myFile to open for access file ((fPath as string) & fName) 
	with write permission
write "=== " & return & "Date: " & myDate & return & 
	"Text from: " & myURL & return & return & myData to myFile starting at eof
close access myFile


[ Reply to This | # ]
OmniWeb version of save selected text
Authored by: Krioni on Feb 23, '04 03:18:32PM
To do this in OmniWeb: (in place of the tell app "Safari"...end tell part)

tell application "OmniWeb"
	set myURL to address of browser 1
	set myData to do script "getSelection();" window browser 1
end tell

Then add in the rest of the parent poster's script.

[ Reply to This | # ]

Save selected text from Safari to a text file
Authored by: orpy on Feb 24, '04 03:00:38PM

Thanks for the javascript tip.

I use javascript all the time to control Photoshop from AppleScript - didn't even give it a thought with Safari...

The script has been updated in the week or so since posting it here. I'd adapted it so that it would copy the whole page as text if nothing was selected.

You can find a more up to date version here:

http://prometheus.idea-digital.com/phpbb2/viewtopic.php?t=164

Regards

--
Martin



[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: macgizmo on Feb 23, '04 03:12:43PM

I haven't been able to drag text out of safari to make a clipping since the first version.

However, you can also simply select the text and under the Safari Menu, select Services/TextEdit/ and select "New Window Containing Selection" - this opens a new Text Edit document containing the selected text - all you have to do is Save it - unless you wish to add more info to the selection.

---
Just getting there isn 't half the fun!



[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: Lunatic Prods on Feb 23, '04 03:43:49PM

Oh, I love this site!

I think I'm reasonably clever, but I'm constantly baffled and enlightened by contributors here.

Some hints are chillingly complex, but this comment about the Services menu just reminds us to go and have a look . . . and there at the top of Safari's Services menu is 'Copy to Tex-Edit Plus' (my word-wiggler of choice) with a very handy keyboard shortcut.

Thanks to all - and, yes, I'm heading to Paypal to do my duty! ;o)

Huw



[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: theilgaard on Feb 23, '04 04:37:25PM

This method will also include any selected images as well.

(You can also choose "Make new sticky note" to create a new note in Stickies, also including images.)



[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: Han Solo on Feb 23, '04 11:21:08PM
Agreed: Services is the way to go here.

As for making clippings from Safari (or any Cocoa app): highlight your selection, then click-and-hold-briefly before starting the drag. OS 9 (and 8 and maybe 7.x...?) used to give visual feedback that indicated "you are now able to drag" (and it was nearly instantaneous); in OS X you just need to pause for half a second before beginning the drag -- there is no visual feedback. It becomes second nature after a few tries....

[ Reply to This | # ]

Save selected text from Safari to a text file
Authored by: liquidgood on Feb 23, '04 06:10:59PM
here you can get a program to add a service called AppendNote to the services menu that will add the selection to a designated file with timestamps.

[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: MN OSX HAM on Feb 23, '04 08:40:25PM

I like this.
I always have a half dozen unsaved text scap windows open, and about a zillion saved to the desktop.

Cool feature to capture the URL.

Thanks for sharing :-)



[ Reply to This | # ]
Safari, Terminal, and nbsp
Authored by: brettmjohnson on Feb 23, '04 09:16:03PM

One problem I have been suffering with is how Safari treats nonbreaking space & nbsp ; when copying plain text to the clipboard (or dragging). The character is converted to some non-ASCII space character. Although TextEdit can handle it, pasting into Terminal.app results in crap.

This is annoying to me because I can no longer copy and paste code examples from Experts Exchange (http://www.experts-exchange.com) without an annoying replace-all step in TextEdit.



[ Reply to This | # ]
Safari, Terminal, and nbsp
Authored by: DavidRavenMoon on Feb 24, '04 08:40:37AM

TextEdit's default format is RTF... so before you copy and paste to the terminal just press Shift-Command-T to change it to plain text (or use the menu command under Format).
.



[ Reply to This | # ]
Doesn't work for me
Authored by: badturtle on Feb 23, '04 10:32:16PM
I did exactly as the hint said. I don't see a script menu. I had to create the folders listed. I saved the script, restarted, and nothing. I even logged out and back in and it didn't work. Here is a screen shot.

[ Reply to This | # ]
Doesn't work for me
Authored by: Lankhmart on Feb 24, '04 03:30:49AM

To enable the script menu, open the AppleScript folder in your Applications folder and run the Install Script Menu app.



[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: jonn8n on Feb 24, '04 04:11:49AM
On my website:

http://homepage.mac.com/jonn8/as/

you can download a similar script that I wrote to take the currently selected text from Safari and create a new email message in your choice of several different mail clients. (See the navbar on the left of the main page and click on "Safari Selection to Mail Message".) My script also uses the do JavaScript method to get the selection, much better than the GUI scripting and it also grabs the URL.

Jon

[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: orpy on Feb 24, '04 07:27:44PM
This is the latest version of this script. It was amended last week so that it will save the text of the whole page if nothing is selected. I've also added the javascript tip that was posted in reply to my oriiginal post.
--File: SaveSelectedOrText.scpt
--Author: Martin Orpen
--Date: 13.02.2004
--Updated: 24.02.2004
--
--Requires OS X and Safari.
--This script will save selected text or a whole page
--as text if nothing is selected. Text is appended
--to a dated file on your desktop.
--
-- = = = = = =

set myData to ""

tell application "Safari"
	activate
	set myURL to URL of document 1
	--line below suggested by Lankhmart on macosxhints
	set myData to do JavaScript "getSelection();" in document 1
	if myData is "" then
		set myData to text of document 1 as text
	end if
end tell

set myDate to current date
set fPath to path to desktop
set fName to (do shell script "date \"+%Y-%m-%d\"") & ".txt"
set myFile to open for access file ((fPath as string) & fName) with write permission
write "=== " & return & "Date: " & myDate & return & "Text from: " & myURL & return & return & myData & return & return to myFile starting at eof
close access myFile
beep
Thanks to all those who responded with advice.

[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: anh on May 24, '04 09:43:04PM

This hint gives most of what I need but assumes that the web page is already open. I am trying to create an automated script that opens a web page, copies the content, and manipulates it. Problem is that the web site is often slow, so I would like to wait patiently for the page to load and then proceed. Is there a way to make the script wait until the page is loaded? Using the "delay XX" kludge is a bit hit and miss, so I would prefer a more elegant solution. Can anyone help? Thanks.



[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: duncman on Jul 19, '05 10:22:08AM

I can see I'm a little late in this but this script is pretty cool. I use it along with Book2Pod to save web sites to my iPod for reading later.



[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: jooblie on Feb 25, '04 11:53:09AM

Is there any way to loop thru all the tabs of a window, grabbing the URL from each?



[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: orpy on Feb 26, '04 05:37:56PM

Not sure - you might have to resort to UI scripting to do this.

You could grab the URL of the currently active tab then select "close tab" and grab the URL of the next tab etc until there are no more tabs left?

Safari's dictionary is sadly lacking on the subject of tabs.

The properties returned from a tabbed window make no mention of tabs.

---
--
Martin



[ Reply to This | # ]
Check out "An improved 'Combine Windows' AppleScript for Safari"
Authored by: ageless on Apr 15, '04 01:42:46AM
http://www.macosxhints.com/article.php?story=20040401180858458

Check out the last version of the applescript, you can adapt it to select all the tabs in all open windows.

[ Reply to This | # ]
Save selected text from Safari to a text file
Authored by: eibcga on Sep 29, '05 06:42:25PM

Is there a modified version of the AppleScript so it will save selected text from the current Safari window and tell iChat to put the text in a status message on the Buddy List? For example, I have a Script that does specified text, the system time and date to the Buddy List status message. But, it would be nice to pull weather data from a web site rather than having to enter it manually. ;) Thank you!



[ Reply to This | # ]