Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,000 hints and counting!

An AppleScript to email Safari URLs with titles via Mail Web Browsers

I've been trying to use Mail/Safari rather than Mozilla. One of the (many) things I miss is the ability to select "send link" when viewing a page, to have a new message created with the page title as the subject and the URL as the body. (I know Mail service has a "send selection" but that's not quite what I want). AppleScript and the AppleScript Menu to the rescue!

tell application "Safari"
  ignoring case
    set theURL to URL of front document
    set theSource to source of front document
    set AppleScript's text item delimiters to "title>"
    set theSource to second text item of theSource
    set AppleScript's text item delimiters to "</"
    set theTitle to first text item of theSource
  end ignoring
end tell

tell application "Mail"
  set accountAddresses to (email addresses of first account)
  set fromAddress to first item of accountAddresses
  set theMessage to make new outgoing message
  set visible of theMessage to true
  set subject of theMessage to theTitle
  set content of theMessage to theURL
  activate
end tell
[robg adds: Save this script as a compiled script in your user's Library -> Scripts folder, and then just activate it using the ScriptMenu icon when you need it. Very handy! As an aside, activate the ScriptMenu by double-clicking ScriptMenu.menu in the Applications -> AppleScript folder.]
    •    
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[19,003 views]  

An AppleScript to email Safari URLs with titles via Mail | 20 comments | Create New Account
Click here to return to the 'An AppleScript to email Safari URLs with titles via Mail' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
An AppleScript to email Safari URLs with titles via Mail
Authored by: fm on Fri, Mar 21 2003 at 11:19AM PST
Note that you do not need to use text item delimiters and conversion between text and list to get the title of a document.

Since Safari puts the document title on document's window title bar, you can get the title with "name of <window reference>".

So, this is the script I use:

tell application "Safari"
set w to front window
set theSubject to name of w
set theURL to URL of document of w
end tell

tell application "Mail"
activate
set newMsg to (make new outgoing message with properties {visible:true, content:theURL, subject:theSubject})
-- code for setting other message properties here
end tell

[ Reply to This | # ]
Summarize
Authored by: Brontojoris on Tue, May 27 2003 at 11:05AM PDT
This version will get Summarize the webpage text too.

tell application "Safari"
ignoring case
set theURL to URL of front document
set theTitle to name of front window
set theText to text of front document
set theDocSummary to summarize theText in 4
end ignoring
end tell

tell application "Mail"
set accountAddresses to (email addresses of first account)
set fromAddress to first item of accountAddresses
set theMessage to make new outgoing message
set visible of theMessage to true
set subject of theMessage to theTitle
set content of theMessage to theDocSummary & "

" & theURL
activate
end tell

[ Reply to This | # ]
An AppleScript to email Safari URLs with titles via Mail
Authored by: lrosenstein on Wed, Aug 13 2003 at 6:04PM PDT
You can also get the document title with JavaScript:

tell application "Safari"
set doc to a reference to document 1
set page_title to do JavaScript "document.title" in doc
end tell

The same goes for the selected text (using "getSelection()"), which is what I like to do.

[ Reply to This | # ]
An AppleScript to email Safari URLs with titles via Mail
Authored by: PancakeMan on Fri, Mar 21 2003 at 11:24AM PST
Worked for me. Is there a way to tell Mail to give focus to the new mail message and put the cursor in the To: field? (Seems like something AppleScript could do, but I'm a total neophyte.)

[ Reply to This | # ]
An AppleScript to email Safari URLs with titles via Mail
Authored by: hemperor on Fri, Mar 21 2003 at 12:17PM PST
Great hint, If you are using Fruit Menu you can set up a contextual menu for Safari and put the compiled app in the menu, or use Youpi Key and set up a key-command to do it. Thanks.

[ Reply to This | # ]
An AppleScript to email Safari URLs with titles via Mail
Authored by: amit_kr on Fri, Mar 21 2003 at 1:11PM PST
I wanted the ability to do the same thing, and found a much simpler and convenient way. Drag the following "url" (actually a javascript) to your bookmark bar:

javascript:location.href='mailto:?SUBJECT='+document.title+'&BODY='+escape(location.href)

and call it whatever (say 'e'). Since this is a bookmark (and not a folder), a hotkey is assigned to it (cmd-0 to cmd-9) by Safari.

Now, whenever you need to send the title/url to someone, just press the hotkey, and that's it!! a mail message is created with the title of the page as the subject, the url as the body, and selection on the "To:" field.

So one could select a part of the webpage, press cmd-c (to copy), cmd-2 (to compose mail with url and title already there), type in mail, tab twice, cmd-v (to paste the part), and send!

[ Reply to This | # ]
Bookmark option - yes
Authored by: Deut3221 on Fri, Mar 21 2003 at 1:41PM PST
Great hint!

[ Reply to This | # ]
sweet
Authored by: darndog on Fri, Mar 21 2003 at 1:59PM PST
Must be the first hint i have seen that i could activate with one mouse drag, Bravo! although i do feel sorry for the original poster...

darndog

[ Reply to This | # ]
An AppleScript to email Safari URLs with titles via Mail
Authored by: PancakeMan on Fri, Mar 21 2003 at 2:38PM PST
cool! this even solves my earlier issue of focusing on the To: field in Mail automatically!

[ Reply to This | # ]
wow.
Authored by: mikerose on Fri, Mar 21 2003 at 2:51PM PST
I just abandoned my effort to convert the Mail script to work with QuickMail -- the Javascript is outstanding! Should work with whatever mailer is set in Internet Prefs, I would think.

Rob, please amend the original hint with this inventive 2nd way.

[ Reply to This | # ]
An AppleScript to email Safari URLs with titles via Mail
Authored by: tjj on Fri, Mar 21 2003 at 3:50PM PST
Nice!
Also works in Camino (and any other browser that has java enabled?)

[ Reply to This | # ]
Excellent, excellent and excellent!
Authored by: sinjin on Fri, Mar 21 2003 at 4:24PM PST
I agree with mikerose, this is worth submitting as a hint. It is the single-most useful Safari hack/add-in/add-on/hint I've seen.

I applaud the original post, with the use of Applescript, and would love to see an Applescript as easy and useful. This hint has just piqued my interest into checking out javascript, though.

[ Reply to This | # ]

An AppleScript to email Safari URLs with titles via Mail
Authored by: luhmann on Sun, Mar 23 2003 at 10:14PM PST
I like this javascript. Is there a way to have it add any highlighted/selected text from the web page to the body of the e-mail message as well? I used to have a script that did this, but it didn't work with Safari.

[ Reply to This | # ]
And here's one for Entourage:
Authored by: Fofer on Sat, Mar 22 2003 at 12:11AM PST
tell application "Safari"
set t to name of window 1
set s to URL of document 1
set u to the clipboard
end tell

tell application "Microsoft Entourage"
activate
set newTask to make new outgoing message with properties {name:t, content:s & return & return & u}
open newTask
end tell
end


-----


This will create an email with the Title of the web page, and paste in it's URL as well as whatever's in your clipboard. (A choice snippet from the page your referring to, perhaps?)

[ Reply to This | # ]
Help! re: And here's one for Entourage:
Authored by: Brian Purnell on Tue, Mar 25 2003 at 10:06PM PST
Thanks, it's a great script, only:

Script Editor reports I don't need the last "end". Is this correct?

Also after running is there a way to bring me back into Entourage "To" box? As it is I end up in some limbo and have to click on the Entourage "To" box to have it active and enter an address. Is there a way to solve this?

Thanks for any help, thanks Fofer for writing the script in the first place.

[ Reply to This | # ]
An AppleScript to email Safari URLs with titles via Mail
Authored by: fritz102 on Sun, Mar 23 2003 at 4:27PM PST
Or you could use this bookmarklet:
javascript:window.location="mailto:"+prompt("Send message to:","Email@Email.com")+"?body="+window.location+"&subject="+document.title

[ Reply to This | # ]
Can anybody do this for Camino/Entourage?
Authored by: tngland on Sun, Mar 23 2003 at 4:35PM PST
That says it, since I use Camino & Entourage, I'd like to be able to do the same, but simply changing Safari to Camino doesn't work for me.

Thomas S. England
Decatur GA 30030
Portfolio:
http://englandphoto.com/portfolio//

---
Thomas S. England
Decatur GA 30030
Portfolio:
http://englandphoto.com/portfolio//

[ Reply to This | # ]
Bookmarklet
Authored by: bluehz on Thu, Aug 14 2003 at 8:19AM PDT
This bookmarklet will create new webpage with the Subject set to the title of the page, and the body set to the URL, then a blank line, and then the selected text. Tested in Safari.
javascript:location.href='mailto:?SUBJECT='+document.title+'&BODY='+escape(location.href+'\r\r'+getSelection())
and this one will popup a dialog that will ask you for the Subject you want to have for your new email (as opposed to above where the page title is used):
javascript:void(subj=prompt('Enter%20Subject:',''));if(subj)void(location.href='mailto:?SUBJECT='+escape(subj)+'&BODY='+escape(location.href+'\r\r'+getSelection()))


[ Reply to This | # ]
Bookmarklet
Authored by: bluehz on Thu, Aug 14 2003 at 8:45AM PDT
This one does the same as above, but gives you popup to enter subject and some msg text:
javascript:void(subj=prompt('Enter%20Subject:',''));void(extra=prompt('Enter%20Msg:',''));if(subj)void(location.href='mailto:?SUBJECT='+escape(subj)+'&BODY='+escape(extra)+'\r'+escape(location.href+'\r\r'+getSelection()))


[ Reply to This | # ]
An AppleScript to email Safari URLs with titles via Mail
Authored by: bamorganti on Fri, Feb 27 2004 at 4:59AM PST
robg, you posted a solution to this issue, but I was hoping you could give a little more step by step explanation. I tried to add this to apple script, but it would not record the script, so obviously I am doing something wrong. Any help would be greatly appreciated.

[ Reply to This | # ]