[Solved] Detect Operating System

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Heertsch
Posts: 28
Joined: Sat Jan 02, 2010 1:04 am

[Solved] Detect Operating System

Post by Heertsch »

Is there a way to detect, on which Operating System OOo Basic runs?
Last edited by Villeroy on Fri Jan 08, 2010 10:46 am, edited 1 time in total.
openOffice 3.2 on Windows 7 / Mac OSC 10.5 / Ubuntu 10.4
RPG
Volunteer
Posts: 2261
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Detect Operating System

Post by RPG »

Hello


Code: Select all

GetGuiType Function [Runtime]
Returns a numerical value that specifies the graphical user interface.
This runtime function is only provided for downward compatibility to previous versions. The return value is not defined in client-server environments.
Syntax:
GetGUIType()
Return value:
Integer
Return values:
1: Windows
3: Mac OS
4: UNIX
From the macro help file

Romke
LibreOffice 24.8.5.2 on openSUSE Leap 15.6
Heertsch
Posts: 28
Joined: Sat Jan 02, 2010 1:04 am

Re: Detect Operating System

Post by Heertsch »

Thanks, works fine!
Andreas
openOffice 3.2 on Windows 7 / Mac OSC 10.5 / Ubuntu 10.4
Heertsch
Posts: 28
Joined: Sat Jan 02, 2010 1:04 am

Detect Operating System

Post by Heertsch »

the GetGUIType() function sends 4 (Unix) on MAC OSX
Is there a way to distinguish Mac OSX and other Unix?
openOffice 3.2 on Windows 7 / Mac OSC 10.5 / Ubuntu 10.4
Heertsch
Posts: 28
Joined: Sat Jan 02, 2010 1:04 am

Detect Operating System

Post by Heertsch »

Ok,here my (quick and dirty) solution:

Code: Select all

'==========================================
'determine Operating System
'==========================================
function OS()as string  	
  select case getGUIType
  case 1: OS="WINDOWS"
  case 3: OS="MAC"
  case 4: OS=iif(instr(environ("PATH"),"openoffice")=0,"OSX","UNIX")
  end select
end function
I hope someone has a better approach. (to interpret the PATH enviroment variable may be dangerous... :knock: )
Andreas
openOffice 3.2 on Windows 7 / Mac OSC 10.5 / Ubuntu 10.4
User avatar
MrProgrammer
Moderator
Posts: 5281
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: [Solved] Detect Operating System

Post by MrProgrammer »

Of course, under the covers a modern Mac system is UNIX (with a really nice front end), so 4 (UNIX) might be a reasonable answer on that platform -- it depends on what you're going to do with this information. But one idea might be to look at the OSTYPE or MACHTYPE environment variables, which on my machine are:
OSTYPE=darwin10.0
MACHTYPE=x86_64-apple-darwin10.0
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.7.6, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
james_h
Posts: 17
Joined: Wed Jan 09, 2008 6:24 am

Re: [Solved] Detect Operating System

Post by james_h »

The function fails on a fork of OpenOffice.org because the function looks for "openoffice", not "libreoffice" or other program directory name.

I adapted the function as follows. It should work for forks:

Code: Select all

Function SpeechEngine()as string     
  select case getGUIType
  case 1: SpeechEngine=iif(fileExists (fsProgramDirectory & "eSpeak\command_line\espeak.exe"),"ESPEAK","SAPI") 'Win
  case 3: SpeechEngine=iif((fileExists("/usr/bin/espeak") or ifileExists("/usr/local/bin/espeak")),"ESPEAK","SAY") 'Old Mac
  case else: SpeechEngine=iif((fileExists("/usr/bin/espeak") or fileExists("/usr/local/bin/espeak")),"ESPEAK","SAY") 'Posix
  end select
end Function

Function fsProgramDirectory
Dim c as string
c = environ("ProgramFiles")
if instr(c, "\") <> 0 then
	fsProgramDirectory= c & "\"
else
	fsProgramDirectory="/usr/bin/"
end if
End Function
RPG
Volunteer
Posts: 2261
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [Solved] Detect Operating System

Post by RPG »

Hello

I have test the code given by Heertsch and it works also with LibreOffice. Maybe the problem you get is by the IIF.

http://www.openoffice.org/issues/show_bug.cgi?id=63614

There is a known bug with this code. When you store and close the file and then load again then it is working. I'm not sure if I have to close OOo complete. For this reason I donot use IIF.

http://www.openoffice.org/issues/show_bug.cgi?id=63614

You can use maybe: switch

Romke
LibreOffice 24.8.5.2 on openSUSE Leap 15.6
User avatar
james_h
Posts: 17
Joined: Wed Jan 09, 2008 6:24 am

Re: [Solved] Detect Operating System

Post by james_h »

This script should avoid a false identification as OSX for UNIX or LINUX systems using an alternate directory name like libreoffice instead of openoffice.

Code: Select all

Function getOS()as String     
  Select Case getGUIType
  Case 1: 
    getOS="WINDOWS"
  Case 3: 
    getOS="MAC"
  Case 4: 
    If Instr(Environ("PATH"),"openoffice")=0 And Instr(Environ("PATH"),Lcase(fsGetSetting("ooname")))=0 Then
	  getOS="OSX"
    Else
	  getOS="UNIX"
    Endif
  End Select
End Function

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
Post Reply