<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/02/unlocking-the-air-api-part-1.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.22795-</id>
  <updated>2009-11-05T20:24:09Z</updated>
  <title>Comments for AIR API - Introduction to the Clipboard (http://www.insideria.com/2008/02/unlocking-the-air-api-part-1.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.22795</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/02/unlocking-the-air-api-part-1.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=22795" title="AIR API - Introduction to the Clipboard" />
    <published>2008-02-05T13:49:45Z</published>
    <updated>2008-02-05T13:29:12Z</updated>
    <title>AIR API - Introduction to the Clipboard</title>
    <summary>
Native clipboard support is an essential element of OS integration within AIR.  Its use can be a simple as copying a line of text from one document to another or as complex as passing references to an array of files from one application to another.  In this tutorial I will demonstrate the basics of the Clipboard functionality that is included with Adobe AIR.</summary>
    <author>
      <name>David Tucker</name>
      <uri>http://www.davidtucker.net/</uri>
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[<div class="ap_r" style="margin: 0 16px 16px 16px;"><a href="http://www.insideria.com/airLogo-Shadow.jpg" class="highslide" onclick="return hs.expand(this)"><img src="http://www.insideria.com/airLogo-Shadow.jpg" alt="airLogo-Shadow.jpg" title="Click to enlarge" width="148"/></a></div>

<p>Last week I <a href="http://www.insideria.com/2008/01/unlocking-the-air-api-intro.html">introduced a new series</a> here at InsideRIA.com.  To start this series, I will be examining the Clipboard support within AIR.</p>

<h2><strong>Introduction</strong></h2>

<p>Native clipboard support is an essential element of AIR's system integration.  The clipboard's use can be as simple as copying a line of text from one document to another or as detailed as passing references of complex objects from one application to another.  In this tutorial I will demonstrate the basics of the clipboard functionality within the AIR API.</p>

<p>The entire clipboard functionality is contained within three classes in the AIR API.</p>

<ul>
<li><strong>Clipboard</strong> ( <strong>Reference:</strong> <a href="http://livedocs.adobe.com/labs/flex3/langref/flash/desktop/Clipboard.html" target="_blank">Flex</a>, <a href="http://livedocs.adobe.com/labs/air/1/jslr/flash/desktop/Clipboard.html" target="_blank">JavaScript</a> )</li>
<li><strong>ClipboardFormats</strong> ( <strong>Reference:</strong> <a href="http://livedocs.adobe.com/labs/flex3/langref/flash/desktop/ClipboardFormats.html" target="_blank">Flex</a>, <a href="http://livedocs.adobe.com/labs/air/1/jslr/flash/desktop/ClipboardFormats.html" target="_blank">JavaScript</a> )</li>
<li><strong>ClipboardTransferMode</strong> ( <strong>Reference:</strong> <a href="http://livedocs.adobe.com/labs/flex3/langref/flash/desktop/ClipboardTransferMode.html" target="_blank">Flex</a>, <a href="http://livedocs.adobe.com/labs/air/1/jslr/flash/desktop/ClipboardTransferMode.html" target="_blank">JavaScript</a> )</li>
</ul>

<p>The main class that you will use is <strong>Clipboard</strong>.  The two helper classes, <strong>ClipboardFormats</strong>, <strong>ClipboardTransferMode</strong> define constants that are used when calling methods on the main class.  </p>

<h2><strong>The Clipboard Class</strong></h2>

<p>The Clipboard class can be instantiated as often as you would like, but creating an instance of the Clipboard class does not give you access to the operating system clipboard (but rather a clipboard that is limited to your application).  The Clipboard class contains a property, <strong>generalClipboard</strong>, which is a reference to the actual operating system clipboard.  To call a method on the operating system (such as the <code>clear</code> method which we will discuss later), you simply need to use the following syntax:</p>

<div class="acode">
<pre>
// Calling a Method on the Native Clipboard
Clipboard.generalClipboard.clear();
</pre>
</div>

<p>This tells your application to use the actual operating system clipboard for this method.  Note that AIR creates the reference to the operating system clipboard.  It will be there in each of your AIR applications by default.</p>

<h2><strong>Understanding Clipboard Data Formats</strong></h2>

<p>Not all clipboard data is the same.  For example, you wouldn't copy an image from your desktop and paste it into a simple text editor.  If you tried, the application wouldn't receive the data because it is not configured to work with that type of data.  In AIR you have the same flexibility as a developer.  You can configure your application to work with different pieces of data differently (or not at all).  The class ClipboardFormats gives constants for the five clipboard data formats that are predefined in AIR.  Several methods in the Clipboard class require that you pass in one of these constants to define the format of the data that you will be utilizing.  </p>

<ul>
<li><strong>BITMAP_FORMAT</strong> - This format allows for storing bitmap (image) data</li>
<li><strong>FILE_LIST_FORMAT</strong> - This format allows for an array of File objects</li>
<li><strong>HTML_FORMAT</strong> - This format is for HTML formatted text</li>
<li><strong>TEXT_FORMAT</strong> - This format is for plain text</li>
<li><strong>URL_FORMAT</strong> - This format is for text that is formatted as a URL</li>
</ul>

<p>A single clipboard item can have multiple formats.  For example, you could add an image as each of the formats listed above: the image data in bitmap format, a reference to its File object, an img tag in HTML, the filename as text, and the URL for the filename.  By adding an object in multiple formats, you increase the ways that the data can be used.</p>

<p>The Clipboard class contains a method, <code>hasFormat</code> which can tell the developer if the current clipboard has any of these formats in it.  For example, if you were building a simple text editor in AIR, you could test to see if the clipboard contained data in the TEXT_FORMAT.  If the clipboard does contain data in the requested format, the method will return true.  Otherwise the method will return false.</p>

<div class="acode">
<pre>
// Check to See if the Clipboard Contains Text
if(Clipboard.generalClipboard.hasFormat( ClipboardFormats.TEXT_FORMAT)) {
	trace("This Clipboard contains data in the Text Format");
} else {
	trace("This Clipboard doesn't contain data in the Text Format");
}
</pre>
</div>

<h2><strong>Clearing the Clipboard</strong></h2>

<p>In the same way, you can also clear the clipboard with the methods <code>clear</code> (which clears the entire clipboard irrespective of its current data type) and <code>clearData</code> (which clears data in the format that you specify).  The following examples clear the operating system clipboard.</p>

<div class="acode">
<pre>
// Clear All Data
Clipboard.generalClipboard.clear();
// Clear All Bitmap Data
Clipboard.generalClipboard.clearData( ClipboardFormats.BITMAP_FORMAT );
</pre>
</div>

<h2><strong>Placing Data on the Clipboard</strong></h2>

<p>The <code>setData</code> method allows you to specify what format and what data that you want to place on the clipboard.  This method takes three arguments, but for most applications, you will only need to worry about the first two arguments.  The first argument is the format that the data will be in.  In most situations, this will need to match one of the constants defined in ClipboardFormats. The second argument is the data that will be placed on the clipboard.  The following example illustrates the placing of basic text on the operating system's clipboard.</p>

<div class="acode">
<pre>
// Place Basic Text on the Clipboard
Clipboard.generalClipboard.setData( 
	ClipboardFormats.TEXT_FORMAT, "Sample Text" );
</pre>
</div>

<p>In this example the words "Sample Text" were placed on the operating system clipboard (and could now be utilized in other applications).</p>

<h2><strong>Getting Data From the Clipboard</strong></h2>

<p>The method <code>getData</code> allows you to retrieve data from the clipboard.  This method can take two arguments, but in most basic situations, you will only need to worry about the first one.  You only need to pass in the format of the data that you want to retrieve from the clipboard.  The following example retrieves all of the text data from the operating system clipboard.</p>

<div class="acode">
<pre>
// Getting Text from the Clipboard
var textData:String = Clipboard.generalClipboard.getData(
	ClipboardFormats.TEXT_FORMAT );
</pre>
</div>

<h2><strong>Coming Up</strong></h2>

<p>In the next article, I will look at some of the more advanced features of the Clipboard class and its helper classes.  I will also demonstrate a sample application that uses the clipboard functionality.</p>]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.22795-comment:2057208</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.22795" type="text/html" href="http://www.insideria.com/2008/02/unlocking-the-air-api-part-1.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/02/unlocking-the-air-api-part-1.html#comment-2057208" />
    <title>Comment from Poppe on 2009-04-08</title>
    <author>
        <name>Poppe</name>
        <uri>http://moyume.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://moyume.com">
        <![CDATA[<p>I noticed there are some platform differences. <br />
On Mac both Clipboard.generalClipboard.hasFormat( ClipboardFormats.URL_FORMAT) and Clipboard.generalClipboard.hasFormat( ClipboardFormats.FILE_LIST_FORMAT) is true for an image file<br />
but on a PC Clipboard.generalClipboard.hasFormat( ClipboardFormats.URL_FORMAT) it is false.</p>

<p>Great blog guys, keep up the good work!</p>]]>
    </content>
    <published>2009-04-08T21:07:35Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.22795-comment:2057351</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.22795" type="text/html" href="http://www.insideria.com/2008/02/unlocking-the-air-api-part-1.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/02/unlocking-the-air-api-part-1.html#comment-2057351" />
    <title>Comment from Ahmad Fathi Hadi on 2009-04-10</title>
    <author>
        <name>Ahmad Fathi Hadi</name>
        <uri>http://blog.fathihadi.net</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://blog.fathihadi.net">
        <![CDATA[<p>hai, Thanks for this one.<br />
now i understand more than before about Clipboard in AIR.</p>]]>
    </content>
    <published>2009-04-11T06:09:36Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.22795-comment:2066220</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.22795" type="text/html" href="http://www.insideria.com/2008/02/unlocking-the-air-api-part-1.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/02/unlocking-the-air-api-part-1.html#comment-2066220" />
    <title>Comment from Nimesh Nanda on 2009-06-15</title>
    <author>
        <name>Nimesh Nanda</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Hi,<br />
      Can you please help me on how to copy image to system clipboard<br />
in flex application ( Not in AIR).</p>

<p>Please help me .. its urgent ....   :)</p>

<p>Thanks,<br />
Nimesh Nanda</p>]]>
    </content>
    <published>2009-06-15T12:52:49Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.22795-comment:2066221</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.22795" type="text/html" href="http://www.insideria.com/2008/02/unlocking-the-air-api-part-1.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/02/unlocking-the-air-api-part-1.html#comment-2066221" />
    <title>Comment from David Tucker on 2009-06-15</title>
    <author>
        <name>David Tucker</name>
        <uri>http://www.davidtucker.net</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.davidtucker.net">
        <![CDATA[<p>@Nimesh - Unfortunately the BITMAP_FORMAT is only available  in AIR (not Flex).  Your only option is to use the HTML_FORMAT - but in that case, the image would have to be saved on a server - and you would have to wrap it in the appropriate HTML.</p>

<p>David Tucker</p>]]>
    </content>
    <published>2009-06-15T12:59:58Z</published>
  </entry>

</feed
