10.6: A Service to shorten URLs

Aug 26, '10 07:30:01AM

Contributed by: renaultssoftware

Snow Leopard only hintServices are all the rage in 10.6 now. Since Apple let non-developer users make their own Services in Automator, people (like me) have been suggesting many services.

This hint is how to make a shortened URL using a lesser-known service named qgf.in. You can use other services too, as long as they have an API (usually the syntax is 'http://shortener.name/api?url=http://foo.bar').

Credits to all the people in the MacOSXHints Forums who helped with writing of the Perl script.

Download the pre-built perl modules and source code from CPAN here (or here is that same link after being shortened using the Service), unpack it, and put it somewhere in your Home folder.

Next, open your favorite text editor and enter the following code:

#!/usr/bin/perl
#
use LWP::Simple;

my $url = shift || usage();

getprint("http://qgf.in/api.php?url=$url");
print "\n";

sub usage {
   print "Usage:\n";
   print "GetURL \n";
   exit(1);
}
Save it to the same folder as the source code, with a name like 'shorten.pl' (no quotes). Don't put the script inside the source-code folder, but at the same level as it. Now in Terminal, type chmod +x without quotes. Don't press Return yet! In Finder drag and drop the Perl script file you just saved to the Terminal so that it inserts the path, and then press Return. This will make that file executable.

You could try giving it a spin in Terminal by typing:

/path/to/script/shorten.pl http://www.apple.com

Of course, replace the dummy path with the actual path, which you know from using the previous Terminal command. For long term usage, you can place the script somewhere in your default command path.

In Automator, make a new Service. Set the top bar to read 'Service receives selected in.' Add a Run AppleScript action with the following script:
on run {input, parameters}
	return (do shell script "/path/to/script/shorten.pl " & (the first item of the input))
end run
Once again, replace the dummy path with the actual path.

Save the Service as Shorten URL or something creative.

Now, you can right-click any URL, and shorten it more-or-less quickly. (It's not very fast though. Side effect of using the system.)

[crarko adds: I tested this, and it works as described. If you haven't used our MacOSXHints forums you may wish to give them a look. They do have a separate account database, so it uses a different registration than the main site.]

Comments (10)


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