Sitzungsinformationen abrufen

Das Berechnen des Anwenderprofil von LibreOffice und der Systemdateipfade der gemeinsam genutzten Module kann mit den Skriptsprachen Python oder mit Basic durchgeführt werden. Aus diesen Informationen können Speicherorte für Skripte von BeanShell, Java, JavaScript und Python abgeleitet werden.

Beispiele:

Mit Python-Shell.

>>> from <the_module> import Session

>>> print(Session.SharedPythonScripts()) # statische Methode

>>> print(Session().UserName) # Objekteigenschaft

>>> input(Session().UserProfile) # Objekteigenschaft

Wählen Sie Extras – Makros – Makro ausführen….


        from <the_module> import Session
            
        def demo_session():
            import screen_io as ui
            ui.MsgBox(Session.Share(),title='Installation Share')  # Statische Methode
            ui.Print(Session.SharedPythonScripts()) # statische Methode
            s = Session() # Instanzerstellung
            ui.MsgBox(s.UserName,title='Hello')  # Objekteigenschaft
            ui.Print(s.UserPythonScripts) # Objekteigenschaft
            
        g_exportedScripts = (demo_session,) # öffentliche Makros
    

Mit LibreOffice Basic.


        Sub Session_example()
            Dim s As New Session ' instance of Session class
            Print "Speicherort für freigegebene Skripte:", s.SharedScripts
            MsgBox s.UserName,,"Hallo"
            Print s.UserScripts, Chr(13), s.UserPythonScripts
        End Sub ' Session_example
    

COM/OLE und Visual Basic Skript-Sprache verwenden.


        ' Der Service Manager ist immer der Einstiegspunkt
        ' Wenn kein Office ausgeführt wird, wird ein Office gestartet
        Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
        ' Der Dienst PathSubstitution enthält Informationen, auf die geschlossen werden kann
        ' Speicherort <Anwenderprofil|Öffentlich>/Scripts/python von
        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-Sitzungsklasse:


        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 zur Variable '$(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

Im Gegensatz zu Basic wird die Pfadnamen-Normalisierung mit Python in der Session-Klasse durchgeführt.


LibreOffice Basic Sitzungsklasse:


        Option Explicit
        Option Compatible
        Option ClassModule
            
        Private _ps As Object ' Privates Mitglied
            
        Private Sub Class_Initialize()
            GlobalScope.BasicLibraries.LoadLibrary("Tools")
            Set _ps = CreateUnoService("com.sun.star.util.PathSubstitution")
        End Sub ' Konstruktor
            
        Private Sub Class_Terminate()
            _ps = Nothing
        End Sub ' Destruktor
            
        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 ' Name des Nutzerkontos
            userName = _ps.getSubstituteVariableValue("$(username)")
        End Property ' Session.userName
            
        Public Property Get UserProfile() As String ' Systempfad des Anwenderprofil
            userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
        End Property ' Session.userProfile
            
        Public Property Get UserScripts() As String ' Systempfad für Benutzerskripte
            userScripts = userProfile() & GetPathSeparator() &"Scripts"
        End Property ' Session.userScripts
            
        Public Property Get UserPythonScripts() As String ' Systempfad für Benutzer-Python-Skripte
            userPythonScripts = userScripts() & GetPathSeparator() &"python"
        End Property ' Session.userPythonScripts
    

Bitte unterstützen Sie uns!