Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Dream-Cypher/771c3e4364a16eca71cd41fd4ac4a2fe to your computer and use it in GitHub Desktop.
Save Dream-Cypher/771c3e4364a16eca71cd41fd4ac4a2fe 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?
# Extended includes an adjustable delay between keypresses and better handling of numbers
#
# Setup
# Create a service: open Automator, create new service, receive no input,
# use any application, run applescript code below, save.
#
# Activate
# Open application menu from menu bar, go to services, and you will see your service
#
# Note
# "com.automator.runner.xpc is not allowed to send keystrokes" error may be solved by adding
# any application you intend to use the script to your Accessibility menu.
on run
tell application "System Events"
delay 2 # DELAY BEFORE BEGINNING KEYPRESSES IN SECONDS
repeat with char in (the clipboard)
# Converts numbers to ANSI_# characters rather than ANSI_Keypad# characters
# https://apple.stackexchange.com/questions/142986/applescript-keystroke-ignoring-numbers
set cID to id of char
if ((cID ≥ 48) and (cID ≤ 57)) then
key code {item (cID - 47) of {29, 18, 19, 20, 21, 23, 22, 26, 28, 25}}
# convert decimal from keypad to ANSI
else if (cID = 46) then
key code 47
else
keystroke char
end if
delay 0.5 # DELAY BETWEEEN EACH KEYPRESS IN SECONDS
end repeat
end tell
end run
# 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
#
# Setup
# Create a service: open Automator, create new service, receive no input,
# use any application, run applescript code below, save.
#
# Activate
# Open application menu from menu bar, go to services, and you will see your service
#
# Note
# "com.automator.runner.xpc is not allowed to send keystrokes" error may be solved by adding
# any application you intend to use the script to your Accessibility menu.
on run
tell application "System Events"
delay 1
keystroke (the clipboard)
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment