Pagkuha ng Impormasyon sa Session

Ang pag-compute ng LibreOffice user profile at shared modules system file path ay maaaring isagawa gamit ang Python o gamit ang Basic na mga wika. Ang mga lokasyon ng BeanShell, Java, JavaScript at Python script ay maaaring makuha mula sa impormasyong ito.

Mga halimbawa:

Gamit ang shell ng Python.

>>> from <the_module> import Session

>>> print(Session.SharedPythonScripts()) # static na paraan

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

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

Mula sa Mga Tool – Macros - Patakbuhin ang Macro ... menu.


        from <the_module> import Session
            
        def demo_session():
            import screen_io as ui
            ui.MsgBox(Session.Share(),title='Installation Share') # static na paraan
            ui.Print(Session.SharedPythonScripts()) # static na paraan
            s = Session() # paggawa ng instance
            ui.MsgBox(s.UserName,title='Hello') # object property
            ui.Print(s.UserPythonScripts) # object property
            
        g_exportedScripts = (demo_session,) # pampublikong macro
    

Gamit ang LibreOffice Basic.


        Sub Session_example()
            Dim s As New Session ' instance of Session class
            I-print ang "Lokasyon ng mga nakabahaging script:", s.SharedScripts
            MsgBox s.UserName,,"Hello"
            Print s.UserScripts, Chr(13), s.UserPythonScripts
        End Sub ' Session_example
    

Gamit ang COM/OLE at Visual Basic Scripting na wika.


        ' Ang tagapamahala ng serbisyo ay palaging ang entry point
        ' Kung walang opisina na tumatakbo pagkatapos ay isang opisina ay sinimulan
        Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
        ' Ang serbisyo ng PathSubstitution ay nagpapakita ng impormasyon upang mahinuha
        ' <UserProfile|Share>/Scripts/python lokasyon mula sa
        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"
    

Klase ng Python Session:


        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 # alternatibo sa '$(username)' na 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

Hindi tulad ng Basic, ang pathname normalization ay ginagawa gamit ang Python sa loob ng Session class.


Klase ng LibreOffice Basic Session:


        Option Explicit
        Option Compatible
        Option ClassModule
            
        Pribadong _ps Bilang Object ' Pribadong miyembro
            
        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 Kunin ang UserName() Bilang String ' Pangalan ng user account
            userName = _ps.getSubstituteVariableValue("$(username)")
        End Property ' Session.userName
            
        Public Property Kunin ang UserProfile() Bilang String ' User profile system path
            userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
        End Property ' Session.userProfile
            
        Public Property Kumuha ng UserScripts() Bilang String ' User scripts system path
            userScripts = userProfile() & GetPathSeparator() &"Scripts"
        End Property ' Session.userScripts
            
        Public Property Kumuha ng UserPythonScripts() Bilang String ' User Python scripts system path
            userPythonScripts = userScripts() & GetPathSeparator() &"python"
        End Property ' Session.userPythonScripts
    

Mangyaring suportahan kami!