Created
April 25, 2026 23:13
-
-
Save wolph/a113b0addc774eb5f8b77a09e5d0a38d to your computer and use it in GitHub Desktop.
clip-type — autotype the current clipboard into the next window you focus.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/osascript | |
| (* | |
| clip-type — type the current clipboard into the next window you focus. | |
| Why this exists: | |
| Some apps refuse paste (Cmd+V) — KVM/VNC/RDP consoles, certain | |
| password fields, locked-down VMs. Synthesizing keystrokes works | |
| where paste doesn't, because the OS sees real key events. | |
| How it works: | |
| 1. If a numeric argument is passed (seconds), wait that long, then | |
| type. This is the "I'll click the target window myself" mode. | |
| 2. With no argument, record which app is frontmost right now | |
| (typically the terminal you launched this from), then poll | |
| every 100ms until a *different* app becomes frontmost. As soon | |
| as you click into another application, the wait ends and the | |
| clipboard is typed into it. | |
| 3. Either way, the final step uses System Events `keystroke` to | |
| emit the clipboard contents one character at a time, as if | |
| typed on the keyboard. | |
| Usage: | |
| clip-type # wait for app switch, then type | |
| clip-type 5 # wait 5 seconds, then type | |
| Requirements: | |
| - Accessibility permission for whichever app launches the script | |
| (Terminal / iTerm / Script Editor). Granted in: | |
| System Settings → Privacy & Security → Accessibility. | |
| - Clipboard must contain text. Images / files will error out at | |
| `the clipboard as text`. | |
| Caveats: | |
| - Switching between windows or tabs of the *same* app does NOT | |
| end the wait — the frontmost-app name is unchanged. You must | |
| switch to a different application. | |
| - `keystroke` is slow for large clipboards and can drop characters | |
| over laggy remote sessions. | |
| - Non-ASCII / dead-key characters depend on the active keyboard | |
| layout and may produce unexpected output. | |
| *) | |
| -- Returns the name of the application that currently owns the menu bar | |
| -- (the "frontmost" app). Used to detect when the user switches windows. | |
| on frontApp() | |
| tell application "System Events" to return name of first application process whose frontmost is true | |
| end frontApp | |
| -- Entry point. `argv` is the list of command-line arguments passed to | |
| -- `osascript`. AppleScript receives them as a list of strings. | |
| on run argv | |
| if (count of argv) > 0 then | |
| -- Explicit-delay mode: argv[1] is seconds to wait before typing. | |
| -- Coerced to `real` so fractional values like "1.5" work. | |
| delay (item 1 of argv as real) | |
| else | |
| -- App-switch mode: snapshot the current frontmost app, then | |
| -- spin until it changes. The 0.1s sleep keeps CPU usage near | |
| -- zero while staying responsive to the user's window switch. | |
| set startApp to frontApp() | |
| repeat while frontApp() is startApp | |
| delay 0.1 | |
| end repeat | |
| end if | |
| -- Type the clipboard. `the clipboard as text` coerces whatever is | |
| -- on the pasteboard to a string (or raises an error if it can't). | |
| -- `keystroke` synthesizes real key events into the frontmost app. | |
| tell application "System Events" | |
| keystroke (the clipboard as text) | |
| end tell | |
| end run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment