Getting Session Information

Computing LibreOffice user profile and shared modules system file paths can be performed with Python or with Basic languages. BeanShell, Java, JavaScript and Python scripts locations can be derived from this information.


В оболонці Python.

>>> from <the_module> import Session

>>> print(Session.SharedPythonScripts()) # static method

>>> print(Session().UserName) # властивість об’єкта

>>> input(Session().UserProfile) # властивість об’єкта

З меню Засоби – Макроси - Запустити макрос....

        from <the_module> import Session
        def demo_session():
            import screen_io as ui
            ui.MsgBox(Session.Share(),title='Installation Share')  # static method
            ui.Print(Session.SharedPythonScripts())  # static method
            s = Session()  # створення примірника
            ui.MsgBox(s.UserName,title='Hello')  # властивість об’єкта
            ui.Print(s.UserPythonScripts)  # властивість об’єкта
        g_exportedScripts = (demo_session,)  # публічний макрос

У LibreOffice Basic.

        Sub Session_example()
            Dim s As New Session ' instance of Session class
            Print "Розташування спільних сценаріїв:", s.SharedScripts
            MsgBox s.UserName,,"Привіт"
            Print s.UserScripts, Chr(13), s.UserPythonScripts
        End Sub ' Session_example

Використання COM/OLE і мови сценаріїв Visual Basic.

        ' The service manager is always the entry point
        ' If there is no office running then an office is started up
        Set sm = WScript.CreateObject("")
        ' PathSubstitution service exhibits information to infer
        ' <UserProfile|Share>/Scripts/python locations from
        Set obj = sm.createInstance("")
        MsgBox CreateObject("WScript.Network").UserName,, "Hello"
        user = obj.getSubstituteVariableValue("$(user)")
        MsgBox user & "/Scripts",, "User scripts location"
        libO = Replace(obj.getSubstituteVariableValue("$(inst)"), "program/..", "Share")
        MsgBox libO & "/Scripts",, "Shared scripts location"

Python Session class:

        import getpass, os, os.path, uno
        class Session():
            def substitute(var_name):
                ctx = uno.getComponentContext()
                ps = ctx.getServiceManager().createInstanceWithContext(
                    '', ctx)
                return ps.getSubstituteVariableValue(var_name)
            def Share():
                inst = uno.fileUrlToSystemPath(Session.substitute("$(prog)"))
                return os.path.normpath(inst.replace('program', "Share"))
            def SharedScripts():
                return ''.join([Session.Share(), os.sep, "Scripts"])
            def SharedPythonScripts():
                return ''.join([Session.SharedScripts(), os.sep, 'python'])
            @property  # alternative to '$(username)' variable
            def UserName(self): return getpass.getuser()
            def UserProfile(self):
                return uno.fileUrlToSystemPath(Session.substitute("$(user)"))
            def UserScripts(self):
                return ''.join([self.UserProfile, os.sep, 'Scripts'])
            def UserPythonScripts(self):
                return ''.join([self.UserScripts, os.sep, "python"])

Unlike Basic, pathname normalization is performed with Python inside Session class.

LibreOffice Basic Session class:

        Option Explicit
        Option Compatible
        Option ClassModule
        Private _ps As Object ' Private member
        Private Sub Class_Initialize()
            Set _ps = CreateUnoService("")
        End Sub ' Constructor
        Private Sub Class_Terminate()
            _ps = Nothing
        End Sub ' Destructor
        Public Property Get SharedScripts() As String
            Dim inst As String, shr As String
            inst = ConvertFromURL(_ps.getSubstituteVariableValue("$(prog)"))
            shr = Tools.Strings.ReplaceString(inst,"Share","program")
            SharedScripts = shr & GetPathSeparator() &"Scripts"
        End Property ' Session.sharedScripts
        Public Property Get SharedPythonScripts() As String
            sharedPythonScripts = sharedScripts() & GetPathSeparator() &"python"
        End Property ' Session.sharedPythonScripts
        Public Property Get UserName() As String ' User account name
            userName = _ps.getSubstituteVariableValue("$(username)")
        End Property ' Session.userName
        Public Property Get UserProfile() As String ' User profile system path
            userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
        End Property ' Session.userProfile
        Public Property Get UserScripts() As String ' User scripts system path
            userScripts = userProfile() & GetPathSeparator() &"Scripts"
        End Property ' Session.userScripts
        Public Property Get UserPythonScripts() As String ' User Python scripts system path
            userPythonScripts = userScripts() & GetPathSeparator() &"python"
        End Property ' Session.userPythonScripts

Будь ласка, підтримайте нас!