VOGONS


First post, by emendelson

User metadata
Rank Oldbie
Rank
Oldbie

EDIT: Newly rewritten script, transfers extended ASCII characters into DOSBox.

Dominus suggested this idea, and here it is. The following AppleScript is a script that will paste the OS X clipboard into DOSBox. This improved version includes some extremely elegant methods suggested by the experts at MacScripter.net, who deserve the credit for the good stuff.

How to use it: Copy the code into the OS X AppleScript Editor, save it as an application with a name like "PasteClipboardToDOSBox" or any other name you like. Optionally assign a hotkey that launches it in Butler, Quicksilver, or some other keyboard macro program. Or simply run it when DOSBox is running, in order to paste the OS X clipboard into DOSBox. Suggestions and improvements welcome.

-- Pastes the OS X clipboard into DOSBox
-- By Edward Mendelson 3 July 2012

-- try to determine DOS codepage for DOSBox according to Mac locale:
set the codePage to "850" -- 850 for Europe
try
set macLocale to do shell script "defaults read -g AppleLocale"
if macLocale is "en_US" or "en_CA" then
set the codePage to "437" -- 437 for North America
end if
end try
-- if you get the wrong results, then set correct codepage by uncommenting next line
-- set the codePage to "437" -- either "437" or "850"

tell application "System Events" to set tNames to (name of processes)
set tClue to "DOSBox" -- looking for DOSBox
set appRunning to {}
repeat with aName in tNames
if contents of aName contains tClue then set end of appRunning to contents of aName
end repeat
-- error number -128
if appRunning is {} then
tell me to activate
display dialog "DOSBox not running." buttons {"OK"} with title "Paste into DOSBox" giving up after 5
error number -128
end if

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, 39, 27, 24}

set shiftList to {"“", "”"}
set shiftCodeList to {39, 39}

set kpCodes to {82, 83, 84, 85, 86, 87, 88, 89, 91, 92}
-- Keypad key codes: KP0=82, KP1=83, KP2=84, KP3=85
-- KP4=86, KP5=87, KP6=88, KP7=89, KP8=91, KP9=92

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 if (character i of getClip) is in shiftList then
set codeItem to my listPosition(character i of getClip, shiftList)
set keyCodeNum to item codeItem of shiftCodeList
key code keyCodeNum using {shift down}
else if (ASCII number of (character i of getClip)) is greater than 127 then
set upperASCII to my getCharValueForDOS(character i of getClip, codePage)
-- next lines by Nigel Garvey at MacScripter.net
tell application "System Events"
key down option
key code (item (upperASCII div 100 + 1) of kpCodes)
Show last 23 lines
				key code (item (upperASCII mod 100 div 10 + 1) of kpCodes)
key code (item (upperASCII mod 10 + 1) of kpCodes)
key up option
end tell
else
keystroke (character i of getClip)
end if
delay 0.01
tell application "DOSBox" to activate
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

on getCharValueForDOS(char, cPage)
-- routine by DJ Bazzie Wazzie at MacScripter.net
return (do shell script "/bin/echo -n " & quoted form of character 1 of char & " | iconv -f UTF8 -t CP" & cPage & " | od -A n -t u1") as integer
end getCharValueForDOS
Last edited by emendelson on 2013-03-05, 21:59. Edited 4 times in total.

Reply 2 of 11, by mr_bigmouth_502

User metadata
Rank Oldbie
Rank
Oldbie

Now we just need something like this for Windows...

Hmm, maybe someone will put copy/paste functionality into DosBox 0.75? (inb4 "not essential for gaming")

Reply 4 of 11, by emendelson

User metadata
Rank Oldbie
Rank
Oldbie
Dominus wrote:

Great script. Needed to be more elaborate than I thought, though. Congrats.

Credit is due to the helpful people at MacScripter.net who suggested using the keystroke and key codes command and some other details.

Now a question to expert users of DOSBox under OS X: Is there any way to emulate the system used under DOS and Windows to enter upper-ASCII characters like é by pressing Alt-130 to enter the keystroke? I'm trying to find something that would work without requiring a special keyboard mapping file so that this script can use it. Simply typing the number keys on the top row of the keyboard with Alt/Option pressed down doesn't do it, and most modern Macs don't have number pads. Thanks for any advice.

Last edited by emendelson on 2012-07-02, 12:01. Edited 1 time in total.

Reply 5 of 11, by emendelson

User metadata
Rank Oldbie
Rank
Oldbie
mr_bigmouth_502 wrote:

Now we just need something like this for Windows...

As Dominus says, I've put a basic (ASCII-characters-only) version in my kludgy printing-clipboard-exchange system for Windows, in another thread. If I can figure out a way to make it work for upper ASCII characters (é, ç, ü, etc.), I'll break it out into a separate utility that you can use to paste the Windows clipboard into DOSBox.

Reply 6 of 11, by emendelson

User metadata
Rank Oldbie
Rank
Oldbie

I've revised the script in the first post; better detection of DOSBox; now supports (as examples) two upper ASCII characters (é and ç); anyone who might be interested can add further upper ASCII characters.

Now to figure out how to do the same thing with upper ASCII characters in AutoIt for Windows...

Reply 7 of 11, by emendelson

User metadata
Rank Oldbie
Rank
Oldbie

Bulletin: Have figured out (with the help of the experts at MacScripter.net) how to get upper-ASCII characters from the OS X clipboard into DOSBox. Am now hoping to make my method more elegant, and will post a revised script when it's ready.

Reply 9 of 11, by emendelson

User metadata
Rank Oldbie
Rank
Oldbie
Dominus wrote:

MacScripter are awesome, they helped me tremendously in the past :)

And thanks to the experts at MacScripter, this script now works fairly well, I think. I'm posting this message to bump the thread because I've now posted a version that solves all the problems I noticed before, I hope. Thank you again for suggesting this project. I learned a lot from it, and I hope someone finds it useful.

Reply 10 of 11, by collector

User metadata
Rank l33t
Rank
l33t
Dominus wrote:

MacScripter are awesome, they helped me tremendously in the past 😀

Ready to give the Sierra scripts another try?