Identificar o sistema operacional

A identificação do sistema operacional pode ser feita em linguagem Python ou Basic.

note

ComputerName é uma propriedade disponível somente no Windows. As chamadas do Basic para as macros Python ajudam a resolver as limitações do LibreOffice Basic.


Usando uma classe de Python:


        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
    

Usando um módulo de classe de Basic:

tip

Falta ao LibreOffice Basic o reconhecimento nativo do MacOS. A identificação de plataforma é possível utilizando a Application Programming Interface (API) do 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 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
            ' Retorna o nome da plataforma como "MAC", "UNIX", "WIN"
            ' Inferido da função "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
    

Exemplos:

Com Python

>>> from <the_module> import Platform

>>> print(Platform().isMacOSX # propriedade do objeto

True

>>> input(Platform().OSName) # propriedade do objeto

Darwin

A partir do menu Ferramentas – Macros - Executar Macro....


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

Com LibreOffice Basic


        Sub Platform_example()
            Dim p As New Platform ' instância da classe Platform
            MsgBox p.isLinux ' propriedade do objeto
            Print p.isWindows, p.OSName ' propriedades do objeto
        End Sub ' Platform_example
    

Entrada e Saída na tela

Função GetGuiType

Função GetPathSeparator

Exemplos de programação em Python

Ajuda dos scripts Python do LibreOffice

♥ Doe para nosso projeto! ♥