Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.6: A Service to shorten URLs System 10.6
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.]
    •    
  • Currently 1.89 / 5
  You rated: 5 / 5 (18 votes cast)
 
[7,634 views]  

10.6: A Service to shorten URLs | 10 comments | Create New Account
Click here to return to the '10.6: A Service to shorten URLs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: A Service to shorten URLs
Authored by: S Barman on Aug 26, '10 09:34:37PM
Just to be UNIX correct and to shorten the script:
#!/usr/bin/perl
use LWP::Simple;

my $url = shift || die "usage: $0 url_to_shorten\n";

getprint("http://qgf.in/api.php?url=$url");
print "\n";
In this case, using the die function prints the message to STDERR and does end with a non-zero return code, both being more UNIX friendly.

Scott

[ Reply to This | # ]

10.6: A Service to shorten URLs
Authored by: fulmar2 on Aug 26, '10 10:50:37PM
This sounds like a fun project... but have you considered using this bookmarklet?

Tiny URL

Just Drag the above into your bookmarks bar, and click it anytime you want to shorten a URL, you're golden. It's fast, too.

[ Reply to This | # ]
10.6: A Service to shorten URLs
Authored by: renaultssoftware on Aug 27, '10 04:56:18AM

What if you're somewhere like Adium.app or Mail?

---
http://groups.google.com/group/applescripters/
http://qgf.in/
http://www.kompilesoft.info/



[ Reply to This | # ]
10.6: A Service to shorten URLs
Authored by: prramesh on Aug 27, '10 02:02:32AM

I use Cloudapp.



[ Reply to This | # ]
10.6: A Service to shorten URLs
Authored by: yesiamnhoj on Aug 27, '10 05:01:05AM

I'd use cURL in the Automator Applescript which is pre installed in OSX. You'd then not need the Perl script.

on run {input, parameters}
return (do shell script "/usr/bin/curl 'http://qgf.in/api.php?url=" & (item 1 of the input) & "'")
end run

Best wishes
John M



[ Reply to This | # ]
10.6: A Service to shorten URLs
Authored by: renaultssoftware on Aug 27, '10 05:27:52AM

I thought of doing that, but doesn't it download the webpage instead of reading the file? Or am I confused?

---
http://groups.google.com/group/applescripters/
http://qgf.in/
http://www.kompilesoft.info/



[ Reply to This | # ]
10.6: A Service to shorten URLs
Authored by: yesiamnhoj on Aug 27, '10 05:36:16AM

Without any options cURL returns the contents of the URL. In this case it would be the shortened URL.

John M



[ Reply to This | # ]
10.6: A Service to shorten URLs
Authored by: renaultssoftware on Aug 27, '10 05:52:11AM

Doggone it! I should've known. It would make the hint above that much easier. Why don't you submit that, saying it's an updated version? I'll give my permission, it's recorded here.

---
http://groups.google.com/group/applescripters/
http://qgf.in/
http://www.kompilesoft.info/



[ Reply to This | # ]
10.6: A Service to shorten URLs
Authored by: dclauzel on Sep 08, '10 02:12:16AM

There is even simpler : I am using Automator to directly run a UNIX script and to place the result into the system pasteboard. That skips the applescript part.

See this post for details.

[ Reply to This | # ]

10.6: A Service to shorten URLs
Authored by: philostein on Sep 08, '10 03:57:59AM
I wrote a Quicksilver action based on yesiamnhoj's Terminal command: cURL.scpt

Put it in ~/Library/Application Support/Quicksilver/Actions and relaunch Quicksilver.
Get a url in the first pane, press tab and find the 'cURL' action. Hit enter and get the shortened url in the clipboard and QS's first pane.

I find QS a lot faster than Automator for this kind of thing. Thanks to renaultssoftware for posting the hint, and to yesiamnhoj for the command. I've been looking for this for ages!

[ Reply to This | # ]