Copy TextField text to System clipboard | Actionscript (AS2 + AS3) Tutorial

Overview

Integrating the clipboard of the operating system with your flash projects is sometimes essential. It’s a very simple and boils down to one basic method… System.setClipboard(). I’ve found a couple other things help the user experience though, such as selecting the text that gets copied and giving the user some sort of feedback to let them know that the text was successfully copied. Here’s a simple way to do it. Have any suggestions to make it better?

I’ve included an as2 version as well as as3. I’ve promised myself to migrate to as3, so I’m not coding anything in 2 that I don’t do in 3 also. This was to discourage me from coding in as2 and to encourage me to code as3, but also let me learn by doing it in both to see the actual differences if I was stuck doing a project in as2. I figured this could help others see the differences between the two versions of actionscript a bit easier and make their own migration as well!

Steps

  1. copy to OS clipboard = System.setClipboard(“Text to COPY”) of System.setClipboard(textBoxToCopy.text)
  2. set selection to text that is copied
  3. give user feedback

Examples and Source

AS2

Get Adobe Flash player

textBox.textBox.text = "Click this text box to copy the text or click the COPY button below. You will see feedback to the user and this text copied to your clipboard!\n\n"+
'copyButton.onRelease = textBox.onPress = function(){\n\tSelection.setFocus("textBox");\n\tSelection.setSelection(0, textBox.text.length);\n\tSystem.setClipboard(textBox.text);\n\ttrace("copied: "+textBox.text);\n\tfeedback("Text Copied!");\n}';

copyButton.onRelease = textBox.onPress = function(){
  Selection.setFocus("textBox.textBox");
    Selection.setSelection(0, textBox.textBox.text.length);
  System.setClipboard(textBox.textBox.text);
  trace("copied: "+textBox.textBox.text);
  textFeedback("Text Copied!");
}

function textFeedback(theFeedback:String){
  feedback.text = theFeedback;
  setTimeout(function(){feedback.text="";}, 1200);
}

AS3

Get Adobe Flash player

textBox.text = "Click this text box to copy the text or click the COPY button below. You will see feedback to the user and this text copied to your clipboard!\n\n"+
'function copyText(e:MouseEvent):void{\n\ttextBox.setSelection(0, textBox.text.length)\n\tSystem.setClipboard(textBox.text);\n\ttrace("copied: "+textBox.text);\n\ttextFeedback("Text Copied!");\n}';

//set it so the textBox selection will show even when textBox has no focus
textBox.alwaysShowSelection = true;

textBox.addEventListener(MouseEvent.CLICK, copyText);
copyButton.addEventListener(MouseEvent.CLICK, copyText);

function copyText(e:MouseEvent):void{
  textBox.setSelection(0, textBox.text.length)
  System.setClipboard(textBox.text);
  trace("copied: "+textBox.text);
  textFeedback("Text Copied!");
}

function textFeedback(theFeedback:String):void {
  feedback.text = theFeedback;
  setTimeout(function(){feedback.text="";}, 1200);
}

Download

Source files: clipboard_as3.fla clipboard_as2+as3.zip

This entry was posted in tutorial and tagged , , , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

