判断操作系统

可以通过 Python 或 Basic 语言判断操作系统。

note

ComputerName 属性只在 Windows 平台可用。Basic 通过调用 Python 宏来绕过 LibreOffice Basic 的限制。


使用 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
    

使用 Basic 类模块:

tip

LibreOffice Basic 缺少 MacOS X 原生识别的能力。可通过 LibreOffice 应用程序编程接口 (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 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
            ' 返回平台名称,即「MAC」「UNIX」「WIN」
            ' 隐式调用「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
    

示例:

使用 Python

>>> from <the_module> import Platform

>>> print(Platform().isMacOSX) # 对象属性

True

>>> input(Platform().OSName) # 对象属性

Darwin

从「工具 – 宏 - 运行宏...」菜单。


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

使用 LibreOffice Basic


        Sub Platform_example()
            Dim p As New Platform ' 类 Platform 的实例
            MsgBox p.isLinux ' 对象属性
            Print p.isWindows, p.OSName ' 对象属性
        End Sub ' Platform_example
    

请支持我们!