Set each digit of a number to variable from a list?

For a script I am writing that pastes the OS X clipboard into the DOSBox open-source DOS emulator, I have written a routine (thanks to the expert help provided by D J Bazzie Wazzie) that finds the PC ASCII number of a character, and then, in DOSBox, types the three-digit code in the way that DOS expects to receive it - with the Alt/Option key held down, and then the three digits typed on the keyboard numberpad, like this:


tell application "System Events"
key down option
key code firstNum
key code secondNum
key code thirdNum
key up option
end tell

I’ve written a hopelessly complicated routine for getting the firstNum, secondNum, and thirdNum variables from the three-digit numeral. It works, but I’m certain there’s a more efficient way to do it. Here is the relevant code. At the top, I’ve created a list with the ten codes I need for keypad1 through keypad0:


set upperASCII to 130 -- or whatever other number is needed
set kpCodes to {83, 84, 85, 86, 87, 88, 89, 91, 92, 82}
-- Keypad key codes:
-- KP1=83, KP2=84, KP3=85, KP4=86, KP5=87
-- KP6=88, KP7=89, KP8=91, KP9=92, KP0=82
set firstNum to character 1 of (upperASCII as string) as number
			if firstNum is 0 then set firstNum to 10
			set secondNum to character 2 of (upperASCII as string) as number
			if secondNum is 0 then set secondNum to 10
			set thirdNum to character 3 of (upperASCII as string) as number
			if thirdNum is 0 then set thirdNum to 10
			set numList to {item firstNum of kpCodes, item secondNum of kpCodes, item thirdNum of kpCodes} --list contents will be 83, 85, 82
			key down option
			repeat with theItem in numList
				key code theItem
			end repeat
			key up option

Is there a more elegant way to get these three numbers into that list"?

Thanks for any help!


set upperASCII to 130 -- or whatever other number is needed
set kpCodes to {82, 83, 84, 85, 86, 87, 88, 89, 91, 92}
-- Keypad key codes:
-- KP1=83, KP2=84, KP3=85, KP4=86, KP5=87
-- KP6=88, KP7=89, KP8=91, KP9=92, KP0=82
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

If the 90 wasn’t missing, you could have dispensed with the kpCodes list too. :frowning:

Spectacularly elegant!! Thank you!!