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.

Eksempler:

Med Phyton skall.

>>> from <the_module> import Session

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

>>> skriv ut(Sesjon().BrukerNavn) # objekt egenskaper

>>> innput(Sesjon().BrukerProfil) # objekt egenskaper

Fra Verktøy - Makroer - Kjør makro ... -menyen.


        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 = Sesjon()  # opprett instans
            ui.MsgBox(s.BrukerNavne,tittel='Hallo')  # objekt egenskaper
            ui.Utskrift(s.BrukerPythonSkript)  # objekt egenskaper
            
        g_eksporteteSkript = (demo_sesjon,)  # felles makroer
    

Ved LibreOffice Basic


        Sub Session_example()
            Dim s As New Session ' instance of Session class
            Skriv ut "Delte skript plasseing:", s.DelteSkript
            MsgBox s.BrukerNavn,,"Hallo"
            Print s.UserScripts, Chr(13), s.UserPythonScripts
        End Sub ' Session_example
    

Bruk COM/OLE og Visual Basic Skriptspråk.


        Tjenesteadministratoren er alltid startpunktet
        ' Hvis office ikke kjøres, vil office states opp
        Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
        'PathSubstitution-tjeneste viser informasjon som skal utledes
        <UserProfile|Share>Scripts/python plassering fra
        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 Sesjon 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  # alternativ til '$(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"])
    
note

I motsetning til Basic utføres normalisering av stinavn med Python i sesjonsklassen.


LibreOffice Basic Sesjon klasse:


        Option Explicit
        Option Compatible
        Option ClassModule
            
        Private _ps As Object ' Privat medlem
            
        Private Sub Class_Initialize()
            GlobalScope.BasicLibraries.LoadLibrary("Tools")
            Set _ps = CreateUnoService("com.sun.star.util.PathSubstitution")
        Slutt på Sub ' Constructor
            
        Private Sub Class_Terminate()
            _ps = Nothing
        Slutt på 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
            
        Offentlig egenskap Hent brukernavn () som String 'Brukernavn
            userName = _ps.getSubstituteVariableValue("$(username)")
        End Property ' Session.userName
            
        Offentlig egenskap Hent UserProfile () Som String 'Brukerprofil systembane
            userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
        End Property ' Session.userProfile
            
        Offentlig egenskap Hent UserScripts() Som String 'Brukerskript systembane
            userScripts = userProfile() & GetPathSeparator() &"Scripts"
        End Property ' Session.userScripts
            
        Offentlig egenskap Hent UserPhytonScripts() Som String 'Brukerskript systembane
            userPythonScripts = userScripts() & GetPathSeparator() &"python"
        End Property ' Session.userPythonScripts
    

Supporter oss!