Sessie-informatie ophalen

De berekening van het LibreOffice gebruikersprofiel en het pad voor de gedeelde module systeembestanden kan worden gedaan met Python of met BASIC. De locatie van scrips van BeanShell, Java, JavaScript en Python kan worden bepaald uit deze informatie.

Voorbeelden:

Met Python-shell.

>>> from <the_module> import Session

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

>>> print(Session().UserName) # object-eigenschap

>>> input(Session().UserProfile) # object-eigenschap

Vanuit het menu Extra – Macro's - Macro uitvoeren....


        from <the_module> import Session
            
        def demo_session():
            import screen_io as ui
            ui.MsgBox(Session.Share(),title='Installation Share')  # static methode
            ui.Print(Session.SharedPythonScripts())  # static methode
            s = Session()  # aanmaken sessie
            ui.MsgBox(s.UserName,title='Hello')  # object-eigenschap
            ui.Print(s.UserPythonScripts)  # object-eigenschap
            
        g_exportedScripts = (demo_session,)  # publieke macro's
    

Met LibreOffice BASIC.


        Sub Session_example()
            Dim s As New Session ' instance of Session class
            Print "Locatie gedeelde scripts:", s.SharedScripts
            MsgBox s.UserName,,"Hallo"
            Print s.UserScripts, Chr(13), s.UserPythonScripts
        End Sub ' Session_example
    

Met gebruik van COM/OLE en Visual Basic Scripting.


        ' De service-manager is altijd het toegangspunt
        ' Als er nog geen office loopt dan wordt er een gestart
        Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
        ' service PathSubstitution service toont benodigde informatie
        ' <UserProfile|Share>/Scripts/python locaties
        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"
    

Python sessie-klasse:


        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  # alternatieve naam voor de variabele '$(username)'
            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"])
    
note

Anders dan bij BASIC wordt de normalisatie van de padnaam gedaan binnen de Python sessie-klasse.


LibreOffice BASIC sessie-klasse:


        Option Explicit
        Option Compatible
        Option ClassModule
            
        Private _ps As Object ' Privé lid
            
        Private Sub Class_Initialize()
            GlobalScope.BasicLibraries.LoadLibrary("Tools")
            Set _ps = CreateUnoService("com.sun.star.util.PathSubstitution")
        End Sub ' Constructeur
            
        Private Sub Class_Terminate()
            _ps = Nothing
        End Sub ' Vernietiger
            
        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 ' Accountnaam gebruiker
            userName = _ps.getSubstituteVariableValue("$(username)")
        End Property ' Session.userName
            
        Public Property Get UserProfile() As String ' Systeempad gebruikersprofiel
            userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
        End Property ' Session.userProfile
            
        Public Property Get UserScripts() As String ' Systeempad gebruikersscripts
            userScripts = userProfile() & GetPathSeparator() &"Scripts"
        End Property ' Session.userScripts
            
        Public Property Get UserPythonScripts() As String ' Systeempad Python-scripts
            userPythonScripts = userScripts() & GetPathSeparator() &"python"
        End Property ' Session.userPythonScripts
    

Help ons, alstublieft!