Ask Your Question

best macro to detect the operating system

asked 2015-06-02 15:02:28 +0200

jay Arr gravatar image

updated 2017-05-16 08:36:07 +0200

EasyTrieve gravatar image

Using LO Version: on MAC OSX 10.10.3

I'm using the last macro code in this openoffice forum thread to detect the native operating system.

It still appears to work for OSX & Windows.

My question is (because it makes reference to OPEN office) is this the best/most reliable way of doing this for LO?

FYI: the code is over five years old.

edit retag flag offensive close merge delete


...five years old... Do you have any problems? It's realy work?

JohnSUN gravatar imageJohnSUN ( 2015-06-02 16:49:38 +0200 )edit

I updated the tags to help others find this in the future, because this question has gotten lots of attention, and because it applies to all of LO's components.

EasyTrieve gravatar imageEasyTrieve ( 2017-05-16 08:38:09 +0200 )edit

6 Answers

Sort by » oldest newest most voted

answered 2015-06-03 10:42:38 +0200

karolus gravatar image

Its really easy if you would use python instead poor old basic.


platform for fine tuning

edit flag offensive delete link more

answered 2017-05-13 04:52:43 +0200

librebel gravatar image

The answer by @karolus above also provides a solution for LO users who want to write their macro in LibreOffice BASIC. They could create a .py file which contains a function that returns sys.platform, and invoke that Python function using BASIC. The following LibreOffice BASIC function returns "linux" for me.

Function get_sys_platform()
REM Returns the output of sys.platform.
REM Trying to find a workaround for the limited GetGUItype() functionality in LibreOffice BASIC.
REM Based on @karolus's idea here:
REM This only works if the user has a file called "" inside the LibreOffice Scripts Folder for Python.
REM  ( On Ubuntu this folder is located at: "/home/username/.config/libreoffice/4/user/Scripts/python" )
REM Inside the file "" there should be these 5 lines ( without the apostrophes ' at the start ):

'#!/usr/bin/env python3
'def get_OS( ):
'   import sys
'   return sys.platform
'g_exportedScripts = get_OS,

REM Now the user can call the get_OS() script in BASIC as follows:
    Dim oScriptProvider, oScript
    Dim aParams(), aOutParamIndex(), aOutParam()
    oScriptProvider = ThisComponent.getScriptProvider()
    oScript = oScriptProvider.getScript( "$get_OS?language=Python&location=user" )
    get_sys_platform = oScript.invoke( aParams(), aOutParamIndex(), aOutParam() )
End Function
edit flag offensive delete link more


@librebel Thanks!

Ratslinger gravatar imageRatslinger ( 2017-05-13 05:18:15 +0200 )edit

Thanks in kind, they call me a Necromancer now :))

librebel gravatar imagelibrebel ( 2017-05-13 05:24:24 +0200 )edit

Nice job! I had to change user to share to get this to work. And just out of curiosity, as it seems the arrays do nothing, what are they used for? They don't return anything.

EasyTrieve gravatar imageEasyTrieve ( 2017-05-14 08:13:30 +0200 )edit

Thanks @EasyTrieve :)

aParams:    all parameters
aOutParamIndex: the indices of all parameters that are specified as out or inout.
aOutParam:  the values of all parameters that are specified as out or inout.

Example: aOutParamIndex == { 1, 4 } means that aOutParam[0] contains the out value of the aParams[4] parameter. (see XInvocation)

librebel gravatar imagelibrebel ( 2017-05-14 08:28:21 +0200 )edit

@librebel, Thanks again. This still doesn't make any sense to me, but don't worry, I'll figure it out at some point.

EasyTrieve gravatar imageEasyTrieve ( 2017-05-14 08:46:30 +0200 )edit

!!! ?????? BTW, has anyone actually confirmed what this returns from OSX?

EasyTrieve gravatar imageEasyTrieve ( 2017-05-14 08:46:49 +0200 )edit

... The reason I ask this is because after playing around with this a little, and especially when getting .nodename, .release, .version, and .hardware from os.uname(), I noticed that for me os.uname().hardware is not working, BUT ... if I return os.uname() as an array, I then get the hardware info. And also I get Linux (capitalized, instead of lower case). This is strange. Seems flaky. Just say'n.

EasyTrieve gravatar imageEasyTrieve ( 2017-05-14 08:59:04 +0200 )edit

In this particular case the arrays do nothing, since get_OS() has no parameters.. If the value of a passed argument is changed by a function, the updated value can be retrieved afterwards from the aOutParam() array.

librebel gravatar imagelibrebel ( 2017-05-14 11:01:03 +0200 )edit

Figured out the user / share thing. There are two places to put python code. Run this to list them:

Function getPythonPaths()
    Dim oPathSettings As Object : oPathSettings = createUnoService("")
    Dim s       As String
    Dim sMsgBox As String
    For Each s In Split(oPathSettings.Basic,";")
        sMsgBox = sMsgBox & left(s,len(s)-len("/basic")) & "/Scripts/python" & chr(10)
    msgbox sMsgBox
End Function

Win users please adjust slash.

EasyTrieve gravatar imageEasyTrieve ( 2017-05-14 21:55:32 +0200 )edit

@EasyTrieve See 'Python script' section here .

Ratslinger gravatar imageRatslinger ( 2017-05-14 22:19:57 +0200 )edit

answered 2019-05-21 11:07:34 +0200

updated 2019-05-21 11:32:46 +0200

Next to the above mentioned Python hints, my take for a solution using Basic language is:

Option Explicit
Sub Main : MsgBox OSName : End Sub

Function OSName As String
    ''' Return platform name as "MAC", "UNIX", "WIN" '''
    With GlobalScope.Basiclibraries
        If Not .IsLibraryLoaded("Tools") Then .LoadLibrary("Tools")
    End With
    Dim keyNode As Object ' '
    keyNode = Tools.Misc.GetRegistryKeyContent("org.openoffice.Office.Common/Help")
    OSName = keyNode.GetByName("System")
    ' inferred from "Tools.UCB.ShowHelperDialog" '
End Function ' (Tools).OSName
edit flag offensive delete link more

answered 2017-05-15 00:03:44 +0200

EasyTrieve gravatar image

updated 2017-05-16 05:46:24 +0200

Here's a little Form tool based on @librebel 's code here to make it a slight bit easier to install and test this feature, (... cause I want to find someone to test this on OSX and other OS's to make sure this works.)

Run the 'Form to get OS using Python call' to help you setup and test this feature.

image description

(There's a few hours I'll never get back, but it was fun.)

* Added a comma after g_exportedScripts = line, as per @librebel 's comment. No harm I guess, as it might break for someone w/o it.
* Also I should note that I took liberty to regularized the name so get_OS is both the filename and function name. (Before the filename was, lower case. Hope I'm not breaking any rule here.)

edit flag offensive delete link more


Nice job @EasyTrieve... one note about the "g_exportedScripts", it expects a string list which defines the python functions within your .py-file that are made executable from within LibreOffice. If there is no more than 1 function name in the string list, it should for some reason end with a comma, e.g.:

g_exportedScripts = macro1,

for 2 or more exported functions:

g_exportedScripts = macro1, macro2
librebel gravatar imagelibrebel ( 2017-05-15 00:53:37 +0200 )edit

@librebel, Thanks. Humm? It works for me without the comma. Figured w/ python's zest for brevity, it was worth a try. Can't find official documentation on g_exportedScripts. But from DuckDuckGo g_exportedScripts I can see that others use it w/ a comma. Only thing I can figure is it might need a comma in older or newer versions. my: python --version gives "2.7.9". What's your version?

EasyTrieve gravatar imageEasyTrieve ( 2017-05-16 05:23:36 +0200 )edit

The comma at the end of one item makes it a tuple. See documentation.

Ratslinger gravatar imageRatslinger ( 2017-05-16 06:25:16 +0200 )edit

@Ratslinger now it make sense .. however when i put a single method name inside tuple brackets () it still requires the extra comma. Perhaps it must be a 2-tuple at the least, where the second element may be empty... @EasyTrieve my Python version is 3.5.2 .. but i also have 2.7 installed from before :)

librebel gravatar imagelibrebel ( 2017-05-16 09:53:36 +0200 )edit

@Ratslinger, thanks, that's interesting. @librebel, the 'documentation' Ratslinger mentions says this: "Note that it is actually the comma which makes a tuple, not the parentheses." ...And looks like I've got some more updating to do.

EasyTrieve gravatar imageEasyTrieve ( 2017-05-16 16:04:57 +0200 )edit

answered 2017-05-11 17:02:18 +0200

Pansmanser gravatar image

updated 2017-05-12 18:25:39 +0200

No, it does not work on LibreOffice. I don't actually see how the PATH helps to distinguish Linux from OSX, which is based on Linux. If that Python trick works, then it is frustrating that the Basic GetGUI call is incomplete. The same source from which jay Arr took his code suggests inspecting environment variables OSTYPE and MACHTYPE, but they don't appear to exist in Ubuntu. In trying to solve this problem, I've been looking for other env var's common to OSX, Win & Linux, but haven't got anything useful yet.

edit flag offensive delete link more


OSTYPE and MACHTYPE on Mint 18 show as MACHTYPE=x86_64-pc-linux-gnu, OSTYPE=linux-gnu.

use set | less to list.

Ratslinger gravatar imageRatslinger ( 2017-05-12 18:59:20 +0200 )edit

Ubuntu 16.04:



Mike Kaganski gravatar imageMike Kaganski ( 2017-05-12 19:22:16 +0200 )edit

Anybody help with the OSX results?

Pansmanser gravatar imagePansmanser ( 2017-05-13 18:49:05 +0200 )edit

@Pansmanser Did you try the basic/Python solution presented by @librebel ( original suggestion by @karolus )? On my Mint system it returns Linux. Don't have others to test, but have no reason to believe it won't work.

Ratslinger gravatar imageRatslinger ( 2017-05-13 19:37:15 +0200 )edit

Thanks, @Ratslinger - I'm trying the @librebel/@karolus solution, as shown above. Meanwhile, environ("MACHTYPE") and environ("OSTYPE") return null strings on my sys and are not present when I list environment variables.

Pansmanser gravatar imagePansmanser ( 2017-05-15 12:29:39 +0200 )edit

@Pansmanser Running set | less using terminal will only display the first page initially. At the : prompt did you press enter or down arrow for more entries? It can be a long list which is the reason to display 'less'.

Ratslinger gravatar imageRatslinger ( 2017-05-15 13:42:00 +0200 )edit

@Ratslinger Thanks for that. Yes, set | less (how do you do that highlighting?) throws up MACHTYPE and OSTYPE. However they are still not detected by environ(). Any fix for that?

Pansmanser gravatar imagePansmanser ( 2017-05-15 15:44:15 +0200 )edit

Let's stick with Python script. Script name should be Contents should be (with comma at end):

#!/usr/bin/env python3
def get_OS( ):
   import sys
   return sys.platform
g_exportedScripts = get_OS,

placed in this directory: /home/robert/.config/libreoffice/4/user/Scripts/python

Ratslinger gravatar imageRatslinger ( 2017-05-15 16:45:36 +0200 )edit

Basic sub code:

Sub PrintMyOS
    Dim oScriptProvider, oScript
    Dim aParams(), aOutParamIndex(), aOutParam()
    oScriptProvider = ThisComponent.getScriptProvider()
    oScript = oScriptProvider.getScript( "$get_OS?language=Python&location=user" )
    get_sys_platform = oScript.invoke( aParams(), aOutParamIndex(), aOutParam() )
    Print get_sys_platform
End Sub

Run the sub & you should get result.

Ratslinger gravatar imageRatslinger ( 2017-05-15 16:46:51 +0200 )edit

No, unfortunately not the result I want. Both routines present and correct (paste & copied from your code, as previously from librebel). Throwing up same exception error at same line.

Pansmanser gravatar imagePansmanser ( 2017-05-15 17:23:58 +0200 )edit

answered 2015-06-03 07:07:48 +0200

jay Arr gravatar image

updated 2015-06-03 07:15:46 +0200

Yes. I've tested this on OSX 10.10.3 and Windows 7

Here's the macros I'm using - notice the references to OO

global OStext   as string
global OScode   as integer
global   FileSystemDelimite as string

sub TestOS
call getOS
print OSText
end sub

public Sub getOS()    
  Select Case getGUIType
  Case 1: 
  Case 3: 
  Case 4: 
    If Instr(Environ("PATH"),"openoffice")=0 And Instr(Environ("PATH"),Lcase(fsGetSetting("ooname")))=0 Then
  End Select
End sub

Function fsGetSetting(sA)
   Dim oProdNameAccess As Object
    Select Case Lcase(sA)
    Case "language"
   Case "country"
   Case "ooname"
   Case "ooversion"
   Case Else
   End Select
End Function
edit flag offensive delete link more


Unfortunately, this incorrectly says my Linux Debian 8.5 / LO is OSX.

EasyTrieve gravatar imageEasyTrieve ( 2017-05-11 21:38:09 +0200 )edit

Because 'openoffice' is not in the path (nor LO) and 'ooname' is not detected. Again, I don't think PATH can be used to do this.

Pansmanser gravatar imagePansmanser ( 2017-05-13 18:51:58 +0200 )edit
Login/Signup to Answer

Question Tools



Asked: 2015-06-02 15:02:28 +0200

Seen: 1,015 times

Last updated: May 21