Identificatie van het besturingssysteem

Het identificeren van het besturingssysteem kan worden uitgevoerd met Python of Basictaal.

note

De eigenschap ComputerName is enkel beschikbaar onder Windows. Standaard aanroepen naar Python-macro's helpen de beperkingen van LibreOffice Basic te overwinnen.


Een Python-class gebruiken:


        import os, platform
        class Platform():
            @property
            def ComputerName(self): return platform.node()
            @property
            def DirSeparator(self): return os.sep
            @property
            def isLinux(self): return (self.OSName=='Linux')
            @property
            def isMacOSX(self): return (self.OSName=='Darwin')
            @property
            def isWindows(self): return (self.OSName=='Windows')
            @property
            def OSName(self): return platform.system()
            @property
            def PathDelimiter(self): return os.pathsep
    

Het gebruiken van een Basic klassemodule:

tip

MacOS X wordt standaard niet herkent onder LibreOffice Basic. Platformidentificatie is mogelijk met behulp van de LibreOffice Application Programming Interface (API).



        Option Compatible
        Option ClassModule
        Option Explicit
        
        Public Property Get ComputerName As String
            If isWindows Then ComputerName = Environ("ComputerName")
        End Property ' Platform.ComputerName
        
        Public Property Get DirSeparator As String
            DirSeparator = GetPathSeparator()
        End Property ' Platform.DirSeparator
        
        Public Property Get IsLinux As Boolean
            isLinux = ( GetGUIType()=4 ) ' Applies to MacOS X as well 
        End Property ' Platform.isLinux
        
        Public Property Get IsMacOSX As Boolean
            isMacOSX = ( OSName="MAC" )
        End Property ' Platform.isMacOSX
        
        Public Property Get IsWindows As Boolean
            isWindows = ( GetGUIType()=1 )
        End Property ' Platform.isWindows
        
        Public Property Get OSName As String
            ' Geef platformnaam terug as "MAC" , "UNIX", "WIN"
            ' Afgeleid van de "Tools.UCB.ShowHelperDialog" functie
            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")
        End Property ' Platform.OSName
        
        Public Property Get PathDelimiter As String
            Select Case OSName
                Case "MAC", "UNIX" : PathDelimiter = ":"
                Case "WIN" : PathDelimiter = ";"
             End Select
        End Property ' Platform.PathDelimiter
    

Voorbeelden:

Met Python

>>> from <the_module> import Platform

>>> print(Platform().isMacOSX) # objecteigenschap

True

>>> input(Platform().OSName) # objecteigenschap

Darwin

Vanuit het menu Extra – Macro's - Macro uitvoeren....


        from <the_module> import Platform
        import screen_io as ui
        p = Platform()
        ui.MsgBox(''.join(['isMacOS: ',str(p.isMacOSX)]),0,p.OSName)
    

Met LibreOffice Basic


        Sub Platform_example()
            Dim p As New Platform ' Instantie van Platformklasse
            MsgBox p.isLinux ' objecteigenschap
            Print p.isWindows, p.OSName ' objecteigenschappen
        End Sub ' Platform_example
    

Invoer/Uitvoer naar Scherm

Functie GetGuiType

Functie GetPathSeparator

Python programmeringsvoorbeelden

LibreOffice Python-scripts Help