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 (
[email protected])
// 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 (
[email protected])
// 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 here, 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).