Skip to content

Instantly share code, notes, and snippets.

@matthewpenkala
Created February 16, 2024 09:21
Show Gist options
  • Save matthewpenkala/8ca4ad5303ac5757a00d915fd893e90a to your computer and use it in GitHub Desktop.
Save matthewpenkala/8ca4ad5303ac5757a00d915fd893e90a to your computer and use it in GitHub Desktop.
Simulate individual keystrokes from the clipboard's contents using AppleScript. Comprehensively iterate through all characters, handling numbers & special characters (e.g., `.`), with pauses to mimic human typing. Ideal for environments where direct paste is otherwise unavailable.
on run {input, parameters}
-- Setup: Delay before initiating the sequence of key presses.
tell application "System Events"
delay 1 -- Delay (in seconds) allows for any preparations before the script runs.
-- Iterates over each character in the clipboard's content.
repeat with char in (the clipboard)
set cID to id of char -- Retrieves the ASCII value of the character for conditional handling.
-- Checks if the character is a number (0-9) and maps it to the corresponding key code.
if cID ≥ 48 and cID ≤ 57 then
key code {item (cID - 47) of {29, 18, 19, 20, 21, 23, 22, 26, 28, 25}}
-- Special character handling using their ASCII values for accurate key code mapping.
else if cID = 46 then -- For period (.), press key code 47.
key code 47
else if cID = 96 then -- For grave accent (`), press key code 50.
key code 50
else if cID = 126 then -- For tilde (~), press key code 50 with shift modifier.
key code 50 using {shift down}
else if cID = 124 then -- For pipe (|), press key code 42 with shift modifier.
key code 42 using {shift down}
else
-- Directly types the character for any other non-special characters.
keystroke char
end if
delay 0.05 -- Introduces a brief pause between each keystroke to simulate natural typing speed.
end repeat
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment