Posedźenske informacije wobstarać

Wobličenje wužiwarskeho profila LibreOffice a systemowych datajowych šćežkow zhromadnje wužitych modulow da so z Python abo Basic wuwjesć. Městna skriptow BeanShell, Java, JavaScript a Python dadźa so z tutych informacijow wotwodźić.

Přikłady:

Z konsolu Python.

>>> from <the_module> import Session

>>> print(Session.SharedPythonScripts()) # statiska metoda

>>> print(Session().UserName) # objektowa kajkosć

>>> input(Session().UserProfile) # objektowa kajkosć

Wubjerće Nastroje – Makra – Makro wuwjesć….


        from <the_module> import Session
            
        def demo_session():
            import screen_io as ui
            ui.MsgBox(Session.Share(),title='Installation Share') # statiska metoda
            ui.Print(Session.SharedPythonScripts()) # statiska metoda
            s = Session() # wutworjenje instancy
            ui.MsgBox(s.UserName,title='Hello') # objektowa kajkosć
            ui.Print(s.UserPythonScripts) # objektowa kajkosć
            
        g_exportedScripts = (demo_session,) # zjawne makra
    

Z LibreOffice Basic.


        Sub Session_example()
            Dim s As New Session ' instance of Session class
            Print "Městno za zhromadne skripty:", s.SharedScripts
            MsgBox s.UserName,,"Witaj"
            Print s.UserScripts, Chr(13), s.UserPythonScripts
        End Sub ' Session_example
    

COM/OLE a skriptowu rěč Visual Basic wužiwać.


        ' Słužbny zrjadowak je přeco zastupny dypk
        ' Jeli so Office njewuwjedźe, so Office startuje
        Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
        ' Słužba PathSubstitution informacije prezentuje, na kotrež so da sćěhować
        ' Městna <UserProfile|Share>/Scripts/python z
        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"
    

Posedźenska klasa Python:


        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 # alternatiwa k wariabli '$(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

Na rozdźěl wot Basic so normalizacija mjeno šćežki z Python w posedźenskej klasy wuwjedźe.


Posedźenska klasa LibreOffice Basic:


        Option Explicit
        Option Compatible
        Option ClassModule
            
        Private _ps As Object ' Priwatny čłon
            
        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 ' Mjeno wužiwarskeho konta
            userName = _ps.getSubstituteVariableValue("$(username)")
        End Property ' Session.userName
            
        Public Property Get UserProfile() As String ' Systemowa šćežka wužiwarskeho profila
            userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
        End Property ' Session.userProfile
            
        Public Property Get UserScripts() As String ' Systemowa šćežka za wužiwarske skripty
            userScripts = userProfile() & GetPathSeparator() &"Scripts"
        End Property ' Session.userScripts
            
        Public Property Get UserPythonScripts() As String ' Systemowa šćežka za wužiwarske skripty Python
            userPythonScripts = userScripts() & GetPathSeparator() &"python"
        End Property ' Session.userPythonScripts
    

Prošu podpěrajće nas!