Skip to content

Instantly share code, notes, and snippets.

@holg
Last active October 7, 2018 00:48
Show Gist options
  • Save holg/fb42ee65de8ec78c358681ec547dc087 to your computer and use it in GitHub Desktop.
Save holg/fb42ee65de8ec78c358681ec547dc087 to your computer and use it in GitHub Desktop.
send key codes to the Terminal, easily changed
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
global debug_level
global delay_float
global appref
global callappref
global keycode
global delay_float
global my_using
on run
set keycode to 40
set test to 0
set delay_float to 0.5
set debug_level to 10
set my_app_str to "Terminal"
set appref to application my_app_str
set callappref to application "System Events"
---set key to ""
--
--keystroke uses a string of the pressed key as argument
--```
--osascript -e 'tell application "System Events" to keystroke "K" using command down'
--```
--sends CMD-K
--```
--osascript -e 'tell application "System Events" to key code 126 using command down'
--```
-- sends CMD-Cursor Up Code key (lang independent: 123 cursor left , 124 cursor right, 125 cursor down, 126 cursor up)
--here a nice reference i found:
--https://eastmanreference.com/complete-list-of-applescript-key-codes
--
send_keycodes({keycode, keycode, keycode, keycode, keycode}, appref, delay_float)
send_key_code(40, appref, {command down})
end run
on send_key_code(keycode, appref, my_using)
tell application "System Events"
if the debug_level > 9 then tell appref to activate
if the debug_level > 3 then delay delay_float
key code keycode using my_using
end tell
end send_key_code
on send_keystroke(stroke_string, appref, my_using)
tell application "System Events"
if the debug_level > 9 then tell appref to activate
if the debug_level > 3 then delay delay_float
keystroke stroke_string using my_using
end tell
end send_keystroke
on send_keycodes(keycodes, appref, delay_float)
repeat with keycode in keycodes
send_key_code(keycode, appref, {})
end repeat
end send_keycodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment