Macro: website lookup via system browser

Now that LO has customizable context menus (5.2), we are open to conveniently adding website lookups! I pieced together this macro for a dictionary lookup:

Sub Dictionary_Lookup ' TFD
    Dim SysShell As Object
    Dim oCursor, oSeL
    oCursor = ThisComponent.CurrentController.ViewCursor
    oSel = oCursor.getString()

    SysShell = createUNOService("com.sun.star.system.SystemShellExecute")
    SysShell.execute("/usr/bin/flashpeak-slimjet", "www.thefreedictionary.com/" & oSel, 0)	
End Sub   	

I would be open to any suggestions for improving the macro. In particular, I would like to know how to call the system browser rather than spec the browser path, if that is possible. I’m on Linux.

Just tried this - LO v5.1.3.2, xubuntu 16.04, Firefox 47.0:

Sub Dictionary_Lookup
Dim oCursor, oSeL
oCursor = ThisComponent.CurrentController.ViewCursor
oSel = oCursor.getString()
Shell "firefox http://www.thefreedictionary.com/" & oSel
End Sub

Also tried other sites and all worked OK.

Edit: When multiple words are to be selected, this dictionary site needs words joined by + sign. This modified macro adjusts for that:

Sub Dictionary_Lookup
Dim oCursor, oSeL
Dim Result As String  
oCursor = ThisComponent.CurrentController.ViewCursor
oSel = oCursor.getString()
Result = join(split(oSel, " "), "+")
Shell "firefox http://www.thefreedictionary.com/" & Result
End Sub

The selected string is examined for spaces and replaces with the plus sign. Other sites may need similar modifications depending upon the requirements.

Thanks you, Ratslinger. Appreciate you taking a look at this.

Your macro is quite a bit more streamlined than my attempt. It seems to have a problem with multiple-word phrases, though. In that case it opens the first word at thefreedictionary, and the second work at its own .com site. For instance “Happy childhood” goes to the happy definition at TFD, and to childhood.com. It does a similar thing using the Vivaldi browser, though the second tab does not get .com appended to it.

Thanks again

Had not looked at multiple words - will try and get back if any success.

Very nice, Ratslinger. Thanks very much.

Paul

On windows, using SystemShellExecute where the URL is passed as the first argument the web page will be opened using the systems default web browser.

Not sure if this works on Linux, may be worth a try.

Sub Dictionary_Lookup
Dim oCursor, oSeL
Dim Result As String  

oCursor = ThisComponent.CurrentController.ViewCursor
oSel = oCursor.getString()
Result = replace(oSel, " ", "+")
SysShell = createUNOService("com.sun.star.system.SystemShellExecute") 
SysShell.execute("www.thefreedictionary.com/" & Result, "", 0) 
End Sub

@mark_t didn’t work on Linux. Thanks for replace though - could not remember.

How about xdg-open?
SysShell.execute("SysShell.execute(“xdg-open”,“www.thefreedictionary.com/” & Result, 0)
or
Shell “xdg-open http://www.thefreedictionary.com/” & Result
Reference
http://www.dwheeler.com/essays/open-files-urls.html

Shell “xdg-open http://www.thefreedictionary.com/” & Result
works. Thanks for the info.

Awesome, Mark. I’m using the xdg-open command, and I find the createUNOService is not necessary. Thanks.