How to Copy To and From the Clipboard using JavaScript in AIR Beta 2

In case there's another poor soul out there who just needs to know how to work with the clipboard using JavaScript in AIR Beta 2, I'm posting this very simple example of how to get text from the clipboard, clear the clipboard, and send text to the clipboard.

Getting text from the clipboard:

var ctext= air.Clipboard.generalClipboard.getData("air:text","cloneOnly");
alert("ctext is: " + ctext);

Clearing the clipboard:

air.Clipboard.generalClipboard.clear();

Sending text to the clipboard:

var myText= "Some text";
var clip= air.Clipboard.generalClipboard;
clip.setData("air:text",myText,false);

The functions involved are all covered in the JavaScript Language Reference for Adobe AIR, but I found the documentation less than clear, and all the examples I found in blog posts and in the Adobe forums were from earlier versions of AIR/Apollo. Even the PDF documentation that came with the AIR download was out-of-date (note to Adobe: that's not good).

This example only covers text, but it should provide enough of a guide so that, along with the language reference, you can work with other data types.

Update: the clipboard functions listed above also seem to work unchanged in AIR Beta 3

Related Blog Entries

Comments
THANK YOU!! i was wondering why my clipboard code wasn't working. even the updated documentation for beta 3 (2007-12-12) is still incorrect!
# Posted By bunnyhero | 1/2/08 7:56 PM
You're welcome: glad to hear that I saved someone else a little pain.
# Posted By Brian Swartzfager | 1/3/08 8:48 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.