Login

Saving Selected Text and URL From Safari to a File

One thing I do everyday is surf the web. I bet you do too. Sometimes I find information, snippets of code or just something useful and I would like to save that information for later use.

I use a FileMaker database to store this information and I extract the data from Safari using AppleScript. Since everyone does not own a copy of FileMaker, this example will demonstrate extracting the info from Safari and writing it to a file on your desktop.

What It Does

This script will take the current Safari selection (what you have highlighted) along with the URL (website address) and append it to a text file on your desktop called "safari_clippings.txt". You can change this location in the script as well as the formatting of the output.

Instructions

  • Save this file as "selection_to_file" in "/Users/your_user_name/Library/Scripts/Applications/Safari/".
  • If it is not already, turn on your scripts menu using the AppleScript Utility located in /Applications/AppleScript/
script_utility.jpg

Now when you are in Safari and click the script icon in the menu bar the "selection_to_file" will be listed at the top.

script_menu.jpg

That's all there is to it!

property FILE_PATH : ((path to desktop as Unicode text) & "safari_clippings.txt")

set {scriptText, theURL} to my getTextFromSafari()

if scriptText is "" then
 tell me to activate
 display dialog "There was nothing selected in Safari." & return & ¬
 "Exiting script" buttons {"Ok"} default button 1
 return
end if

tell me to activate
set clippingName to text returned of (display dialog "What would you like to name this clipping." default answer "" buttons {"Cancel", "Ok"} default button 2)
set theDate to do shell script "date +'%m-%d-%Y %H:%M:%S'"
set scriptText to "--------------------------------------------------" & return & return & ¬
 theDate & return & ¬
 theURL & return & return & scriptText & return & return

do shell script "echo " & quoted form of scriptText & " >> " & quoted form of POSIX path of FILE_PATH


on getTextFromSafari()
 tell application "Safari"
 activate
 set scriptText to (do JavaScript "getSelection()" in document 1)
 tell current tab of window 1
 set theURL to get URL
 end tell
 end tell
 return {scriptText, theURL}
end getTextFromSafari

Add comment


Security code
Refresh

 

Product Categories