user3847051
user3847051

Reputation: 23

How to access to web page using Groovy?

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

Answers (1)

tim_yates
tim_yates

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

Related Questions