Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlchan/24595bfe9f509af4f8947948b6d9a59a to your computer and use it in GitHub Desktop.
Save carlchan/24595bfe9f509af4f8947948b6d9a59a 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 highjack paste
#
# How?
# 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
on run
tell application "System Events"
delay 1
keystroke (the clipboard)
beep
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 highjack paste
#
# How?
# 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
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}}
else
keystroke char
end if
delay 0.5 # DELAY BETWEEEN EACH KEYPRESS IN SECONDS
end repeat
beep
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment