Groovy web console

subscribe to the feed Subscribe
to this
site
a (via #groovywebconsole)
tweet this snippet Tweet
this
script

a

Published 8 months ago by a with tags a
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
String.metaClass.browse {
    def handler = [ 
      (~/^Mac OS.*/)  : { "open $it".execute() },            
      (~/^Windows.*/) : { "cmd /C start $it".execute() },
      (~/.*/)         : {
         // assume Unix or Linux                         
         def browsers = [ 'firefox', 'opera', 'konqueror', 'epiphany', 'mozilla', 'netscape' ]
         // find a browser we know the location of
         def browser = browsers.find {
           "which $it".execute().waitFor() == 0
         }
         // and run it if one found
         if( browser )
           "$browser $it".execute()
      }
    ]
    def k = handler.find { k, v -> k.matcher( System.properties.'os.name' ).matches() }
    k?.value( delegate )
}

"http://www.google.com".browse()