I heart groovy

Posted: January 19th, 2011 | Author: | Filed under: code | Tags: | No Comments »

Reason number 2343242354 why i love groovy – I had a bunch of urls and I needed to run NSLOOKUP on them. I had a couple of options, write a shell script, do each by hand, write it in java (eeek) or write it in groovy. I chose groovy.

["https://www.googleadservices.com",
"https://roia.biz",
"https://www.emjcd.com",
"https://nmargin.com",
"https://ssl.google-analytics.com",
"https://www.ifactz.com",
"https://secure.interclick.com",
"https://r.turn.com",
"https://secure.trafficmp.com",
"https://secure.fastclick.net",
"https://ads.revsci.net",
"https://conv.opt.fimserve.com",
"https://ds.contextweb.com",
"https://ad.yieldmanager.com",
"https://beacon.dedicatednetworks.com",
"https://flex.atdmt.com",
"https://seal.verisign.com",
"https://sealserver.trustkeeper.net"].each{ url ->
println "nslookup ${url[8..-1]}".execute().text
}

Ok let’s look whats going on here. First thing you’ll notice if you’re coming from the java world is there is no class definition, there’s an implicit class created for you so for simple scripts especially run once scripts like this, it’s not necessary.

The first part is a list which is created with []

["https://www.googleadservices.com",
"https://roia.biz"]

Next we are calling a method on the list called .each which accepts a closure which is in this case

{ url ->
println "nslookup ${url[8..-1]}".execute().text
}

Contained in this closure is what I really needed to do, which was do an NSLOOKUP on each url, but only the domain, I had to strip out the protocol (I know this could have easily been done with a search and replace in my editor but then you don’t have any example code)

We start by creating a groovy string – denoted by the"" which allows us to substitute the url in with the ${url} syntax. Another niceity is to omit the {}’s if you do not need to run any additional methods on the variable you are substituting – had the https:// not been in the url i could have written it like so:

println "nslookup $url".execute().text

What you’ll notice is by using the array index notation with a range I am able to get all chars starting at position 8 and go all the way to the end (in groovy -1 means the end of the string, -2 would be 2 from the right -3, 3 from the right etc. – no more subString(pos, string.len - pos)) This same syntax also works with lists

Now that I have the portion of the domain I need, we call .execute() on it. In groovy a .execute method is added to the String class and when doing so the entire string will be passed to the command line and executed. The following would print out the directory listing where the script was running.

"ls -la".execute().text

Finally we call .text (equivalent of .getText()) since .execute only returns a java.lang.UNIXProcess and the text property gives you back the output

Also note that println is the equivalent of System.out.println("text") – it’s just another groovy shortcut.


Alfred : A better ⌘ SPACE

Posted: January 19th, 2011 | Author: | Filed under: mac | Tags: , , | No Comments »

Over the past few weeks I’ve been using Alfred on both my work and home macs.  It’s a replacement for quicksilver which doesn’t seem to have been updated lately and to be quite honest I never really “got”.  I used it to launch applications and search with finder, but other than that not much else.  After quicksilver I just started using good old ⌘ SPACE to open spotlight and use it as my application launcher and for that alone it works just fine. It also replaces spotlight’s file searching using find {file} and open {file} to find and open files on disk – you can include / exclude the types of files it searches as well.  Alfred adds to this a bunch of web searching by default, type google some term and a new tab opens in the default browser (Chrome in my case) with your search term.  There are built in searches for gmail, twitter, amazon, wikipedia and many others which each have their own keyword and search term combination.

At work I often find myself doing certain search tasks (more go to url.com/x/ where x is a variable) all the time. Enter alfred’s custom searches.  You define your keyword, your url and it replaces a token “{query}” with your search term.  Now opening a track ticket is ⌘ SPACE t 12345 which translates to trac.site.com/ticket/12345.  I have also added trac searching, stack overflow searching and a few others.

As well as app launching and searching it’s also got commands for various system commands such as activate screensaver, logout, lock, empty trash, and shutdown (yes there are already shortcuts for these, this just provides alternate ways of doing it). Another great feature is access the OSX dictionary, spell {word} will give you options on how to spell the word and define {word} will give you its definition.

The product is still in beta but it seems very well thought out and has given me no problems as of yet.

ps: As a bonus I changed the default behavior of ⌘ SPACE in OSX preferences to ⌘ SHIFT SPACE and mapped alfred to ⌘ SPACE instead