Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Help using the "keystroke" command

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)

Posted on Dec 31, 2010 7:30 PM

Reply
7 replies

Dec 31, 2010 8:43 PM in response to nick092

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?

Jan 2, 2011 8:54 AM in response to nick092

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 num tokeystroke 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.

Help using the "keystroke" command

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.