Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Correctly keystroke characters in AppleScript
-- While `keystroke text_to_write` also works, it types so fast some characters get mixed up.
-- For example, `keytroke "Example"` might result in `EXample` as if AppleScript wasn’t fast enough to release ⇧ after the `E`.
-- By using a `repeat` loop, we ensure each character gets typed in isolation.
-- However, you might need to decompose certain characters. For example, to type `ã` you might have to tell the command to type `~a`.
-- In those situations it might also be beneficial to add a `delay 0.01` before the `end repeat`.
on type_text(text_to_write)
tell application "System Events"
repeat with c from 1 to count characters of text_to_write
keystroke (character c of text_to_write)
delay 0.01
end repeat
end tell
end type_text
type_text("{{whatever you want}}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.