Reputation: 23
i try to connect to webpage using Groovy,
i tried this, it works when the URL is www.google.fr
String.metaClass.browse {
def handler = [(~/^Mac OS.*/) : { "open $it".execute() }, (~/^Windows.*/) : { "cmd /C start $it".execute() },
(~/.*/) : {
//--- assume Unix or Linux
def browsers = [ 'firefox'.'chrome' ]
//--- 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 )
}
www.google.fr".browse.()
if i put URL which download a file it throw Compilation error. Thank you for your help.
Upvotes: 1
Views: 4877
Reputation: 171114
If you want to add it as a method to the String class, you can do:
String.metaClass.browse = { ->
java.awt.Desktop.desktop.browse(new URI(delegate))
}
Then calling
"http://www.google.com".browse()
Will open your default browser
Upvotes: 5