Hi,

I am trying to retrieve RTF (Rich Text Format) data from the clipboard
in Javascript for processing, using XPCOM to access the clipboard (this
is under Windows). Getting the data isn't much of a problem:

        var clip = Components.classes["@mozilla.org/widget/clipboard;1"].
                     getService(Components.interfaces.nsIClipboard);
        if (!clip) return false;

        var trans = Components.classes["@mozilla.org/widget/transferable;1"].
                createInstance(Components.interfaces.nsITransferable);
        if (!trans) return false;
        trans.addDataFlavor("Rich Text Format");

        clip.getData(trans, clip.kGlobalClipboard);

        var str = new Object();
        var strLength = new Object();

        try
        {
                trans.getTransferData("Rich Text Format", str, strLength);
        }
        catch(e) { alert("Nothing to paste"); return false; }

        if (str)
                str = str.value.
                        QueryInterface(Components.interfaces.nsISupportsString);
        else
                return false;

So now I have an nsISupportsString interface containing the data. The
problem is that the data is encoded as Windows-1252, but since Mozilla
doesn't know about RTF, it assumes that it's Unicode data. I tried to
convert it using the scriptable Unicode converter, but the result is
that I lose every other character. I trawled around in the XPConnect
sources, and it appears to "deflate" Unicode strings when the XPCOM
interface is expecting an 8-bit char*. What's more, I couldn't see any
data type that I can pass to it where it would treat it as a native
8-bit string.

I tried writing the data to a binary stream, I tried writing to a
scriptable stream, I tried storing it in an atom... nothing helps. Now
I'm frustrated. I suppose I can loop over the string and insert an
8-bit null after every character manually, but I simply can't believe
that's the best way to do this. Actually I'm guessing there's a really
obvious way, but I'm not a Javascript guy and I'm stumped.

Matt

_______________________________________________
Mozilla-xpcom mailing list
[email protected]
http://mail.mozilla.org/listinfo/mozilla-xpcom

Reply via email to