Få informasjon om økta

Handsaming av brukarprofilar og delte systemfilstiar for modular i LibreOffice kan gjerast i Python og i Basic. Du kan også finna plasseringa av BeanShell-, Java-, JavaScript- og Python-skript ut frå desse opplysningane.

Eksempel:

Med Pythonskal.

>>> from <the_module> import Session

>>> print(Session.DeltePythonSkript()) # statisk metode

>>> skriv ut(Session().BrukarNamn) # objekteigenskapar

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

Frå menyen Verktøy → Makroar → Køyr makro,


        from <the_module> import Session
            
        def demo_session():
            import screen_io as ui
            ui.MsgBox(Session.Share(),title='Installation Share')  # statisk metode
            ui.Print(Session.SharedPythonScripts())  # statisk metode
            s = Session()  # lag ei økt
            ui.MsgBox(s.UserName,title='Hallo')  # objekteigenskapar
            ui.Print(s.UserPythonScripts)  # objekteigenskapar
            
        g_exportedScripts = (demo_session,)  # felles makroar
    

Med LibreOffice Basic.


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

Bruka COM/OLE og Visual Basic skript-språk.


        ' Tenesteadministratoren er alltid startpunktet
        ' Viss det ikkje køyrer eit kontorprogram, vert eit slikt opna
        Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
        'PathSubstitution-tenesta viser informasjon som skal utgreiast
        ' <UserProfile|Share>/Scripts/python-plassering frå
        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 øktklasse


        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 variabelen '$(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

I motsetnad til Basic, vert normaliseringa av stinamn utført med Python i øktklassen.


LibreOffice Basic øktklasse:


        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")
        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 ' Brukarnamn
            userName = _ps.getSubstituteVariableValue("$(username)")
        End Property ' Session.userName
            
        Public Property Get UserProfile() As String ' Systemsti for brukarprofilen
            userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
        End Property ' Session.userProfile
            
        Public Property Get UserScripts() As String ' Systemsti for brukarskript
            userScripts = userProfile() & GetPathSeparator() &"Scripts"
        End Property ' Session.userScripts
            
        Public Property Get UserPythonScripts() As String ' Systemsti for brukarskript i Python
            userPythonScripts = userScripts() & GetPathSeparator() &"python"
        End Property ' Session.userPythonScripts
    

Støtt oss!