Long and shortened URL scripts
November 26, 2007 at 10:59 PM by Dr. Drang
When I wrote my first script for automatically shortening the URL of the current Safari page and putting it on the clipboard, it was short and fairly easy to understand. It was written in Python (my current language of choice for most scripting) with two short calls out to AppleScript via the osascript
command. Then I began adding bells (literally—I added code to sound the system alert) and whistles and the script grew. Then I learned that the additions that worked on OSX 10.4 wouldn’t work on 10.5 and I needed two versions. Today I learned that what worked on 10.4.10 wouldn’t work on 10.4.11. What a mess!
Also, I wrote a simpler script for just putting the URL of the current page onto the clipboard—with no shortening. This was also a Python program and was just a copy of the previous script with the shortening bits and the alert bits edited out. It, too, made calls out to AppleScript; when I finished I realized that it was pretty much all AppleScript with a Python wrapper around it. Stupid.
A few people have emailed me with compliments (more on the idea behind the scripts, I think, than on their execution), bugs, and links to their improvements. Matt McVickar made some serious improvements, generalizing the shortening script to work with Firefox, Camino, and Opera in addition to Safari. His version of the shortening script is written entirely in AppleScript, and it became clear to me that my scripts should have been purely AppleScript in the first place. I had started with Python because that’s what I’m programming in nowadays, not because it was the best tool for the job.
(If you’re wondering why anyone would want scripts that do these jobs, read the text of my original post. In short, I’m often writing—in TextMate or Mail or Twitterific or iChat—about a web page that’s showing in Safari. These scripts give me a way of quickly getting and pasting the URL [original or shortened] without switching out of the application I’m writing in.)
So here is my final (I hope) script for getting the URL of the frontmost web page in Safari and putting it on the clipboard, ready for pasting. I call it “furl.scpt” (front URL) and have it saved in my ~/Library/Scripts folder
1.
tell application "Safari"
set longURL to URL of front document
end tell
set the clipboard to longURL as text
Much cleaner than the Python version.
Here’s the improved script for getting the shortened URL of the frontmost web page in Safari and putting it on the clipboard. It’s called “surl.scpt” (shortened URL) and is also saved in ~/Library/Scripts
. (Jason Snell actually beat me to this; see the update at the bottom of the post.)
tell application "Safari"
set longURL to URL of front document
end tell
set cmd to "curl http://metamark.net/api/rest/simple?long_url=" & longURL
set shortURL to do shell script cmd
set the clipboard to shortURL as text
beep
Since AppleScript can’t (as far a I know) send an HTTP GET request, I have it call curl
through the do shell script
command. Thus, there is still some mixed-language programming in this script, but less than in the Python version.
I put beep
at the end of the script after a brief email exchange with John Gruber wherein he pointed out that Metamark sometimes takes a few seconds to deliver the shortened URL. The beep is there to let me know that the shortened URL is ready and on the clipboard. There’s no beep at the end of furl.scpt
because that script doesn’t make an internet request and always runs quickly.
If you want to use TinyURL instead of Metamark as the shortening service, replace the set cmd...
line with
set cmd to "curl http://tinyurl.com/api-create.php?url=" & longURL
Assuming you have Quicksilver set up to catalog your ~/Library/Scripts
folder—QS refers to it as “Scripts (User)”—you can now call these scripts from Quicksilver like this:
If you find yourself using the furl and surl scripts regularly, you might want to create a Quicksilver trigger that binds them to a keyboard shortcut.
You can click on the image to see a full-sized version of the screen shot.
With luck, I won’t need to rewrite these scripts again.
Update
Not a rewrite, but I did think of another helpful variation on these scripts. I’ve called it scurl.scpt
(shortened clipboard URL), and it takes a URL on the clipboard, shortens it, and puts the shortened version on the clipboard. I use it when there’s a link on a page that I want to shorten without following the link. Here it is.
set longURL to the clipboard
set cmd to "curl http://metamark.net/api/rest/simple?long_url=" & longURL
set shortURL to do shell script cmd
set the clipboard to shortURL as text
beep
As with surl.scpt
, you can use TinyURL instead of Metamark by changing the set cmd...
line.
Clearly, this script will only work if the clipboard contains a valid URL before you run it. If the clipboard contains something else, the script will either fail or return garbage. I don’t see this as a big problem—it’ll be pretty obvious when it happens—and don’t see much point in adding a lot of error checking code.
I should mention that I’ve tested these scripts on Tiger (10.4.11) and Leopard (10.5). I think they’d work on earlier versions of Tiger, but I don’t have a computer with an earlier version.
Further update
I just looked at the AppleScript source code of Jason Snell’s version of surl
, which he calls Tiny URL. Except for the variable names and the beep
command at the end, our two scripts are identical. I’d seen the link to his work—which came out the day after my initial, inferior Python version—before I wrote this post, but hadn’t looked into it before today. He deserves credit for being the first to write a clean version of this script.
Last (I hope) update (2/9/09)
The surl
and scurl
scripts, as written here, will fail with certain characters in the original (long) URL. Improved versions can be found here.
1 The ~ is a Unixism that comes from shell syntax. It means the user’s home directory. Thus, ~/Library/Scripts
is just a shorter way of writing /Users/username/Library/Scripts
.