7 Replies Latest reply: Jan 3, 2011 5:20 PM by ToothpickTed
nick092 Level 1 Level 1 (0 points)
I understand that you can make applescript type by using the command: keystroke "text". However, it seems that whenever numbers are included in what I am trying to type, it doesn't work (for example: keystroke "12980" doesn't type 12980). This may have something to do with the fact that I'm using a laptop keyboard... I don't know. Does anyone have a fix for this?

Note: I already found out that "key code 21" types "4", etc, but this doesn't help me since I am trying to type a large list of values with numbers in them.

Macbook, Mac OS X (10.5.8)
  • Pierre L. Level 5 Level 5 (4,115 points)
    Maybe I'm missing the point, but the following script will type "12980" in the frontmost TextEdit document:


    *tell application "TextEdit"*
       activate
    *   if not (document 1 exists) then make new document*
    *   tell application "System Events"*
    *      keystroke "12980"*
    *   end tell*
    *end tell*


    And I'm using a MacBook Pro laptop.
  • nick092 Level 1 Level 1 (0 points)
    Nope for some reason that doesn't work on my laptop (I have a MacBook Intel Core 2 Duo running 10.5.8)
  • nick092 Level 1 Level 1 (0 points)
    I believe it may be because AppleScript is trying to press the numbers on my keyboard that are beneath the U, I, O, J, K, L, etc keys, but I could be wrong.
  • red_menace Level 6 Level 6 (14,855 points)
    I see this same behavior on my MacBook Pro using my regular account, but it works correctly in a new Guest account. I haven't tried restarting without various scripting additions or background helper applications, but it does look like one of those is interfering.

    I have a couple of Satimage scripting additions installed and a couple of background daemons from LittleSnitch and TinkerTool running - are you running any third party items like these?
  • red_menace Level 6 Level 6 (14,855 points)
    Well, I had a chance to restart my machine, and the keystroke command is running correctly again in my regular account. I've rerun about everything I can think of to try and duplicate the problem, but it had been running for several days so there is probably some combination I've missed.
  • Andrew99 Level 4 Level 4 (1,300 points)
    nick092 wrote:
    This may have something to do with the fact that I'm using a laptop keyboard... I believe it may be because AppleScript is trying to press the numbers on my keyboard that are beneath the U, I, O, J, K, L, etc...


    FWIW, I'm using an iMac extended keyboard and I've never been able to keystroke numbers either. I don't know why.

    As a workaround, you might have a look at the script below. It iterates through the digits of a number you want to keystroke (numtokeystroke) and, based on the value of the digit itself, matches each to its relative position in code_list. For example, 9 in 12980 is matched to the ninth item of code_list (not the third, as its position in the number itself would indicate). Each digit in turn becomes a variable (codelistitem) which is used in the *key code* command later in the script.

    The script can be hard coded to accept a preset variable; however, in this case, *set numtokeystroke to "12980"* is commented out in favor of *text returned of (display dialog...)*. This was done to more fully test the variables and to provide an option to enter numbers on the fly.


    set code_list to {"18", "19", "20", "21", "23", "22", "26", "28", "25", "29"}
    set numtokeystroke to text returned of (display dialog "Enter a number below..." default answer "")
    -- set numtokeystroke to "12980"
    tell application "TextEdit"
    activate
    if not (exists document 1) then
    make new document
    end if
    tell application "System Events"
    repeat with i from 1 to the count of numtokeystroke
    set codelistitem to item i of numtokeystroke
    try
    key code (item codelistitem of code_list) as integer
    on error
    key code (item 10 of code_list) as integer
    end try
    end repeat
    end tell
    end tell



    The script worked for me in Mac OS 10.4.11; others' results may vary. Good luck.
  • ToothpickTed Level 1 Level 1 (0 points)
    You also may want to consider doing a SW Update... There might be some weirdness going on with the configs prior OS 10.6.x, that some of the other people aren't seeing.

    -T