Automatically send a TinyURL from Safari via Mail.app

Feb 08, '05 09:41:00AM

Contributed by: daydream

TinyURL is a cool concept. Many mail clients will either wrap or otherwise distort a URL, depending on the size or characters it may contain. TinyURL creates a URL that you can use to represent a longer one, especially useful for sending to people in mail. The normal process is to submit a URL, at which point you'll be given back a TinyURL to use. Normally you'd take the TinyURL they give and copy and paste into an emai. While it is handy, I decided to figure out a way to do it automatically, using AppleScript, and bypassing the copy and paste procedure.

Read the rest of the hint for the script and notes...

The AppleScript:

-- ####################
-- #  -- eMail tinyURL.scpt --
-- #  script to automagically get
-- #  and paste a tinyURL from 
-- #  the active Safari window 
-- #  into Mail.app.             
-- #  --  by nekvas 2005
-- ####################

-- get the url
tell application "Safari"
  set bigURL to the URL of document 1
end tell

-- build the command for curl...quoting the url
set curlCMD to ¬
 "curl --stderr /dev/null \"http://tinyurl.com/create.php?url=" & bigURL & "\""

-- grep for tinyurl value
set grepCMD to "| grep 'tinyurl value='"

-- use awk to pull the url out of the retuned page, using " as a delimiter
set awkCMD to "| awk -F'\"' '{ print $2 }'"

-- build the command
set shellCMD to curlCMD & grepCMD & awkCMD

-- run the script and get the result
do shell script shellCMD
set tinyURL to the result

-- pass the value to Mail and put it in the body
tell application "Mail"
  activate
  set this_message to make new outgoing message with properties ¬
   {visible:true, subject:"check out this page", content:("<" & tinyURL & ">")}
end tell
First, a few notes: I'm sure this could be done a better way, but being that I'm not an AppleScript god, I decided to get this done with the toolbox approach, using curl, grep and awk.
Directions:
  1. Copy the script text from this hint.
  2. Paste it into a new script in Script Editor.
  3. Save it into ~/Library/Scripts/Applications/Safari as a Script, and name it whatever you like. You may or may not have to create some or all of these folders.
From there, you should be able to access it using the AppleScript menu in Safari. Selecting the script will get a TinyURL for the foremost browser window, create a new mail with Mail.app, and paste the TinyURL into the body. Hope this works for everyone.

[robg adds: This worked for me. If you like the TinyURL service, you might want to check out the TinyRoulette hint from a while back. Quite fun when you have some time to waste -- though you're not sure where you might wind up, so it's not really work safe!]

Comments (21)


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