Automate: Typing Clipboard

More and more people involved with computers todays, as result more restrictions provided in IT systems and it becames normal practice unfortunately. Initially application were developed to automate and improve productivity, but last years it has opposite direction. Many programs now doesn't allow to do simple thing like right click, select or copy content to clipboard, etc. One of these "human invention" is to perevent paste clipboard operations. Scripts listed below simulates human keyboard input. As example it works with standard "Notepad" or "TextEdit" applications, but any other real one can be used - "Citrix Viewer", "Safari" etc. During execution it activates target application and then types text from clipboard in similar to human way.


MacOS Automator can be used to run AppleScript listed below. Sample activates "TextEdit" application, wait some short time before start, after this it types clipboard text as human does.


on run {input, parameters}
	tell application "TextEdit" to activate
	local txtCb
	set txtCb to the clipboard
	delay 0.7
	repeat with i from 1 to length of txtCb by 1
		delay (random number from 0.07 to 0.2)
		tell application "System Events"
			keystroke text i through i of txtCb
		end tell
	end repeat
	return input
end run


Windows sample is HTA application with button "Paste". Clicking on the button will switch focus to wordpad application and type text from clipboard there. Code is demo one and doesn't cover all special character, but it works in 90% cases (convertChar function can be extended to improve quality).


 <TITLE>Typing Text</TITLE>
	APPLICATIONNAME="Typing Text Sample"


var shell = new ActiveXObject("");

function convertChar(ch)
	if( ch==null ) return null;
		case "\r": return null;
		case "\n": return "{ENTER}";
	return "{"+ch+"}";

function sendKeys(s, i, tmin, tmax)
	if( s==null ) return;
	if( i>=s.length) return;
	var ch = s.substring(i, i+1);
	ch = convertChar(ch);
	if( ch!=null )
		shell.sendKeys(ch, true);
		function() {
			sendKeys(s, i+1, tmin, tmax);
		}, (Math.random()*(tmax-tmin+1)+tmin)

function typeClipboardText()
	var s = window.clipboardData.getData("Text");

	setTimeout(function () {
		shell.AppActivate("Document - wordpad");
		setTimeout(function() {
			sendKeys(s, 0, 50, 200);
		}, 350);
	}, 350);

function init()
	self.resizeTo(120, 90);

<BODY onLoad="init()" style="owerflow:hidden;" scroll="no">
	<BUTTON onClick="typeClipboardText()" TYPE="button">Paste</BUTTON>

Revision:1, Last Modified: 06/03/2013 18:37, Copyright © by Vadim Melnik 2013. Visit my Homepage.