10.6: Create a tri.im URL shortening Service

Sep 14, '09 07:30:00AM

Contributed by: elldove

One of the great new features of Snow Leopard is the ability thru Automator (and by extension, AppleScript) to make your own service. Automator can set-up services to accept specific inputs (ie files, images, or text) to act on. If you have selected the appropriate type of input, the service will show up in your context menu.

To create a service which generates a tr.im shortened URL when control-clicking a link in Safari. First, open Automator, and select Create a New Service. Set the Service to receive selected text in Safari. Find Run AppleScript from the Action library on the left, and drag that over to the right half of the Automator window. In that area, paste in the following:

on run {inText}
    tell application "Safari"
    activate
    set searchTerm to inText
    set xNow to do JavaScript " 
var x,
z,
i;
x = '" & searchTerm & "';
if (x != null) {
     x = x.toLowerCase();
     z = document.links;    
     for (i = 0; i < z.length; ++i) {
            if ((z[i].innerHTML && z[i].innerHTML.toLowerCase().indexOf(x) != -1) || z[i].href.toLowerCase().indexOf(x) != -1) {      
          z[i].style.color = '#2e2e2e !important';
            sndT = 'http://tr.im/marklet?url=' + escape(z[i].href);
            popup_window = window.open(sndT);
            }
     }
}
" in document 1
  end tell
end run
Save your service, then go to Safari and find a link you want tr.im-ed. Control-click on that link, and you should see your new service at the bottom of the contextual menu.

[robg adds: I can't get this one to run properly on my machine, so I'm posting it in hopes that the community can figure out the issue -- it looks like it's working, but nothing actually seems to happen. In theory, this could be a very useful hint.]

Comments (19)


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