Best macro to detect the operating system

Using LO Version: 4.4.3.2 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.

…five years old… Do you have any problems? It’s realy work?

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.

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

sys.platform

platform for fine tuning

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 @wertie's idea here: https://ask.libreoffice.org/t/best-macro-to-detect-the-operating-system/13300
REM This only works if the user has a file called "get_os.py" 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 "get_os.py" 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( "vnd.sun.star.script:get_os.py$get_OS?language=Python&location=user" )
	get_sys_platform = oScript.invoke( aParams(), aOutParamIndex(), aOutParam() )
End Function

@librebel Thanks!

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

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.

Thanks @EasyTrieve :slight_smile:

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, Thanks again. This still doesn’t make any sense to me, but don’t worry, I’ll figure it out at some point.

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

… 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.

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.

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("com.sun.star.util.PathSettings")
	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)
	Next
	msgbox sMsgBox
End Function

Win users please adjust slash.

@EasyTrieve See ‘Python script’ section here .

@Ratslinger, thanks.

This is evidently a promising approach for us basic users. However I am getting a script framework error exception at the line beginning ‘oScript =’ with the message “unsatisfied query for interface of type com.sun.star.script.provider.XScriptProvider” (Ubuntu 16.04, LO 5.1.6.2)

@Pansmanser Where did you save the Python script to?

Thanks @Ratslinger for help in this. I saved get_os.py to
/home/robert.config/libreoffice/4/user/Scripts/python. I had to create Scripts/python.
(Is the script it meant to end with that comma?)
Also ran @EasyTriee’s GetPythonPaths macro to locate the paths. The other path displays as file:///usr/lib/libreoffice/program/…/share/Scripts/python, but it didn’t seem to match the file structure. Not sure what’s missing at …, but nothing looks right below program.

@Pansmanser, error relates to this section: get_os.py$get_OS?language=Python&location=user" Check upper lower case. Note his filename is lower case and his function is mixed case. I stumbled over this at first, then regularized the names to get_OS.py, and get_OS. Perhaps that is the issue. Also, do you mean /home/robert/.config/libreoffice/4/user/Scripts/python ? (had missing slash in your comment). If dirs are missing, then create them as needed.