[Solved] Macro that launches a Google search

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Wolfhart
Posts: 17
Joined: Wed Nov 09, 2016 8:31 pm

[Solved] Macro that launches a Google search

Post by Wolfhart »

Hello,

About a year ago, I asked for help on libreofficeforum.org concerning a macro that launches a Google search of the string that is currently selected in LibreOffice. A poster called echo8hink was so kind to give me the code for the macro, namely the following:

Code: Select all

sub Google_search
    Dim oSelections, oFirstSelection As Object
    Dim sPhrase, sGooglePhrase,sSplitArray(), sSendPhrase As String
    oSelections = ThisComponent.CurrentSelection
    oFirstSelection = oSelections.getByIndex(0 )
    sPhrase = oFirstSelection.String        'assign selected text to string variable
    If sPhrase = "" Then
        MsgBox "Please select text first."
        Exit Sub
      Else 
        'work on string, replace space with +
        sSplitArray() = split(sPhrase," ")
        sSendPhrase = Join(sSplitArray(),"+")   
        'build Google string
        sGooglePhrase = "https://www.google.com/#safe=active&q=%22"+sSendPhrase+"%22"
        'launch browser, full screen window, with Google string
        Shell(ConvertToURL("chromium-browser"),1,sGooglePhrase)
    End If
end sub
Now, I have had an idea for a little improvement of the macro, but I do not know how to implement it myself because I do not know how to write a macro. Since libreofficeforum.org does not exist any more, I could not ask again for help there. I consequently asked for help on ask.libreoffice.org (https://ask.libreoffice.org/en/question ... breoffice/), but there I was told to raise the question here, in this forum.

The idea for the improvement is the following: What the macro currently does is that it takes the string that is selected in LibreOffice and launches the Internet browser to search for the string (which is first wrapped in quotation marks) on Google. (The macro as given above works on Ubuntu with Chromium installed. For other systems or other browsers, the command "chromium-browser" has to be replaced by the command that launches the intended browser on the given system.) The macro is currently intended for cases where the search string consists of several words. (That's why the string is wrapped in quotation marks.) Sometimes, though, I do not want to search for a string that consists of several words, but only for a single word. And I am thinking that, in such cases, it should not be necessary to select the word, but that it should be sufficient to indicate the word by the position of the cursor. In other words, I would like to change the behavior of the macro in the following way: If no text is selected, then the search string should be the word in which (or at the end of which) the cursor is currently located. But if a certain portion of text is selected, the search string should be the selected text.

Can that be done? If so, can someone tell me how the above macro needs to be modified in order to achieve it?

Thanks in advance for your help!
Last edited by Wolfhart on Sat Nov 12, 2016 1:39 am, edited 1 time in total.
LibreOffice 5.1.6.2 on Ubuntu 16.04
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Macro that launches a Google search of the string select

Post by Sébastien C »

Hello,
Sorry for my English, I am French and read just a bit English
Maybe a code like

Code: Select all

Sub GoogleSearch()
 Dim               oCurs As Object
 Dim searchEngineAddress As String, sPhrase As String, sGooglePhrase As String

 searchEngineAddress = "https://www.google.com/#safe=active&q="

 sPhrase = thisComponent.currentSelection.getByIndex(0).String ' Assign selected text to string variable

 If sPhrase = "" Then
          oCurs = thisComponent.text.createTextCursorByRange(thisComponent.currentController.viewCursor)
  oCurs.gotoStartOfWord(false) : oCurs.gotoEndOfWord(true)
  sGooglePhrase = searchEngineAddress & oCurs.string
 Else
  sGooglePhrase = searchEngineAddress & "%22" & join(split(sPhrase, " "), "+") & "%22"
 End If

 shell(convertToURL("chromium-browser"), 1, sGooglePhrase)     ' Launch browser, full screen window, with Google string.
End Sub
can help
You will say if enjoy
:D
 Edit: Another idea...

You can rewrite the first line of code

Code: Select all

Dim               oCurs As Object
by the line

Code: Select all

Dim               oCurs As Object,  oShell As Object
and the last line

Code: Select all

shell(convertToURL("chromium-browser"), 1, sGooglePhrase)     ' Launch browser, full screen window, with Google string.
by the two lines

Code: Select all

 oShell = createUNOService("com.sun.star.system.SystemShellExecute")
 oShell.execute(sGooglePhrase, "", 1)                          ' Launch browser, full screen window, with Google string.
From now on, it will no longer be necessary to adjust the macro for one browser rather than another since LibreOffice will launch the default browser for the operating system. 
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
Wolfhart
Posts: 17
Joined: Wed Nov 09, 2016 8:31 pm

Re: Macro that launches a Google search of the string select

Post by Wolfhart »

Thank you, Sébastien! Works perfectly.
Merci beaucoup!
LibreOffice 5.1.6.2 on Ubuntu 16.04
Wolfhart
Posts: 17
Joined: Wed Nov 09, 2016 8:31 pm

Re: [Solved] Macro that launches a Google search

Post by Wolfhart »

Salut Sébastien,

About the edit at the end of your reply (the new version that is supposed to launch the default browser): I just tried the macro with the changes you indicate, and indeed it launches the default browser, but without the search for the string, that is, it just launches a new empty window of the browser. Maybe something is missing in the changes you indicate?
LibreOffice 5.1.6.2 on Ubuntu 16.04
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: [Solved] Macro that launches a Google search

Post by Sébastien C »

Hello Wolfhart !

No, I don't understand the mistake...

Try the linked file of this present post and tell me if it do not work.

By me, on M$-XP and GNU-Linux-Mageia, it works.

If not, it is maybe a problem with your OS settings.
Attachments
googleSearch.odt
(66.48 KiB) Downloaded 257 times
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
Wolfhart
Posts: 17
Joined: Wed Nov 09, 2016 8:31 pm

Re: [Solved] Macro that launches a Google search

Post by Wolfhart »

Salut Sebastién,

Yes, you are right, the problem was on the side of the browser. Sorry, and thanks for the reply!
LibreOffice 5.1.6.2 on Ubuntu 16.04
Wolfhart
Posts: 17
Joined: Wed Nov 09, 2016 8:31 pm

Re: [Solved] Macro that launches a Google search

Post by Wolfhart »

Salut Sebastién (if you are still around),

There is a little problem with the macro, and I wanted to ask you whether it can be fixed: When the word I want to look for on Google is in a footnote or in a table, the method of putting the cursor in the middle or a the end of the word does not work. I get the following error message:

Code: Select all

BASIC runtime error.
An exception occurred 
Type: com.sun.star.uno.RuntimeException
Message: End of content node doesn't have the proper start node.
But the method of selecting the word does work, that is, there is no error message then and the Google search is launched as planned. So my question is this: Can the macro be modified such that the method of putting the cursor in the middle or at the end of the word also works in footnotes and tables?
LibreOffice 5.1.6.2 on Ubuntu 16.04
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: [Solved] Macro that launches a Google search

Post by Sébastien C »

Hello Wolfhart !

The description of the problem you are giving is perfectly clear; I reproduce it.
My week is too busy for me to focus on this issue. If some of you all (ahem Zizi64...!) know directly how to fix the problem, I have nothing against it!
I repost the same file with footnotes and a table to do the tests. But without (yet) the solution.
See you soon ; anyway.
:D
Attachments
googleSearch.odt
(67.56 KiB) Downloaded 210 times
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: [Solved] Macro that launches a Google search

Post by hubert lambert »

Hello,
Wolfhart wrote:When the word I want to look for on Google is in a footnote or in a table, the method of putting the cursor in the middle or a the end of the word does not work.
The text object of a footer, a table cell or a frame is different from the main text object. Thus this line will fail when the view cursor is not within "ThisComponent.Text":

Code: Select all

oCurs = thisComponent.text.createTextCursorByRange(thisComponent.currentController.viewCursor)
Try this instead:

Code: Select all

 [...]
 oSelection = thisComponent.currentSelection(0)
 if not oSelection.supportsService("com.sun.star.text.TextRange") then exit sub  ' avoid error if selection is a graphic or any other non-text object
 sPhrase = oSelection.String ' Assign selected text to string variable
 If sPhrase = "" Then
  oCurs = oSelection.text.createTextCursorByRange(thisComponent.currentController.viewCursor)
  [...]
Regards.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: [Solved] Macro that launches a Google search

Post by Sébastien C »

A very BIG Thank tou yo hubert lambert !
It is cool to be Frenchies here...
I inserted your code into mine; And it is, inevitably, much better.
All in all ...
;-)

Code: Select all

Sub GoogleSearch()
 Dim               oCurs As Object,  oShell As Object
 Dim searchEngineAddress As String, sPhrase As String, sGooglePhrase As String

 searchEngineAddress = "https://www.google.com/#safe=active&q="
             sPhrase = thisComponent.currentSelection.getByIndex(0).String       ' Assign selected text to string variable
         mySelection = thisComponent.currentSelection(0)

 If Not mySelection.supportsService("com.sun.star.text.TextRange") Then Exit Sub ' Avoid error if selection is a graphic or any other non-text object

 sPhrase = mySelection.String                                                    ' Assign selected text to string variable

 If sPhrase = "" Then
          oCurs = mySelection.text.createTextCursorByRange(thisComponent.currentController.viewCursor)
  oCurs.gotoStartOfWord(false) : oCurs.gotoEndOfWord(true)
  sGooglePhrase = searchEngineAddress & oCurs.string
 Else
  sGooglePhrase = searchEngineAddress & "%22" & join(split(sPhrase, " "), "+") & "%22"
 End If

 oShell = createUNOService("com.sun.star.system.SystemShellExecute")
 oShell.execute(sGooglePhrase, "", 1)                                            ' Launch browser, full screen window, with Google string.
End Sub
Attachments
googleSearch.odt
(67.62 KiB) Downloaded 270 times
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
Wolfhart
Posts: 17
Joined: Wed Nov 09, 2016 8:31 pm

Re: [Solved] Macro that launches a Google search

Post by Wolfhart »

Thank you very much, Hubert and Sébastien! Works perfectly now. Merci beaucoup!
LibreOffice 5.1.6.2 on Ubuntu 16.04
Post Reply