Text Documents (Writer)
HTML Documents (Writer Web)
Spreadsheets (Calc)
Presentations (Impress)
Drawings (Draw)
Database Functionality (Base)
Formulae (Math)
Charts and Diagrams
Macros and Scripting
Office Installation
Common Help Topics
OneOffice Logo

Getting Session Information

Computing Office 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.

Examples:

With Python shell.

>>> from import Session

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

>>> print(Session().UserName) # object property

>>> input(Session().UserProfile) # object property

From Tools – Macros - Run Macro... menu.


        from  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()  # instance creation
            ui.MsgBox(s.UserName,title='Hello')  # object property
            ui.Print(s.UserPythonScripts)  # object property
            
        g-exportedScripts = (demo-session,)  # public macros
    

With Office Basic.


        Sub Session-example()
            Dim s As New Session ' instance of Session class
            Print "Shared scripts location:", s.SharedScripts
            MsgBox s.UserName,,"Hello"
            Print s.UserScripts, Chr(13), s.UserPythonScripts
        End Sub ' Session-example
    

Using COM/OLE and Visual Basic Scripting language.


        ' The service manager is always the entry point
        ' If there is no office running then an office is started up
        Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
        ' PathSubstitution service exhibits information to infer
        ' <userprofile|share>/Scripts/python locations from
        Set obj = sm.createInstance("com.sun.star.util.PathSubstitution")
            
        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"
    </userprofile|share>

Python Session class:


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

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

Office Basic Session class:


        Option Explicit
        Option Compatible
        Option ClassModule
            
        Private -ps As Object ' Private member
            
        Private Sub Class-Initialize()
            GlobalScope.BasicLibraries.LoadLibrary("Tools")
            Set -ps = CreateUnoService("com.sun.star.util.PathSubstitution")
        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