Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jlaundry/5aa527cbc35094bb48634326e1a0c822 to your computer and use it in GitHub Desktop.
Save jlaundry/5aa527cbc35094bb48634326e1a0c822 to your computer and use it in GitHub Desktop.
Paste as keystrokes (macOS)
# Why?
# To paste text into windows that normally don't allow it or have access to the clipboard.
# Examples: Virtual machines that do not yet have tools installed, websites that hijack paste
#
# Extended vs Simple?
# * Includes an initial delay to allow you to change active windows
# * Adds small delay between keypresses for slower responding windows like SSH sessions
# * Better handling of numbers
# * VMWare bug fix
#
# Setup
# Apple Shortcuts app
#
# MAKE SURE YOUR CAPSLOCK IS OFF
on run {input, parameters}
tell application "System Events"
delay 1 # DELAY BEFORE BEGINNING KEYPRESSES IN SECONDS
repeat with char in (the clipboard)
set cID to id of char
#display dialog char & ":" & cID giving up after 2
if ((cID ≥ 48) and (cID ≤ 57)) then
# Converts numbers to ANSI_# characters rather than ANSI_Keypad# characters
# https://apple.stackexchange.com/a/227940
key code {item (cID - 47) of {29, 18, 19, 20, 21, 23, 22, 26, 28, 25}}
else if (cID = 46) then
# Fix VMware Fusion period bug
# https://apple.stackexchange.com/a/331574
key code 47
else if (cID = 96) then
# Fix grave (`)
key code 50
else if (cID = 126) then
# Fix tilde (~)
key code 50 using {shift down}
else if (cID = 124) then
# Fix pipe (|)
key code 42 using {shift down}
else
keystroke char
end if
delay 0.2 # DELAY BETWEEEN EACH KEYPRESS IN SECONDS
end repeat
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment