user2620625
user2620625

Reputation:

Using Applescript to Execute a Complicated Keystroke

I'm trying to write an Applescript in Automator that will press the left arrow button while holding down control, option, and command. The code I have so far is:

on run {input, parameters}

    tell application "System Events"
        tell application "Sublime Text 2" to activate
        keystroke "left" using {control down, option down, command down}
    end tell

    return input
end run

However, this is not working. Any suggestions as to how to fix this code? Thanks!

Upvotes: 37

Views: 40452

Answers (2)

Linkmichiel
Linkmichiel

Reputation: 2130

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

Links:

Upvotes: 6

adamh
adamh

Reputation: 3292

When using arrow keys you need to target them via key code.

tell application "Sublime Text 2" to activate

tell application "System Events" 
    key code 123 using {control down, option down, command down}
end tell

ARROW KEY CODES

  • LEFT: (key code 123)
  • RIGHT: key code 124)
  • UP: (key code 126)
  • DOWN: (key code 125)

Upvotes: 66

Related Questions