First post, by emendelson
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)
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