OOoBasic crash course: Creating a lookup macro

66

Author: Dmitri Popov

Learning OOoBasic can be a bit like learning a foreign language. If you have the time and ambition to communicate fluently, you can spend months or even years studying grammar and expanding your vocabulary. But sometimes you just need some basic skills to get you through daily situations. In this case, a crash course that introduces you to some basic principles and building blocks of the language would do just fine. The same is true for OOoBasic — if you need to write a simple macro that makes your daily computing life a bit easier, you don’t have to spend time reading about methods, routines, and object properties. What you need is some working examples and an explanation of how they work. And if you are looking for something like that, then read on.

Often, when you are working in OpenOffice.org Writer, you want to find a synonym, look up a term in Wikipedia, or translate a word into another language. These operations often involve several steps: copy a word from your document, open a browser, go to a Web site, paste the word into the search field, etc. A simple macro can dramatically simplify this process and save you a lot of clicking, copying, and pasting. This macro helps not only to reduce unnecessary workload, but also demonstrates how you can make OpenOffice.org talk to external applications.

The main building block of the macro is the following code:


Sub LookupMacro
Shell ("firefox", 1)
End Sub

This macro sends the “firefox” shell command, which instructs the system to launch the specified application (in this case, Firefox). (If you are using Windows, you have to provide a full path to the Firefox executable: C:Program FilesMozilla Firefoxfirefox.exe.) In a similar manner, you can launch virtually any application directly from within OpenOffice.org. For example, use the wnb command to launch the WordNet browser instead of Firefox (provided that WordNet is installed on your machine).

Knowing how to launch an external application is a big step forward, but the macro is far from finished. First of all, you have to add code that allows the macro to grab the selected word in the currently opened document. This can be done with just two lines of code:


oDoc=thisComponent
oLookup=oDoc.CurrentController.getViewCursor

The first line tells the macro that the currently opened document is the one it should work with, while the second line grabs the selected word.

Now that the macro has the word, you can add the code that points the browser to the desired online reference. Let’s say you want to look up the word in Wikipedia. If you take a closer look at Wikipedia’s URL, you will notice that it has the following form: http://en.wikipedia.org/wiki/ plus the article’s name; for example, http://en.wikipedia.org/wiki/Monkey. So all you have to do is to modify the Shell line accordingly, so it looks like this:


Shell("firefox",1, "http://en.wikipedia.org/wiki/" & oLookup.String)

This tells the system to launch Firefox and point it to the specified link, appending the oLookup string (the selected word) to the end of the URL.

Now you can use the macro to look up words in Wikipedia — but before you leave the editor, it’s worth spending a minute or two to make the macro a bit more user-friendly. Right now the macro works only if the user has selected a word, so you might want to modify the macro so it displays an error message if nothing is selected:


If oLookup.String ="" then MsgBox "Please select a word to look up" : End

This line shows the specified error message and exits the macro if nothing is selected. Here is the final macro:


Sub WikipediaLookup
oDoc=thisComponent
oLookup=oDoc.CurrentController.getViewCursor
If oLookup.String ="" then MsgBox "Please select a word to look up" : End
Shell("firefox",1, "http://en.wikipedia.org/wiki/" & oLookup.String)
End Sub

What’s next?

Although this macro is rather simple, you can use it to look up words in virtually any desktop application and online reference. For example, if you want to look up a word in WordNet, you just have to modify the Shell command as follows:


Shell("wnb",1, oVC.String)

Want to translate the selected word from English to German using the LEO online dictionary? No problem, just tweak the Shell command so it uses the appropriate URL:


Shell("firefox",1, "http://dict.leo.org/ende?lp=ende&lang=en&searchLoc=0&cmpType=relaxed&sectHdr=on&spellToler=on&search=" & oLookup.String)

Actually, you can create a “multi-lookup” macro by simply copying, pasting, and modifying the WikipediaLookup block, like this:


Sub WikipediaLookup
oDoc=thisComponent
oLookup=oDoc.CurrentController.getViewCursor
If oLookup.String ="" then MsgBox "Please select a word to look up" : End
Shell("firefox",1, "http://en.wikipedia.org/wiki/" & oLookup.String)
End Sub
Sub LEOLookup
oDoc=thisComponent
oLookup=oDoc.CurrentController.getViewCursor
If oLookup.String ="" then MsgBox "Please select a word to look up" : End
Shell("firefox",1, "http://dict.leo.org/ende?lp=ende&lang=en&searchLoc=0&cmpType=relaxed&sectHdr=on&spellToler=on&search=" & oLookup.String)
End Sub

Finally, instead of running the macro manually by choosing Tools -> Macros -> Run Macro and navigating to the desired item, you can create a separate toolbar. Choose Tools -> Customize, click on the Toolbar tab, and press the New button. Give the toolbar a name, select OpenOffice.org Writer from the Save in drop-down list, and press OK. Now press the Add button, and select the WikipediaLookup macro. Repeat this step for LEOLookup and other macros.

That’s all there is to it. Now you know how to open external applications from within OpenOffice.org, grab the selected word, and look it up in virtually any online reference.

Dmitri Popov is a freelance writer whose articles have appeared in Russian, British, and Danish computer magazines.