|
|
|
OpenOffice Macros: Identify the Operating SystemTelling the Difference Between Windows and Linux in a Macro
Operating Systems such as Windows and Linux do operate in different ways. This article shows how a macro can tell the difference.
OpenOffice.org is a cross-platform application - which means that a developer may add a macro to a Calc document in Linux and then, when that same document is opened by a Calc user on a Windows PC, the macro will still run. However, there are differences between the two operating systems that can cause problems for the unaware programmer. This article sheds a little light on one of those differences - the file structure - and shows how a programmer can make a macro aware of its operating system. Comparing the Windows and Linux File StructuresIt's those tiny differences between operating systems that can cause the programmer problems - the file structure being one; and this difference can be understood better by looking at an example - the user's Document folder:
So to sum up the differences:
So, a bit of a show stopper then. Well, not really - it's those very differences that enable a programmer to identify which operating system the macro is running on. Identifying the Operating System from the File StructureThe programmer knows that:
and so this information can be used in conjunction with the Dir method (which is used to obtain the contents of a directory): rootDir = Dir ("C:")
This will return an empty string in Linux but it will contain information in Windows and can, therefore, be used to see which operating system is being used: rootDir = Dir ("C:")
if ( rootDir = "" ) then
msgbox "This is a Linux Machine"
else
msgbox "This is a Windows machine"
end if
Identifying the Operating System from the EnvironmentAs well as examining the file structure itself, the programmer can obtain environmental variables from the system by using the environ method, and from this the operating system can be deduced; for example a Linux system will return values for the following environ calls, but Windows will not:
and, as before, these can used to determine the operating system: shell = environ("SHELL")
if ( shell = "" ) then
msgbox "This is a Windows Machine"
else
msgbox "This is a Linux machine"
end if
An Example of an Operating System Aware MacroA macro that is aware of its operating system can now be written: Function correct_url (filename as String) as String
Dim Url as String
Dim rootDir
rootDir = Dir ("C:")
if ( rootDir = "" ) then
' This is a Linux Machine
Url = "/home/" & environ ("USER") & "/" & filename
else
' This is a Windows machine
Url = "C:\" & filename
end if
correct_url = convertToUrl (Url)
End Function
Sub Main
msgbox (correct_url ("Documents/Shares.ods")
End Sub
The result of running this macro will depend on the operating system being used:
ConclusionUnderstanding the differences between operating systems is important in any cross-platform application; however, using the simple techniques in this article mean that there need be no problems when using an OpenOffice.org macro on Windows or on Linux. ReferencesMark Alexander Bain, Learn OpenOffice.org Spreadsheet Macro Programming: OOoBasic and Calc automation (Packt Publishing, 2006)
The copyright of the article OpenOffice Macros: Identify the Operating System in Computer Programming is owned by Mark Alexander Bain. Permission to republish OpenOffice Macros: Identify the Operating System in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|