4

I am creating an Applescript that show a dialog where the user should be able to enter either any common character (such as a) or any special key (such as page down or ⌘). How can the latter keys/key codes be entered into a regular text field in an Applescript dialog?

  • Isn’t this already the case? Just copy and paste or use text replacement. – JBis Jun 16 '18 at 11:12
  • @Josh I want the script to know that page down was entered and then act upon that. The visual feedback doesn't really matter. – d-b Jun 16 '18 at 11:27
  • @d-b Words or the symbol? – JBis Jun 16 '18 at 15:36
  • @Josh The ASCII code. My script is gonna send page down (or enter or A or End) somewhere. That is, repeat the key I just pressed. – d-b Jun 16 '18 at 15:51
  • 1
    @d-b Unfortunately I don’t have time to write the code write now (I’m not mobile) but later I will. AppleScript has limited keys that it can use and your script is going to be a giant “if” conglomerate. If you want the full ASCII table your going to have hundreds of if statements (I think 256 but maybe more). There’s not default way to do what you want. – JBis Jun 16 '18 at 15:57
3
+50

As promised:

display dialog "Enter your ASCII keycode" default answer ""
set a to the text returned of the result
tell application "System Events" to keystroke (ASCII character a)

Heres a list of all the possible key codes.

Common ones:

You can use any ASCII code, for the arrow keys this will be:

tell application "System Events" to keystroke (ASCII character 31) --down arrow

tell application "System Events" to keystroke (ASCII character 30) --up arrow

tell application "System Events" to keystroke (ASCII character 29) --right arrow

tell application "System Events" to keystroke (ASCII character 28) --left arrow

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .