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

An AppleScript to send an email about a page in Safari Web Browsers
When I find a web page that might be of interest to someone I know, I send an email suggesting they have a look. I want this email to be short and enable the recipient to decide quickly if the page is, in fact, of interest. The email usually includes some text from the page, a link, and a comment from me.

But, if you do it often, this recipient-friendly approach becomes labor intensive for the sender. There's too much copying, pasting, and application switching. Hence this Applescript. It puts text you select in Safari into a new Mail message within quotation marks, appends the page link, and puts the page name into the email's subject field. Add a comment, if you wish, and press send.
tell application "Safari"
    activate
    set the clipboard to ""
    tell application "System Events" to keystroke "c" using command down
    set myCount to count (the clipboard)
    if myCount = 0 then
        display dialog "Oops, you forgot to select some text in Safari." & return & return & "Please select the text you want to include in your email and try again." buttons {"OK"} default button "OK"
        return
    end if
    set SelectedText to the clipboard
    set PageURL to URL of front document
    set PageName to name of front document
end tell

tell application "Mail"
    activate
    set MyEmail to make new outgoing message
    set MessageFont to message font
    set MessageFontSize to message font size
    tell MyEmail
        set content to ("\"" & SelectedText & "\"" & return & return & "More at " & PageURL)
        set subject to PageName
        set font of content to MessageFont
        set size of content to MessageFontSize
    end tell
    tell window named PageName
        set index to 1
    end tell
end tell
To install:
  1. Copy the script.
  2. Open the application Script Editor and paste the script into the window.
  3. Name and save the script in your user's Library/Scripts/Safari folder (create the folders as necessary).
  4. If you have not already done so, use Applescript Utility to show the Scripts menu in the menu bar.
To use: In Safari, select the text for your email, then run the script by selecting it in the scripts menu in the menu bar.

[robg adds: I tested this and it worked as described. For the save location, however, I would recommend your user's Library » Scripts » Applications » Safari folder. By placing it here, you'll see it listed directly in a 'Safari Scripts' section of the Scripts menu when you're in Safari. If you store it just in the Scripts/Safari folder, then it will appear in a Safari sub-menu of the Scripts menu.

Note that you can do most of this without the script, simply by selecting the text on the page, copying it, then pressing Shift-Command-I (File » Mail Link to This Page). Mail will open with the page title filled in as the subject, along with the link to the page in the body of the message. Enter a recipient in the To line, click into the body, and press Command-V to paste the copied text. If you do this a lot, though, this script will save some time and key presses.]
    •    
  • Currently 1.75 / 5
  You rated: 3 / 5 (8 votes cast)
 
[12,763 views]  

An AppleScript to send an email about a page in Safari | 17 comments | Create New Account
Click here to return to the 'An AppleScript to send an email about a page in Safari' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to send an email about a page in Safari
Authored by: brucio on Feb 06, '08 08:41:42AM
Compiler shows me an error at:
set content to (""" & SelectedText & """ & return & return & "More at " & PageURL)
Syntax error: Expected ", " but found ".

[ Reply to This | # ]
An AppleScript to send an email about a page in Safari
Authored by: clarkj29 on Feb 06, '08 09:11:05AM

compiler found same error:
set content to (""" & SelectedText & """ & return & return & "More at " & PageURL)



[ Reply to This | # ]
An AppleScript to send an email about a page in Safari
Authored by: vwgtiturbo on Feb 06, '08 09:31:02AM

Sorry for the double-post, but I just realized that I posted incorrectly...

If you look at the area that the script editor highlights, you will see """. Erase one of those marks. Then you will see another area on the same line with the same """. Erase one of those marks. So, change this:

set content to (""" & SelectedText & """ & return & return & "More at " & PageURL)

to:

set content to ("" & SelectedText & "" & return & return & "More at " & PageURL)

You can then save the script as outlined above, and it works.



[ Reply to This | # ]
An AppleScript to send an email about a page in Safari
Authored by: egoham on Feb 06, '08 10:11:15AM

I see no difference in your replies.

Figured the extra " needed removed, both instances.

Yes it complies at this point.

However, I still have a problem.

Mail comes to the front, but a new message is not created. I even looked to see if the message was hidden behind the main window.

Any help appreciated.

Using Mac OS X 10.4.11, Mail Version 2.1.2 (753), and Version 3.0.4 (523.12.2) on a Dual 2.7 GHz PPC G5



[ Reply to This | # ]
Why do you need an AppleScript for this ?
Authored by: DonColore on Feb 06, '08 09:14:30AM

While in Safari, just type Apple-Shift-i and Safari does everything for you.



[ Reply to This | # ]
Why do you need an AppleScript for this ?
Authored by: vwgtiturbo on Feb 06, '08 09:29:40AM

Apple-Shift-I doesn't include selected text in the email. It does the page title as the Subject, and includes a link, but the purpose of the script is to include some of the text from the page so that the recipient can see how (by your selected text) the site is worthy of visiting.



[ Reply to This | # ]
Why do you need an AppleScript for this ?
Authored by: emaven on Feb 06, '08 04:41:42PM

Yes, that is true. But i use the mouse a lot, not the keys.

So i have a bookmarklet (javascript) that is in the bookmarks bar. I just click on it, and it puts the subject and link in the body of the email and the subject in the title of the email. (it does not send any selected info however)

Since I send these types of emails to several people, i got tired of putting in the recipients. So instead of having one bookmarklet on the toolbar, i have a folder that contains several bookmarklets, each one preset for a particular person(s). I just select the one i want to use.

Here is a sample bookmarklet. Be sure to put in the email address you want to use after the mailto:

javascript:location.href='mailto:name@email.com?SUBJECT='+document.title+'&BODY='+document.title+'%5Cn%20%20%20%3C'+escape(location.href)+'%3E'



[ Reply to This | # ]
Why do you need an AppleScript for this ?
Authored by: osxpounder on Feb 07, '08 01:10:49PM

I use a similar bookmarklet, and I just found, as I tried today's hint, that the bookmarklet creates a plain-text email [perhaps because that's my preference], whereas the Safari Apple-Shift-i command creates a rich text email, which I don't prefer. Still a great hint and comments thread here, though.



[ Reply to This | # ]
An AppleScript to send an email about a page in Safari
Authored by: vwgtiturbo on Feb 06, '08 09:27:21AM

If you look at the area that the script editor highlights, you will see """. Erase one of those marks. Then you will see another area on the same line with the same """. Erase one of those marks. So, change this:

set content to (""" & SelectedText & """ & return & return & "More at " & PageURL)

to:

set content to ("" & SelectedText & "" & return & return & "More at " & PageURL)

You can then save the script as outlined above, and it works.



[ Reply to This | # ]
An AppleScript to send an email about a page in Safari
Authored by: tomschmidt on Feb 06, '08 10:33:47AM

For some unknown reason, 2 backslashes have disappeared from one line of this script. In the "tell myEmail" statement near the end of the script, the second line should read, "set content to ("\"" & SelectedText & "\"" & return & return & "More at " & PageURL)". As brucio observes, without the backslashes there will be a syntax error.

Also, thanks robg for pointing out the correct place to put the script!



[ Reply to This | # ]
An AppleScript to send an email about a page in Safari
Authored by: vwgtiturbo on Feb 06, '08 11:05:29AM

Good point. Removing the " worked, but this way is the intended use, I suspect, as it puts quotes around the selected text (and my misinformation of removing the third " adds the selected text to the body of the email, but without quotes). Obviously, I am a rookie :)



[ Reply to This | # ]
An AppleScript to send an email about a page in Safari
Authored by: brucio on Feb 06, '08 11:19:34AM
Yikes! Now I get an Applescript error: "Mail got an error: NSReceiverEvaluationScriptError: 4"

with "index" highlighted at "set index to 1"

Any ideas what that's about?

[ Reply to This | # ]

An AppleScript to send an email about a page in Safari
Authored by: egoham on Feb 06, '08 11:39:07AM

I get that error if I Run the script from Script Editor. However, if run from the Scripts menu in the menu bar the error is not generated.

I still have problems with the script.

I corrected the contents back to the original script with the additions of the backslashes, noted by the OP. Complies just fine. Save it.

Highlight some text in Safari. Run the script, Mail comes to the front. Doesn't create a new message.



[ Reply to This | # ]
An AppleScript to send an email about a page in Safari
Authored by: brucio on Feb 06, '08 01:06:01PM

Yes, I was trying to run it from Script Editor. Fixed. But now I have the same problem; Mail.app comes to front, but no message.



[ Reply to This | # ]
An AppleScript to send an email about a page in Safari
Authored by: tomschmidt on Feb 06, '08 12:05:09PM

I can't respond to the "Mail got an error: SReceiverEvaluationScriptError: 4" issue or the message not created problem, since the script works on my machine. For what it's worth, I use Script Editor 2.2, Applescript 2.0, and OS X 10.5.1.



[ Reply to This | # ]
An AppleScript to send an email about a page in Safari
Authored by: brw3sbc on Feb 07, '08 08:00:46AM
This previous hint may be of interest to those reviewing the current hint:

http://www.macosxhints.com/comment.php?mode=view&cid=62124

[ Reply to This | # ]
An AppleScript to send an email about a page in Safari
Authored by: dzurn on Feb 07, '08 08:01:30AM
Or just bookmark this Javascript and it will do the same thing, except it also removes extraneous whitespace from the selected text.

Copy the code below, create a bookmark and paste.

javascript:location.href='mailto:?SUBJECT='+document.title+'&BODY=I thought you might be interested in this: \r'+escape(location.href)+' \r \r '+escape(document.getSelection().replace(/\s/g,' ').replace(/ {2,}/g,' '))

(Created from bookmarklets scraped from www.bookmarklets.com)

---
Madness takes its toll.
Please have exact change.

[ Reply to This | # ]