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
- copy to OS clipboard = System.setClipboard(“Text to COPY”) of System.setClipboard(textBoxToCopy.text)
- set selection to text that is copied
- give user feedback
Examples and Source
AS2
[kml_flashembed fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2008/11/clipboard_as2.swf” targetclass=”flashmovie” publishmethod=”dynamic” width=”550″ height=”400″]
[/kml_flashembed]
[cc lang=”actionscript” tab_size=”2″ lines=”40″]
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);
}
[/cc]
AS3
[kml_flashembed fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2008/11/clipboard_as3.swf” targetclass=”flashmovie” publishmethod=”dynamic” width=”550″ height=”400″]
[/kml_flashembed]
[cc lang=”actionscript” tab_size=”2″ lines=”40″]
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);
}
[/cc]
Download
Source files: clipboard_as3.fla clipboard_as2+as3.zip
Nice tutorial – many thanks.
You should include the “import flash.system.*” though.
Most helpful – thanks.
I don’t get the highlighted text effect after copying however – could it me a Mac thing?
Thanks anyways …
Great tutorial. Exactly what I was looking for. Thanks.
Great work
Thanks
Thanks, it helps!
hey, this helped me a lot!! thanx so much….rock on!
Thank you!
Thanks! Very well done – exactly what I was looking for. Thanks again, and have an AWESOME week.
joeFREELANCE
Spot on, great solution!
Thanks
Awesome tutorial. And also a big thank’s to Mothmenace who added the import!
Many thanks
is there a solution to copy a jpeg into the clipboard using as3
Thanks a lot! Great Tutorial.
Is there a way to copy the Current URL to the System Clipboard?
There are a couple ways to do it, mostly using ExternalInterface to get the url (which I wrote about here: https://52.25.247.172.xip.io/circlecube/2008/03/tutorial/get-current-url-and-query-string-parameters-to-flash-tutorial/). and then it’s just as easy as copying the string to the clipboard.
Awesome. Very helpful.
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
This is very helpful script thanks!
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.
@Alex – it sounds like you could construct a string variable from your different textareas and then copy that to the clipboard.
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!
@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.
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
Hi, I just have a problem with the copying and pasting the information to a different text editor. It does not recognize new lines. For example, if i copy
function copyText(e:MouseEvent):void{
textBox.setSelection(0, textBox.text.length)
System.setClipboard(textBox.text);
trace(“copied: “+textBox.text);
textFeedback(“Text Copied!”);
if I paste it to Notepad it shows as a single line.