S'està obtenint informació de la sessió

Computing Collabora Office perfil d'usuari i mòduls compartits Els camins de fitxer del sistema es poden realitzar amb Python o amb llenguatges bàsics. Les ubicacions de scripts Java JavaScript i Python es poden derivar d'aquesta informació.

Exemples:

Amb l'intèrpret de Python.

>>> from <the_module> import Session

>>> print(Session.SharedPythonScripts()) # mètode estàtic

>>> print(Session().UserName) # propietat de l'objecte

>>> input(Session().UserProfile) # propietat de l'objecte

Des del menú Eines ▸ Macros ▸ Executa una macro.

from <the_module> import Session

def demo_session():
    import screen_io as ui
    ui.MsgBox(Session.Share(),title='Instal·lació compartida')  # mètode estàtic
    ui.Print(Session.SharedPythonScripts()) # mètode estàtic
    s = Session()  # creació de la instància
    ui.MsgBox(s.UserName,title='Hola')  # propietat de l'objecte
    ui.Print(s.UserPythonScripts)  # propietat de l'objecte

g_exportedScripts = (demo_session,)  # macros públiques

Amb el Collabora Office Basic.

Sub Session_example()
    Dim s As New Session ' instance of Session class
    Imprimeix «Ubicació dels scripts compartits» s.SharedScripts
    MsgBox s.UserName,,"Hola"
    Print s.UserScripts, Chr(13), s.UserPythonScripts
End Sub ' Session_example

Amb el COM/OLE i el llenguatge VBScript

' El gestor de serveis sempre és el punt d'entrada
' Si no hi ha cap aplicació office executant-se, se n'iniciarà una
Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
' El servei PathSubstitution mostra informació per inferir
' <UserProfile|Share>/Scripts/python ubicacions des de
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"

Classe Session del 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  # alternativa a la 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

A diferència de Basic, la normalització de pathname amb Python es realitza dins de la classe Session.


Classe Session del Collabora Office Basic:

Option Explicit
Option Compatible
Option ClassModule

Private _ps As Object ' membre privat

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 Get UserName() As String ' nom del compte d'usuari
    userName = _ps.getSubstituteVariableValue("$(username)")
End Property ' Session.userName

Public Property Get UserProfile() As String ' camí del perfil d'usuari al sistema
    userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
End Property ' Session.userProfile

Public Property Get UserScripts() As String ' camí dels scripts d'usuari al sistema
    userScripts = userProfile() & GetPathSeparator() &"Scripts"
End Property ' Session.userScripts

Public Property Get UserPythonScripts() As String ' camí dels scripts d'usuari en Python al sistema
    userPythonScripts = userScripts() & GetPathSeparator() &"python"
End Property ' Session.userPythonScripts

Ens cal la vostra ajuda!