Skip to content

Instantly share code, notes, and snippets.

@sscotth
Last active October 9, 2024 11:51
Show Gist options
  • Save sscotth/310db98e7c4ec74e21819806dc527e97 to your computer and use it in GitHub Desktop.
Save sscotth/310db98e7c4ec74e21819806dc527e97 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?
# * Includes an initial delay to allow you to change active windows
# * Adds small delay between keypresses for slower responding windows like SSH sessions
# * Better handling of numbers
# * VMWare bug fix
#
# Setup
# Apple Shortcuts app
#
# MAKE SURE YOUR CAPSLOCK IS OFF
on run
tell application "System Events"
delay 2 # DELAY BEFORE BEGINNING KEYPRESSES IN SECONDS
repeat with char in (the clipboard)
set cID to id of char
if ((cID ≥ 48) and (cID ≤ 57)) then
# Converts numbers to ANSI_# characters rather than ANSI_Keypad# characters
# https://apple.stackexchange.com/a/227940
key code {item (cID - 47) of {29, 18, 19, 20, 21, 23, 22, 26, 28, 25}}
else if (cID = 46) then
# Fix VMware Fusion period bug
# https://apple.stackexchange.com/a/331574
key code 47
else
keystroke char
end if
delay 0.5 # DELAY BETWEEEN EACH KEYPRESS IN SECONDS
end repeat
end tell
end run
on run
tell application "System Events"
keystroke (the clipboard)
end tell
end run
@jlaundry
Copy link

@SHREYAS-KS-CS104 if you load https://w3c.github.io/uievents/tools/key-event-viewer.html inside parallels, and then use this to type equals, what keyDown events do you see?

@knkwelsh
Copy link

knkwelsh commented Jul 8, 2023

Any chance one of you could develop a Mac app to paste clipboard as keystrokes entry? I use this daily where text windows don’t allow pasting and it’s critical for my daily work but lately I’ve been getting error messages at times trying in Chrome and Safari. If this stops working entirely it would be a severe blow to my daily work so I would gladly pay for a reliable and easily installed app.

please help!!

@StormYudi
Copy link

Any chance one of you could develop a Mac app to paste clipboard as keystrokes entry? I use this daily where text windows don’t allow pasting and it’s critical for my daily work but lately I’ve been getting error messages at times trying in Chrome and Safari. If this stops working entirely it would be a severe blow to my daily work so I would gladly pay for a reliable and easily installed app.

please help!!

I found a Golang script, with a little modification:

package main

import (
	"github.com/GuoFlight/gkeybd"
	"golang.design/x/clipboard"
)

func main() {
	err := clipboard.Init()
	if err != nil {
		panic(err)
	}
	content := string(clipboard.Read(clipboard.FmtText))
	err = gkeybd.TypeStr(content)
	if err != nil {
		panic(err)
	}
}

and after compiling it, with iCanHazShortcut, I can finally bind the paste key to the keyboard, it works like a charm.
image

Warning: need to change #import <Foundation/Foundation.h> in vendor/github.com/micmonay/keybd_event/keybd_darwin.go to #import <Cocoa/Cocoa.h> in macOS when compiling.

@jkoppenol
Copy link

@PeetMcK @markmcjr

Not sure if the regular Scripts Menu Privacy settings were different in Big Sur. I can however confirm that the following works on my 12.3.1 box (as an FYI, it is supervised in MDM, just in case that has an affect on this that I'm unaware of).

1. ...
...
5. In Script Editor Preferences ensure ...
6. In SystemPreferences -> Security & Privacy -> Accessibility add ...
...

I think think this way of embedding into MacOS menu has more merit than via "Quick Action" method. In the latter case you have to give any app that initiates the Quick Action permissions to control the computer, which is inconvenient and security threat. In the way mentioned by PeetKcK you only have to perform one time a few actions for only one Apple Supplied MacOS App.

Thanks @sscotth for creating this script and others for making it more user-friendly !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment