If you are writing a JScript and need a way to copy some text to the clipboard, you can use the following definition:

// Uses Internet Explorer to copy text to the clipboard.
function copyToClipboard(text)
{
	var objIE;
	try
	{
		objIE = new ActiveXObject("InternetExplorer.Application");
		objIE.Navigate("about:blank");
		objIE.document.parentWindow.clipboardData.setData("Text", text);
	}
	catch(e)
	{
		WScript.echo("The following error occurred while trying to "
		  + "access the system's clipboard using Internet Explorer:\n"
		 + e.description);
		return false;
	}
	objIE.Quit();
	return true;
}

Here is an example code block that calls this function:

msg = "This message has just been copied to the system's clipboard.";
copyToClipboard(msg);
WScript.echo(msg);

This function relies on the existence of Internet Explorer on the PC. Of course, this function is only guaranteed to work for stand-alone JS files or WSF files.

Categories: BlogJScript

Leave a Reply

Your email address will not be published. Required fields are marked *