Top > OOoPython > mytools > transferable

transferable Edit

クリップボードにデータをコピーするためのクラスと関数。

使い方

ctx = XSCRIPTCONTEXT.getComponentContext()

from mytools.transferable import copy_string
copy_string(ctx, u'コピーする文字列')

copy_string 関数を使うと簡単に文字列をクリップボードにコピーできます。

transferable.py

  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
# -*- coding: utf_8 -*-
 
import unohelper
 
from com.sun.star.datatransfer import XTransferable
from com.sun.star.datatransfer import DataFlavor
 
 
class trans(unohelper.Base, XTransferable):
    """Generic .datatransfer.XTransferable interface."""
    def __init__(self, data, fs):
        self.data = data
        self.fs = fs
    
    def getTransferData(self, f):
        try:
            i = [ff.MimeType for ff in self.fs].index(f.MimeType)
            return self.data[i]
        except:
            return
    
    def getTransferDataFlavors(self):
        return self.fs
    
    def isDataFlavorSupported(self, f):
        try:
            [ff.MimeType for ff in self.fs].index(f.MimeType)
            return True
        except:
            return False
 
 
def copy2clipboard(ctx, data, fs):
    """Copy data to the clipboard."""
    t = trans(data, fs)
    c = ctx.ServiceManager.createInstanceWithContext(
        "com.sun.star.datatransfer.clipboard.SystemClipboard",ctx)
    c.setContents(t, None)
 
 
def copy_string(ctx, string):
    """Copy string to the clipboard."""
    f = create_DataFlavor(u'text/plain;charset=utf-16')
    copy2clipboard(ctx, (string,), (f,))
 
 
def create_DataFlavor(m):
    f = DataFlavor()
    f.MimeType = m
    return f

Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup   Help   RSS of recent changes