Use AppleScript for a Virtual Numpad

Jul 22, '10 07:30:00AM

Contributed by: nathanator11

The keyboards on Mac notebooks, and now desktops, too, have no number pad. It used to be that notebook keyboards had a numlock key, and you could use the letter keys on the right hand side of the keyboard as a number pad, but that feature seems to have vanished. It can, however, be brought back with AppleScript. [crarko adds: You can get it by pressing and holding the 'fn' key, which is also cumbersome.]

The code given below will let you type as if this numpad existed. It will convert the letters into the corresponding numbers, and copy the result to the clipboard. When you need a numpad, just switch to this program, and then back to your original, and paste. Also, you can type normally by typing " " (double-space). Use that to switch to text mode, and again to switch out. For example: "jkl jkl jkl" will produce "123jkl123". If you use triple-spaces, one space will be left ("123 jkl 123"). So, you could type this: " My favorite number is jkluio789 ." and get "My favorite number is 123456789." Also, keys that do not represent any number will remain unchanged. Here's what each key does [crarko adds: Seeing the picture included in the download below will make this clearer]:

U=4 I=5 O=6 P=+
J=1 K=2 L=3
M=0 ,=00
7, 8, and 9 stay the same.
Normally, numpad keys aren't staggered, so this takes a little getting used to. But, I still find it a lot faster than the row of numbers at the top.

set toQuit to false
repeat while toQuit is false
 set dd to display dialog "Enter text..." with title "Numpad" buttons {"Quit", "Continue"} default button 2 default answer ""
 set ddText to text returned of dd as string
 set ddBtn to button returned of dd as string
 if ddBtn is "Quit" then set toQuit to true
 if ddBtn is "Continue" then
  set oldDel to AppleScript's text item delimiters as string
  set AppleScript's text item delimiters to "" as string
  set chars to items of ddText as list
  set AppleScript's text item delimiters to oldDel as string
  set nums to chars
  set sp to false
  set spSet to false
  set spUnSet to false
  considering case
   set valNum to 1 as integer
   repeat with val in nums
    if spSet is true then
     set spSet to false
     set valNum to valNum + 1
    else
     if spUnSet is true then
      set spUnSet to false
      set valNum to valNum + 1
     else
      if sp is true then
       if (val as string) is " " then
        if (item (valNum + 1) of nums) is " " then
         set sp to false
         set spUnSet to true
        end if
       end if
       set valNum to valNum + 1
      else
       if (val as string) is "u" then set item valNum of nums to "4"
       if (val as string) is "i" then set item valNum of nums to "5"
       if (val as string) is "o" then set item valNum of nums to "6"
       if (val as string) is "p" then set item valNum of nums to "+"
       if (val as string) is "j" then set item valNum of nums to "1"
       if (val as string) is "k" then set item valNum of nums to "2"
       if (val as string) is "l" then set item valNum of nums to "3"
       if (val as string) is "m" then set item valNum of nums to "0"
       if (val as string) is "," then set item valNum of nums to "00"
       if (val as string) is " " then
        if (item (valNum + 1) of nums) is " " then
         set sp to true
         set spSet to true
        end if
       end if
       set valNum to valNum + 1
      end if
     end if
    end if
   end repeat
  end considering
  set numsText to nums as string
  if numsText contains " " then
   set oldDel to AppleScript's text item delimiters as string
   set AppleScript's text item delimiters to " " as string
   set num to 1
   set sps to (count text items of numsText) as integer
   set numsText2 to "" as string
   repeat with num from 1 to sps
    set numsText2 to (numsText2 & text item num of numsText) as string
    set num to num + 1
   end repeat
   set AppleScript's text item delimiters to oldDel as string
  else
   set numsText2 to numsText
  end if
  set the clipboard to numsText2
 end if
end repeat


[crarko adds: When trying the examples, you need to actually type the text into the entry box; copy and paste didn't always work. It takes a few minutes to get used to the spaces to change modes, but it does work. The source code, a compiled version of the app, and a picture of the virtual keypad are mirrored here.]

Comments (10)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2010060515230735