identifier le système d'exploitation

L'identification du système d'exploitation peut être effectuée avec le langage Python ou Basic.

note

La propriété ComputerName est uniquement disponible pour Windows. Les appels Basic aux macros Python permettent de surmonter les limitations de Basic LibreOffice.


Utiliser une classe Python :


        """ the_module """
        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
    

Utilisation d'un module de classe Basic :

tip

Le Basic LibreOffice ne reconnaît pas nativement MacOS X. L'identification de la plateforme est possible à l'aide de l'interface de programmation (API) LibreOffice.



        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 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
            ' Nom des plateformes retournées "MAC", "UNIX", "WIN"
            'Déduit de la fonction "Tools.UCB.ShowHelperDialog"
            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
    

Exemples :

En Python

>>> from < the_module > import Platform

>>> print(Platform().isMacOSX) # propriété del'objet

True

>>> input(Platform().OSName) # propriété de l'objet

Darwin

Depuis le menu Outilss – Macros - Exécuter la macro....


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

En Basic LibreOffice


        Sub Platform_example()
            Dim p As New Platform ' instance de la classe Platform
            MsgBox p.isLinux ' propriété de l'objet
            Print p.isWindows, p.OSName ' propriété de l'objet
        End Sub ' Platform_example
    

Aidez-nous !