21 Comments

  1. Mothmenace
    Posted November 17, 2008 at 12:44 pm | Permalink

    Nice tutorial – many thanks.

    You should include the “import flash.system.*” though.

  2. Posted December 3, 2008 at 2:00 pm | Permalink

    Most helpful – thanks.

    I don’t get the highlighted text effect after copying however – could it me a Mac thing?

    Thanks anyways …

  3. knuckfubuck
    Posted December 4, 2008 at 7:48 pm | Permalink

    Great tutorial. Exactly what I was looking for. Thanks.

  4. Posted December 12, 2008 at 4:33 am | Permalink

    Great work

    Thanks

  5. Posted January 29, 2009 at 5:01 am | Permalink

    Thanks, it helps!

  6. jo
    Posted February 11, 2009 at 9:10 am | Permalink

    hey, this helped me a lot!! thanx so much….rock on!

  7. fakir
    Posted March 17, 2009 at 9:18 am | Permalink

    Thank you!

  8. Joe
    Posted April 15, 2009 at 7:49 am | Permalink

    Thanks! Very well done – exactly what I was looking for. Thanks again, and have an AWESOME week.

    joeFREELANCE

  9. ddee
    Posted April 23, 2009 at 9:16 am | Permalink

    Spot on, great solution!
    Thanks

  10. Joey
    Posted August 9, 2009 at 5:03 pm | Permalink

    Awesome tutorial. And also a big thank’s to Mothmenace who added the import!

    Many thanks

  11. Posted October 29, 2009 at 9:41 am | Permalink

    is there a solution to copy a jpeg into the clipboard using as3

  12. Srikanth
    Posted November 22, 2009 at 4:18 pm | Permalink

    Thanks a lot! Great Tutorial.

    Is there a way to copy the Current URL to the System Clipboard?

  13. Jamus
    Posted February 1, 2010 at 12:14 pm | Permalink

    Awesome. Very helpful.

  14. mandia
    Posted May 9, 2010 at 1:01 pm | Permalink

    hey ;)
    Good tutorial unfortunately it’s not exactly what i am looking for.
    Does anybody know how to copy only a selected text not a whole textfield.
    I hope somebody will help me

  15. Posted August 30, 2011 at 2:06 pm | Permalink

    This is very helpful script thanks!

  16. Alex
    Posted December 28, 2011 at 10:02 pm | Permalink

    Nice tutorial Evan! I have a question though. I am currently on a project to create a ticket logger for our company. This ticket logger would use the system.clipboard as well. However, the problem that I am having is that I cannot make it to work with multiple textareas. I know that the clipboard only holds one data at a time. Thus, the same exact code will not work with multiple textareas. I was informed that I need to gather all those data as one big string but I don’t know exactly how to do it. BTW, I am using Actionscript 3.0 via Flash CS5.

    • Posted January 4, 2012 at 10:37 am | Permalink

      @Alex – it sounds like you could construct a string variable from your different textareas and then copy that to the clipboard.

  17. gojakv
    Posted January 4, 2012 at 9:45 am | Permalink

    Any idea how to use this code to copy desired node which text box reads from from xml file? For example email node:

    Location
    Name
    email address
    Contacts/img/empty.jpg

    Thanks!

    • Posted January 4, 2012 at 10:39 am | Permalink

      @gojakv – I’m not quite sure what you mean. It is possible to get xml data into a text field and then copy to clipboard if that’s what you’re after.

  18. Karl
    Posted January 24, 2012 at 10:59 am | Permalink

    I have been using for some time this nice Banner, from developer FX. They have a really nice Live control panel, that allows you to change the Flash banner on the fly. Thing I was surprised is that the control panel is also in Flash and once you finish changing the attributes you can generate an XML list of attributes with a copy/paste button. I was wondering how they got it to work, and thanks to this tutorial I now know. If you are interested in a nice application of this type of script you can check http://www.flashxml.net/banner-rotator.html

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Recent Posts

    Developing for old browsers is (almost) a thing of the past – (37signals)

    Basecamp announces that it’s new version will not support old browsers! They will of course continue to support them in their “classic” version. But this is good news and as more implement this type of policy the internet will be happy. It used to be one of the biggest pains of web development. Juggling different [...]

    Screen shot 2012-02-10 at 12.59.33 PM

    Vendor Prefixes – about to go south

    What are “standards” coming to? Who’s guilty? Apple and Chrome: They’re supporting vendor prefixed properties like they’re a standard part of development. Firefox, Opera and Internet Explorer: They should have been on the ball more. Need to push their evangelism further. Teach developers that it’s not exclusively -webkit to style elements. All the browsers: experimental [...]

    bio

    An Event Apart Notes: Ethan Marcotte, Responsive Web Design

    Ethan Marcotte has become the father of Responsive Web Design and spent this whole day focused on principles, techniques, gotchas, examples, … all about building and how to build responsive sites. With a sprinkle of mobile first. For Ethan, it all started with this article: http://www.alistapart.com/articles/dao/ Think of architecture, the whole design phase is established [...]

    Webcomm_Montreal

    An Event Apart Notes: Jared Spool, The Curious Properties of Intuitive Web Pages

    Senseless waste of asterisks… Avis used an asterisk to denote optional fields. This means that there is a lot of baggage that comes with an asterixk. Somewhere this symbol got meaning, it’s not in the bible! We can control when something goes from unintuitive to intuitive. A design is intuitive (although technically and grammatically speaking [...]

    untitled-158-2

    An Event Apart Notes: Marco Arment, Bridging the App Gap

    The iPhone changed our industry in 2007: first mobile to have a desktop class web browser and it made people start using their mobile phones as computers! All apps other than apple provided ones were web browser apps. Most of the first apps were branded web browsers. No real difference between using mobile site or [...]

    lukew_space

    An Event Apart Notes: Luke Wroblewski, Mobile to the Future

    MASS MEDIA (in waves) Print, Recordings, Cinema, Radio, Television, Internet, Mobile Mobile is the new Mass Media It certainly is mass and massive. More mobile devices (by far) every day than babies born on the planet. Mobile devices are eating into our personal computing shares. New waves of mobile media eat all previous media waves. [...]

    IMG_4500

    An Event Apart Notes: Josh Clark, Buttons are a Hack

    Mobile and touch should be revolutionizing design and user experience. We don’t want to touch a tiny link or button. Back button is just stupid. Fitz’s Law, make things fat proximal targets for easy touching. People are lazy, let’s as designers LET people be lazy! Maybe even use the whole screen as your control. Eliminate [...]

    nicole_pink_bkg_2012

    An Event Apart Notes: Nicole Sullivan, Our Best Practices are Killing Us

    Grep to for analyze css. CSS duplication is a web-wide problem. Started helping facebook optimize thier site and they had 1.9MB of css loading. The same color showing up hundreds of times. Many many color statements and declarations. !important declarations get dangerous. Sites found to have over 500 !important declarations! float is a serious problem [...]

    ericmeyer

    An Event Apart Notes: Eric Meyer, The Future of CSS

    Cormorant trained to fish for the fishermen. The fishermen tie bands around the birds necks that are. Fishermen fish at night, using lamps they attract bugs, which attract fish, and the cormorant get to fish but cant swallow them and the fishermen take the fish. Browser vendors are promising us new CSS tools, but don’t [...]

    sammyj

    An Event Apart Notes: Ethan Marcotte, Rolling Up Our Responsive Sleeves

    Henry Adams (Descendant of 2 presidents: great-grandson to John Adams and grandson to John Quincy Adams). He lived between the civil war and world war 1. He witnessed the industrial revolution. Chaos was the law of nature, Order was the dream of man Samuel Johnson – funniest man in the 17th Century… Responsive Design: 1. [...]

  • Recent Comments

    me-gamer

    me-gamer

    this is really nice. this made many of my task simple.
    Lori Newman

    Lori Newman

    Just wanted to thank you for your presentation. It was extremely informative and just what I...
    Karl

    Karl

    I have been using for some time this nice Banner, from developer FX. They have a really nice Live...
    Karl

    Karl

    Thank you for this wonderful link… recommend it! Fast, simple, easy… :-)
    Gabriel

    Gabriel

    Hi Valerie, I don’t know if you are still following this post, but I tried seeing if it is...
    avinash

    avinash

    Hi Evan, I am using the same code and trying it on chrome/firefox it is not working on neither...