Type (send keys), not paste, clipboard contents?

I would like to write a script that would be able to paste the contents of the OS X clipboard into the MS-DOS emulator DOSBox. DOSBox does not support Cmd-V or any other OS X-based method of pasting text.

Am I right in thinking that the only way to do this would be to use GUI Scripting and send one character at a time as a keystroke? That seems very impractical (I would have to parse upper-case letters as “x” with shift down, etc.) and not worth the trouble. Is there any other way to accomplish this via AppleScript?

Thanks for any help or advice.

Hello! :slight_smile:

What you want to accomplish, is to paste text into the program, when it is active?

This works for me, when I insert text into an editor inside the terminal, like pico or vim, but I am just typing it, using regular text, ( and the text is typed like if I had typed it in, via the keyboard.

The applescript, is stored and executed from the the script menu in my user folder, so I have access to it from various applications.


tell application "Terminal" to activate

tell application "System Events"
	set my_string to "This is my typist"
	
	keystroke my_string
end tell

Should you wish to send keycodes, for for instance sending special keys, then you will have to download a program like keycodes that shows you the actual commands.

This little example, shows how I would execute several commands, you can have a string that contains return/linefeed by inserting them promptly, or by massaging your text a little.

This is just a hack, illustrating how I would have sendt a key to your dosbox, assuming the dos prompt is there:


tell application "DosBox" to activate

tell application "System Events"
     keystroke "C:"
     delay 0.1
     keycode 36 ” Sending a return to finish off what is typed
     keystroke "dir"
     delay 0.1
     keycode 36
end tell


Rereading your post: I think you want something like this:
it is up to you to try it and test it :slight_smile:


set {oldtids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, linefeed}
set theDosText to every text item of theDosText
set AppleScript's text item delimiters to {return}
set theDosText to theDosText
set AppleScript's text item delimiters to oldtids
tell application "DosBox" to activate

tell application "System Events"
     keystroke theDosText
end tell

I hope this helps, do come back if it doesn’t work for you.

This is exactly what I was looking for! Thank you!

One minor correction: in your second code sample, instead of “keycode” it should say “key code”, as I discovered when I got an error message with your original code.

Now, perhaps you or someone else can help with the next part of this problem?

When I try to paste a long string of text into DOSBox, it seems to overload the keyboard buffer, and many characters get lost. Perhaps one solution would be to split the text into blocks of 64 characters (or fewer), each “keytroked” separately, with a short delay in between.

Is there an easy way to break up a long string this way?

Meanwhile, this code seems to work for short strings, or a series of short strings with carriage-returns between them:

try
	the clipboard as text
on error
	set the clipboard to " "
end try

tell application "DOSBox" to activate
delay 0.1

tell application "System Events"
	keystroke (the clipboard as text)
end tell

Thank you again for pointing me toward the solution!

To answer my own question about the too-fast typing, this script seems to work:

try
	set getClip to the clipboard as text
on error
	set getClip to " "
end try
tell application "DOSBox" to activate
delay 0.1
tell application "System Events"
	repeat with i from 1 to count characters of getClip
		keystroke (character i of getClip)
		delay 0.03
	end repeat
end tell

But one thing is very odd. I am using this to paste text in WordPerfect for DOS running in DOSBox. Whenever the clipboard text contains a numeral (0-9), the numeral does NOT get pasted into DOSBox. Instead, WordPerfect acts as if the Insert/Typeover keystroke was pressed. The “Typeover” indicator appears at the lower left of the WordPerfect window.

This is very strange. I am now trying to figure out a way to prevent it!

Hello!

I’ll give you a little hint for your next request :wink:


tell application "DosBox" to activate
tell application "System Events"
repeat while theDosText ≠ "" 

     set currentText to characters 1 thru 64 of theDosText as text 
     set theDosText to characters 65 thru −1 of theDosText as text

    ” here you do the conversent
    ” here you "type it in "
end repeat
end tell

I hope it helps!

Edit
I have no clue to your last question, try using the keystroke command first, on the same text containg the numbers, and see if it it helps!

EDIT: I saw what was wrong with the previous version of this post.

Before I try your suggestion, here is something more about the problem in which numbers and the period (full stop) do not get typed into DOSBox.

The answer to this seems to be to check whether the character in the clipboard is one that will not get typed, and replace it with “key code ##” where ## is the code number that matches the character. This seems to work.

set charList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."}
set codeList to {29, 18, 19, 20, 21, 23, 22, 26, 28, 25, 47}
try
	set getClip to the clipboard as text
on error
	set getClip to " "
end try
tell application "DOSBox" to activate
delay 0.1
tell application "System Events"
	repeat with i from 1 to count characters of getClip
		if (character i of getClip) is in charList then
			set codeItem to my listPosition(character i of getClip, charList)
			set keyCodeNum to item codeItem of codeList
			key code keyCodeNum
		else
			keystroke (character i of getClip)
		end if
		delay 0.03
	end repeat
end tell

on listPosition(thisItem, thisList)
	repeat with n from 1 to the count of thisList
		if item n of thisList is thisItem then return n
	end repeat
	return 0
end listPosition


Hello!

I am not sure if you are doing anything wrong, I am not trying your code, as I don’t have DosBox anyway.

I think, that given the delays, and the typing, that the DosBox might be at err.

So, please try to keystroke a text, and see how that goes!

As you were posting, I figured out my problem. I was putting the key codes in quotation marks in the list; when I removed the quotation marks, this began to work perfectly!

Thank you again for all your help!

Hello! :wink:

I saw that. I am glad it works for you!

Thank you again! I wish I knew why DOSBox doesn’t like the numbers of the period, but it doesn’t. I noticed that DOSBox also acts strange with some other non-text strings, but that’s something I can work out later. Meanwhile, this script is a very good start. I am grateful for your patient help.