Best macro to detect the operating system

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?

@Pansmanser Not really ready to figure out another method. I am fairly certain the script is the way to go. Yes it can be embedded in the .odb (which is the ‘document’ setting). Please ask as a NEW question - How to embed Python script in document. This will make it available to others searching for it. Also the answer requires more room than allowed here.

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

Edits:

  • 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 get_os.py, lower case. Hope I’m not breaking any rule here.)

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

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

@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 :slight_smile:

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