An AppleScript to send an email about a page in Safari

Feb 06, '08 07:30:03AM

Contributed by: tomschmidt

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.]

Comments (17)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080129120714204