[request] want to put RTF-formated text to the clipboard - solution

Bug #1317773 reported by RaiMan
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SikuliX
In Progress
Medium
RaiMan

Bug Description

Hi all!

I need put the RTF formated string and "text/rtf" DataFlavor into clipboard . Here is code what I started:

from java.awt import Toolkit
from java.awt.datatransfer import Clipboard, StringSelection, DataFlavor, UnsupportedFlavorException
from java.io import *

def ClipboardPutRtfString(rtf_str):
    cb_data = StringSelection(rtf_str)
    cb = Toolkit.getDefaultToolkit().getSystemClipboard()
    RTF_FLAVOR = DataFlavor("text/rtf", "Rich Text Format")
    p = (cb_data, RTF_FLAVOR)
    cb.setContents(p, None)
if __name__ == '__main__' :
    RTF_STRING = u"{\\rtf1\\ansi\\deff0\r\n{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;}\r\nThis line is the default color\\line\r\n\\cf2\r\nThis line is красная\\line\r\n\\cf1\r\nThis line is the default color\r\n}\r\n}"
    ClipboardPutRtfString(RTF_STRING)

But now I'm sticked with how prepare transferable p object in Jython to pass it to setContents method, because my example gets error:

[error] TypeError ( setContents(): 1st arg can't be coerced to java.awt.datatransfer.Transferable )
[error] --- Traceback --- error source first line: module ( function ) statement ##: main ( ClipboardPutRtfString ) cb.setContents(p, None)
[error] --- Traceback --- end --------------

Revision history for this message
RaiMan (raimund-hocke) wrote :

Thanks for the solution:

Here is a codefix what doing what I need:

def putRTFStringToClipboard(rtf_str):
    from java.awt.datatransfer import StringSelection
    from java.awt.datatransfer import Clipboard
    from javax.activation import DataHandler
    from java.awt import Toolkit
    from java.lang import String
    from java.nio import ByteBuffer
    from java.lang import StringBuffer
    from java.io import InputStream, ByteArrayInputStream
    cb = Toolkit.getDefaultToolkit().getSystemClipboard()
    cb_data = map(ord, rtf_str)
    ba = ByteArrayInputStream(cb_data)
    cb.setContents(DataHandler(ba,"text/rtf"), None)

Changed in sikuli:
status: New → In Progress
importance: Undecided → Medium
assignee: nobody → RaiMan (raimund-hocke)
milestone: none → 1.2.0
RaiMan (raimund-hocke)
Changed in sikuli:
milestone: 2.0.0 → 2.1.0
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Related questions

Remote bug watches

Bug watches keep track of this bug in other bug trackers.