获取会话信息

可使用 Python 或 Basic 语言计算 Collabora Office 用户配置文件与共享模块系统文件路径。BeanShell、Java、JavaScript 及 Python 脚本位置可从该信息推得。

示例:

使用 Python shell。

>>> from <the_module> import Session

>>> print(Session.SharedPythonScripts()) # 静态方法

>>> print(Session().UserName) # 对象属性

>>> input(Session().UserProfile) # 对象属性

从「工具 – 宏 - 运行宏...」菜单。

from <the_module> import Session

def demo_session():
    import screen_io as ui
    ui.MsgBox(Session.Share(),title='默认安装位置的共享目录')  # 静态方法
    ui.Print(Session.SharedPythonScripts())  # 静态方法
    s = Session()  # 创建实例
    ui.MsgBox(s.UserName,title='Hello')  # 对象属性
    ui.Print(s.UserPythonScripts)  # 对象属性

g_exportedScripts = (demo_session,)  # 公共宏

使用 Collabora Office Basic。

Sub Session_example()
    Dim s As New Session ' instance of Session class
    Print "共享脚本位置:", s.SharedScripts
    MsgBox s.UserName,,"哈啰"
    Print s.UserScripts, Chr(13), s.UserPythonScripts
End Sub ' Session_example

使用 COM/OLE 与 Visual Basic 脚本语言。

' 服务管理器总是进入点
' 如果没有 office 在运行,则启动一个 office
Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
' PathSubstitution 服务展示可推断的信息
' <UserProfile|Share>/Scripts/python 位置来自
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 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  # 「$(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

与 Basic 不同,路径名的正规化在 Session 类内部是通过 Python 实现的。


Collabora Office Basic Session 类:

Option Explicit
Option Compatible
Option ClassModule

Private _ps As Object ' 私有成员

Private Sub Class_Initialize()
    GlobalScope.BasicLibraries.LoadLibrary("Tools")
    Set _ps = CreateUnoService("com.sun.star.util.PathSubstitution")
End Sub ' 构造函数

Private Sub Class_Terminate()
    _ps = Nothing
End Sub ' 析构函数

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 ' 用户账户名称
    userName = _ps.getSubstituteVariableValue("$(username)")
End Property ' Session.userName

Public Property Get UserProfile() As String ' 用户配置文件系统路径
    userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
End Property ' Session.userProfile

Public Property Get UserScripts() As String ' 用户脚本系统路径
    userScripts = userProfile() & GetPathSeparator() &"Scripts"
End Property ' Session.userScripts

Public Property Get UserPythonScripts() As String ' 用户 Python 脚本系统路径
    userPythonScripts = userScripts() & GetPathSeparator() &"python"
End Property ' Session.userPythonScripts

请支持我们!