views:

844

answers:

3

I have a XULRunner application that needs to copy image data to the clipboard. I have figured out how to handle copying text to the clipboard, and I can paste PNG data from the clipboard. What I can't figure out is how to get data from a data URL into the clipboard so that it can be pasted into other applications.

This is the code I use to copy text (well, XUL):

var transferObject=Components.classes["@mozilla.org/widget/transferable;1"].
    createInstance(Components.interfaces.nsITransferable);

var stringWrapper=Components.classes["@mozilla.org/supports-string;1"].
    createInstance(Components.interfaces.nsISupportsString);

var systemClipboard=Components.classes["@mozilla.org/widget/clipboard;1"].
    createInstance(Components.interfaces.nsIClipboard);

var objToSerialize=aDOMNode;

transferObject.addDataFlavor("text/xul");

var xmls=new XMLSerializer();
var serializedObj=xmls.serializeToString(objToSerialize);

stringWrapper.data=serializedObj;

transferObject.setTransferData("text/xul",stringWrapper,serializedObj.length*2);

And, as I said, the data I'm trying to transfer is a PNG as a data URL. So I'm looking for the equivalent to the above that will allow, e.g. Paint.NET to paste my app's data.

Thanks in advance.

+2  A: 

Neal Deakin has an article on manipulating the clipboard in xulrunner. I'm not sure if it answers your question specifically, but it's definitely worth checking out.

pc1oad1etter
That is a really good article, in fact it's the basis for the code above. Unfortunately it doesn't deal with getting binary data onto the clipboard. Thanks for the response!
Joel Anair
Doh! #xulrunner?
pc1oad1etter
What is #xulrunner?
Joel Anair
Just suggesting, could join xulrunner's irc channel -- #xulrunner at irc.freenode.net. Someone there might be able to answer your question.
pc1oad1etter
Wow. I had no idea that was there; I've never done IRC. Thanks for the suggestion.
Joel Anair
+2  A: 

Here's a workaround that I ended up using that solves the problem pretty well. The variable dataURL is the image I was trying to get to the clipboard in the first place.

var newImg=document.createElement('img');
newImg.src=dataURL;

document.popupNode=newImg;

var command='cmd_copyImageContents'

var controller=document.commandDispatcher.getControllerForCommand(command);

if(controller && controller.isCommandEnabled(command)){
    controller.doCommand(command);
}

That copies the image to the clipboard as an 'image/jpg'.

Joel Anair
Thanks for sharing with us Joel! +1
pc1oad1etter
A: 

this is so difficult and i can't get it working. spent two days on it