Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

<< Prev 5 | Next 5

Copy a text from Flash to to the sytem clipboard

I have found out that in order to copy a text from Flash to to the sytem clipboard we need a visible textarea, if we don't have it, we get an empty string. But in order to copy from clipboard to Flash we don't need a visible textarea (a hidden textarea is enough). This rule applies to the IE4 and above version, but I have written a IE 5 and above version which is much more simple and doesn't require any textareas.
Here is some background info for people interested in this subject.

In the Flash movie we have a dynamic text area with the variable name "text". We copy the clipboard content from and into it.
There is a button (copy) with that action:

on (release) {
        var myText = _root.text;
        getURL ("JavaScript:flashToClipBoard('"+myText+"'); void(0);");
}

When the button Copy is pressed the text in the dynamic textbox is passed as the argument of our JavaScript function.
I have posted the HTML code here:
//************************************IE4 and above version************************************//
<html>
<head>
<title>Flash To Clipboard</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="Javascript">
<!--
// Flash to Clipboard
// By drZoode (zoode@usa.net)
// IE 4 and above specific execCommand methods
// are used to get access to the clipboard
// the innerText property of a hidden textarea (id="hiddenBox")
// is used to manipulate the text range

function flashToClipBoard(flashInput) {
        // the argument of this function is passed from Flash
        myForm.hiddenBox.innerText = flashInput;
        copiedText = myForm.hiddenBox.createTextRange();
        copiedText.execCommand("Copy");
        pastedText = myForm.pasteHere.createTextRange();
        pastedText.execCommand("Paste");
}

function clipBoardToFlash() {
        pastedText = myForm.pasteHere.createTextRange();
        pastedText.execCommand("Paste");
        var goToFlash = myForm.pasteHere.innerText;
        // we use a Flash Method to *simulate* the pasting effect
        // as this is a IE only exercise we do not care for Netscape friendly syntax
        window.movie.SetVariable("_root.text", goToFlash);
}
//-->
</script>
</head>

<body bgcolor="#999999" text="#000000">

<form id="myForm">
<table width="600" border="0" cellspacing="5" cellpadding="5">
<tr>
<td>
<textarea rows="10" cols="50" id="hiddenBox" style="display:none" name="textarea">
</textarea>
</td>
</tr>
<tr>
<td>
<p>The text in the clipboard will appear here.
<br>You can open a text editor program (e.g. Notepad) and paste the text there.</p>
</td>
</tr>
<tr>
<td>
<textarea rows="6" cols="40" id="pasteHere" name="textarea2">
</textarea>
</td>
</tr>
</table>
<br>
<br>
</form>

<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"
WIDTH=500 HEIGHT=350 ID="movie">
<PARAM NAME=movie VALUE="clipboard.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#999999> <EMBED src="clipboard.swf" quality=high bgcolor=#999999 WIDTH=500 HEIGHT=350 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></EMBED>
</OBJECT>
<p> NOTE: If the text you have copied in Flash has carriage returns you cannot copy it back to the clipboard.
<p><b>Here is some text that you can copy to the clipboard</b>
<br>(Choose Edit>Copy or right-click and copy).
<br>
<br>'Twas brillig, and the slithy toves
<br>Did gyre and gimble in the wabe:
<br>All mimsy were the borogoves,
<br>And the mome raths outgrabe.
<br><br>
<button onClick = "clipBoardToFlash()">Paste to Flash </button>
</p>
</body>
</html>
//************************************************************************//

If we don't use any form we can omit the id reference of the form in our scripts. The first form (id="pasteHere") is used to display the clipboard content, so you can do without it. flashToClipBoard function works fine when the textarea "hiddenBox" is not displayed.

function flashToClipBoard(flashInput) {
        // the argument of this function is passed from Flash
        hiddenBox.innerText = flashInput;
        copiedText = hiddenBox.createTextRange();
        copiedText.execCommand("Copy");
}

But the second function (flashToClipBoard) requires the textarea to be displayed (Style="display:none") will give us an empty string. (We cannot use a hidden textarea for that function)

But if we don't care for IE4.x browsers and we want to write a script for IE 5 and above browsers, things are much more easy for us. Here is the version for IE 5 and above:
(You can use the same Flash movie in that example)
//************************************IE5 and above version************************************//
<html>
<head>
<title>Flash To Clipboard</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="Javascript">
<!--
// Flash to Clipboard - IE 5 and above version
// By drZoode (zoode@usa.net)
// this version uses IE 5 and above specific clipboardData object

function flashToClipBoard(flashInput) {
        // the argument of this function is passed from Flash
        var fromFlash = window.clipboardData.setData('Text', flashInput);
}
function clipBoardToFlash() {
        var goToFlash = window.clipboardData.getData('Text');
        // we use a Flash Method to *simulate* the pasting effect
        // as this is a IE only exercise we do not care for Netscape friendly syntax
        window.movie.SetVariable("_root.text", goToFlash);
}
//-->
</script>
</head>

<body bgcolor="#999999" text="#000000">
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"
WIDTH=500 HEIGHT=350 ID="movie">
<PARAM NAME=movie VALUE="clipboard.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#999999> <EMBED src="clipboard.swf" quality=high bgcolor=#999999 WIDTH=500 HEIGHT=350 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></EMBED>
</OBJECT>
<p> NOTE: If the text you have copied in Flash has carriage returns you cannot copy it back to the clipboard.
<p><b>Here is some text that you can copy to the clipboard</b>
<br>(Choose Edit>Copy or right-click and copy).
<br>
<br>'Twas brillig, and the slithy toves
<br>Did gyre and gimble in the wabe:
<br>All mimsy were the borogoves,
<br>And the mome raths outgrabe.
<br><br>
<button onClick = "clipBoardToFlash()">Paste to Flash </button>
</p>
</body>
</html>
//************************************************************************

Netscape requires a trusted script to give access to the system clipboard. I have figured out the basic structure of such a script but (as I am not a Java programmer) I couldn't find out the exact syntax to instantiate the Java object and use its methods. Probably it should look like this

<SCRIPT LANGUAGE="JavaScript" ARCHIVE="myArchive.jar" ID="myID">
function getMyClipboard() {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalSystemClipboardAccess");
        // **************
        // instantiate the Java object and use its methods
        // cb = new Packages.java.awt.datatransfer.Clipboard(String);
        // myCb = cb.getContents(Object);
        //document.write('My clipboard:<BR>'+myCb);
        // **************
}
</SCRIPT>

If someone knows the JAVA syntax and can post it on FORUMS, it would be nice.
It is also interesting that such a widely used browser as Internet Explorer 5.x has such a few security restrictions (regarding the clipboard access).


HTML view / download


Posted by: No name | website http://
Default home page from within Flash
from flash:
on (release) {
        fscommand ("MakeHomePage", null);
}
and when publish the flash publish it with the html and select in the publish with the html the "flash with FS Command" option, then in the html: inside the : (in the code "MainFrame" is the name of the SWF - MainFrame.swf): (most of the code below is written by flash itself when publishing flash with html with the FS command option selected) GOOD LUCK!
<IE:HOMEPAGE ID="oHomePage" />
<SCRIPT LANGUAGE=JavaScript>
<!--
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
// Handle all the the FSCommand messages in a Flash movie
function MainFrame_DoFSCommand(command, args) {
        var MainFrameObj = InternetExplorer ? MainFrame : document.MainFrame;
        if (command == "MakeHomePage")
        {
                homePage();
        }
        
}
function homePage()
{
        oHomePage.setHomePage(parent.location.href);
        event.returnValue = false;
}
// Hook for Internet Explorer
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 &&
navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
        document.write('<SCRIPT LANGUAGE=VBScript\> \n');
        document.write('on error resume next \n');
        document.write('Sub MainFrame_FSCommand(ByVal command, ByVal args)\n');
        document.write(' call MainFrame_DoFSCommand(command, args)\n');
        document.write('end sub\n');
        document.write('</SCRIPT\> \n');
}
//-->
</SCRIPT>

Posted by: mayam | website http://
Detect platform
all = getVersion();
os = substring (all,1,3);

Posted by: Vitalik | website http://www.ego7.com
Dynamic Login Structure within External Txt File
// Title: Dynamic Login Structure within External Txt File
// Level of Expertise: Beginner (Basic knowledge of Actionscript)
// Posted Date: 29th Sept 2002, Sunday
// Author: Mustafa Basgun (mustafa@basgun.com)

// Assume AAA is the username and BBB is the password.
// Create AAA.txt in the same directory with the source file.
// Write password=BBB in AAA.txt and save it.

// Create two textFields and name them as username and pswd.
// Write the following code to the first frame:

username = "";
pswd = "";
stop ();

// Then, write the following to the second frame:

login = username +".txt";
loadVariablesNum (login, 0);

// Create two scenes for the denied and accepted users.
// Let's say scene1 for the denied, scene2 for the accepted users.
// Code for the third frame is:

if (pswd ne password) {
        gotoAndPlay ("scene1", 1);
} else if (pswd eq password) {
        gotoAndPlay ("scene2", 1);
}
stop ();

// Code for the enter button (with a keyboard event) is:

on (release, keyPress "<Enter>") {
        if (username add pswd eq "" add "") {
                gotoAndStop ("scene2", 1);
        } else {
                gotoAndPlay ("scene1", 1);
        }
}

// Now, you can test your swf with AAA as a username and BBB as
// a password, since your swf and AAA.txt are in the same directory.
// You can add more than one txt file, which means new usernames
// with new passwords without touching the source file.

Posted by: Mustafa Basgun | website http://www.basgun.com
Dynamic Newsticker (MX)
System.useCodepage = true;

function newsticker(content, posX, posY, depth, tempo) {
        this.loadVariables(content);
        this.onData = function() {
                createTextField("text", depth, posX, posY, 10, 20);
                text.html = true;
                text.htmlText = news;
                text.selectable = false;
                text.autoSize = "left";
                
                createTextField("text2", depth+1, posX, posY, 10, 20);
                text2.html = true;
                text2.htmlText = news;
                text2.selectable = false;
                text2.autoSize = "left";
                text2._x = newstext._width;
                
                function tick() {
                        text._x -= tempo;
                        text2._x -= tempo;
                        if (text2._x <= posX) {
                                text._x = posX;
                                text2._x = text._width + posX;
                        }
                        updateAfterEvent();
                }
                setInterval(tick, 30);
        };
}

newsticker("ticker.txt", 0, 7, 1, 1);

Posted by: Matthias Kannengiesser | website http://www.flashstar.de

<< Prev 5 | Next 5

Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.