OpenOffice.org Basic crash course: Saving user settings

93

Author: Dmitri Popov

The ability to save user settings can come in handy if you want to make your OpenOffice.org solutions more flexible, efficient, and user-friendly. In this article, we take a look at how to save user settings in a plain text file and then retreive them from there.

In the very first installment of our ongoing crash course series we talked about how to launch external applications using the OOo Basic Shell function. This allows you, for example, to open a link in Firefox directly from within OpenOffice.org:

Shell ("firefox", 1, "http://www.wikipedia.org")

But the code above has a serious drawback. The link to the application’s executable is hard-wired into the macro, which makes it less flexible. If you wanted to run this code on Windows, you’d have to modify the path accordingly:

Shell ("C:Program FilesMozilla Firefoxfirefox.exe", 1, "http://www.wikipedia.org")

You can make your macro more flexible by checking the platform you’re running on using the GetGUIType function:

If GetGUIType=1 Then Shell ("C:Program FilesMozilla Firefoxfirefox.exe", 1, "http://www.wikipedia.org") Else Shell ("firefox", 1, "http://www.wikipedia.org")

But things get even more complicated if you want to share your macro with other users. First of all, Firefox is not always installed in the default location. And some people might not use Firefox at all, preferring Opera or Konqueror. In theory, one way out of this conundrum is to let users specify the browser they want to use in the macro with an input box that prompts the user to enter the path to the desired browser:

BrowserPath=InputBox("Enter browser path", "Default browser") Shell (BrowserPath, 1, "http://www.wikipedia.org")

However, prompting the user to enter the path every single time is not particularly practical. A better approach is to enable the macro to save the entered path in a preference file, and then retrieve the path from there when needed.

The first thing you have to do is to tweak the macro so it can save the preference file in a specified location without hard-wiring the path to it in the macro itself. Fortunately, OpenOffice.org Basic has a service that can solve this chicken-and-egg problem. You are probably familiar with the environment variables $USER and $HOME, which contain the current username and home directory. You can use these environment variables in shell scripts and work with their values. Well, OpenOffice.org has a few variables of its own. The $(user) variable, for example, returns the path to the OpenOffice.org user preferences directory (e.g. /home/user/.openoffice.org2/user). You can use the com.sun.star.util.PathSubstitution service to replace the $(user) variable with the current OpenOffice.org user directory:

SubstService = CreateUnoService("com.sun.star.util.PathSubstitution") UserPath = SubstService.substituteVariables("$(user)", true)

This gives you a directory where you can store the preference file. All you have to do now is to specify a file name. To keep things tidy, you might also want to set up a separate subdirectory for storing the preference file:

PrefFile = UserPath + "/prefs/defaultbrowser.ini"

Once you’ve specified the path to the preference file, you can prompt the user to enter the path to the browser using a standard input box:

BrowserPath=InputBox("Enter browser path", "Default browser")

The last step is, of course, to write the specified browser path to the defaultbrowser.ini preference file. We’ve covered writing to a file using OpenOffice.org Basic before, so the following code block should be easy to understand:

f1 = FreeFile() Open PrefFile for output as #f1 Print #f1, BrowserPath Close #f1

Here is what the entire macro looks like:

Sub WriteBrowserPath SubstService = CreateUnoService("com.sun.star.util.PathSubstitution") UserPath = SubstService.substituteVariables("$(user)", true) PrefFile = UserPath + "/prefs/defaultbrowser.ini" BrowserPath=InputBox("Enter browser path", "Default browser") f1 = FreeFile() Open PrefFile for output as #f1 Print #f1, BrowserPath Close #f1 End Sub

Reading it back

Now we need a macro that does the exact opposite: it opens the preference file for reading and retrieves the browser path from it. You can then use the retrieved browser path with the Shell() routine to launch the specified browser.

Similar to the first macro, start with obtaining the path to the preference file. You could write a function to do that, but to keep things simple, just reuse the code from the first macro.

If the preference file exists, the macro opens it and reads the first line, which contains the browser path. If the file doesn’t exist, the macro calls the WriteBrowserPath macro you created previously:

If FileExists(PrefFile) Then f1 = FreeFile() Open PrefFile for Input as #f1 Line Input #f1, BrowserPath Close #f1 Else WriteBrowserPath End If

Again, here is the macro in its entirety for your perusal:

Sub ReadBrowserPath SubstService = CreateUnoService("com.sun.star.util.PathSubstitution") UserPath = SubstService.substituteVariables("$(user)", true) PrefFile = UserPath + "/prefs/defaultbrowser.ini" If FileExists(PrefFile) Then f1 = FreeFile() Open PrefFile for Input as #f1 Line Input #f1, BrowserPath Close #f1 Else WriteBrowserPath End If End Sub

With these macros, you can save and recall user preferences in a text file, which can help you to make your macros more flexible. For another illustration of how the described techniques work in practice, take a look at the Last Session extension, which allows you to save current documents and open them with a single mouse click the next time you start OpenOffice.org.

Category:

  • Office Software