Best macro to detect the operating system

Problem now (partially) solved by @Ratslinger. I did not have the python script provider installed. Does it work in OSX?

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 ' com.sun.star.configuration.ConfigurationAccess '
    keyNode = Tools.Misc.GetRegistryKeyContent("org.openoffice.Office.Common/Help")
    OSName = keyNode.GetByName("System")
    ' inferred from "Tools.UCB.ShowHelperDialog" '
End Function ' (Tools).OSName

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: 
    OStext="WINDOWS"
    OScode=1
   FileSystemDelimiter="\"
  Case 3: 
    OStext="MAC"
    OScode=3
    FileSystemDelimiter="/"
  Case 4: 
    If Instr(Environ("PATH"),"openoffice")=0 And Instr(Environ("PATH"),Lcase(fsGetSetting("ooname")))=0 Then
     OStext="OSX"
     OScode=4
     FileSystemDelimiter="/"
    Else
     OStext="UNIX"
     OScode=5
     FileSystemDelimiter="/"
    Endif
  End Select
End sub

Function fsGetSetting(sA)
   GlobalScope.BasicLibraries.LoadLibrary("Tools")
   Dim oProdNameAccess As Object
   oProdNameAccess=GetRegistryKeyContent("org.openoffice.Setup/Product")
    Select Case Lcase(sA)
    Case "language"
      fsGetSetting=GetStarOfficeLocale().language
   Case "country"
      fsGetSetting=GetStarOfficeLocale().country
   Case "ooname"
      fsGetSetting=oProdNameAccess.getByName("ooName")
   Case "ooversion"
      fsGetSetting=oProdNameAccess.getByName("ooSetupVersion")
   Case Else
      fsGetSetting="???"
   End Select
End Function

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

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.

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.

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

use set | less to list.

Ubuntu 16.04:

MACHTYPE=x86_64-pc-linux-gnu

OSTYPE=linux-gnu

Anybody help with the OSX results?

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

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.

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 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 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?

Let’s stick with Python script. Script name should be get_os.py. 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

Basic sub code:

Sub PrintMyOS
    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() )
    Print get_sys_platform
End Sub

Run the sub & you should get result.

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.

OK. Found your problem. You are using a LO distro version. It doesn’t come with everything you normally get when you use the version from LibreOffice. You are missing the script provider. Close any open LO programs. From terminal run:

sudo apt-get install libreoffice-script-provider-python

Once installed, run the basic sub again.

Yee-hah!! and Halellujah! Solved! I was starting to suspect that my LO was incomplete. Thanks @Ratslinger.

OK this works, and I hate to be ungrateful. BUT my jubilation may be premature. It requires the python script to be installed on every machine on which you need to use it, which makes it not very portable. And I don’t know if it would work on OSX, which was why I started down this track. @Ratslinger, is there any chance of making the OSTYPE method work?
Or can python scripts be embedded, as with basic?