I no longer have any faith that any API will stick around all that long. Search APIs are becoming fewer and fewer. Google1 still offers the Ajax Search API, but it’s not convenient to use with System Services and such because it requires non-default libraries to work in Ruby and other scripting languages. It’s easy in PHP, though. It’s not my favorite language to use for local scripting, but it was the easiest solution to the problem. I think…

Took me a couple of tries, but I got a Bash script working (calling PHP because PHP isn’t available as an interpreter in Automator) that you can use as a Lucky Link service that takes input on STDIN and returns a Markdown Link with the passed text. Perfect for System Services. I put together a 3-pack: Lucky Link, Wikipedia Link and Link Blog Post. The “Link Blog Post” Service requires editing to set the blog to search. Just put your site URL into the $site variable and you’re good to go.

I’m revising the Blogsmith Bundle2 search features (which are currently broken) with this, which should revive the most useful parts of the bundle. I’m also porting it to Sublime Text 2, but progress has been slow. I’ll get there.

Here’s the basic script from the Services, followed by a wikipedia linking version:

luckylink.shraw
"
cat | php -r '
$input = trim(file_get_contents("php://stdin"));
$terms = urlencode($input);
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&filter=1&rsz=small&q=$terms";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "G-LINK");
$body = curl_exec($ch);
curl_close($ch);

$json = json_decode($body,TRUE);
$output = "[$input](".$json["responseData"]["results"][0]["unescapedUrl"].")";
printf("%s",$output);'

Here’s the download for the 3 services:

Google Lucky Link Services v1.1

Lucky linking System Services that return Markdown links.

Published 09/26/12.

Updated 09/26/12. Changelog

DonateMore info…

  1. Gabe won’t be happy about this. 

  2. Link created with the Lucky Link service. It took less than two seconds and got it right on the first try.