Skip to content

Instantly share code, notes, and snippets.

@sscotth
Last active September 8, 2024 16:04
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
@markmcjr
Copy link

markmcjr commented Jan 17, 2021

This works great for me - for any noobs out there like me running BigSur, here are some tips that may save you some time.

  1. Launch Automator (yup it comes with BigSur and already installed, no need to install it)
  2. New Document
  3. Click Quick Action
  4. Change "Workflow Receives" to "No Input"
  5. On the left scroll down and double click "Run AppleScript"
  6. Erase the Template Text and Past the Code above "I used the basic workflow script" (you dont need both scripts)
  7. Save the Script - I called mine Clip2Keystrokes
  8. NOTE: You will need to add the Automator and ANY app you plan to run this script on to the Accessibility Permissions.
  9. Copy your text to the clipboard
  10. Then switch to the App you want to paste in (in my case it was a Teamviewer session)
  11. Click the App name in the top left (next to the apple icon), you will see a Services Menu option.
  12. Mouse over the Services menu and you will see your script name, "Clip2Keystrokes" in my case.
  13. Be sure the cursor is already where you need it to be as clicking on the script will cause it to start typing.

That was it.
Thanks @sscotth for creating this script!

@sscotth
Copy link
Author

sscotth commented Jan 18, 2021

Thanks @sscotth for creating this script!

👍

@loudestnoise
Copy link

loudestnoise Try adding the any application you intend to use it on in the System Preferences > Security & Privacy > Privacy > Accessibility menu.

Amazing, yep that did the trick. Thank you @sscotth, and for GitHub for sending me a notification, which caused me to revisit this.

@knkwelsh
Copy link

knkwelsh commented May 26, 2021

WOW!! Hey I'm brand new here, know nothing about AppleScript and haven't written computer code since I learned Basic in the mid 1980's. I needed to figure out how to paste into a website message window on Chrome with keystrokes so I searched and found your script and followed Scott's code and Markmcjr's directions using BigSur above and it works!!! You all are awesome and thank you so much!!

@sscotth
Copy link
Author

sscotth commented Jun 2, 2021

Welcome. Enjoy!

@craignied
Copy link

@markmcjr every once in a while someone goes the extra mile and does a walkthrough that just makes life so much easier. You just did that. Thank you from the interwebs, and it's still working for Monterey. You've given me hours of my life back!

@PeetMcK
Copy link

PeetMcK commented May 6, 2022

@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. Open Script Editor
  2. Create "New Document"
  3. Paste the above script and adjust to taste
  4. Save as a "Script" to ~/Library/Scripts (I name mine "Paste as Keystrokes.scpt")
  5. In Script Editor Preferences ensure "Show Script in menu bar" is enabled
  6. In SystemPreferences -> Security & Privacy -> Accessibility add "/System/Library/CoreServices/Script Menu.app"
  7. With some text in your clipboard and your cursor placed in a text field, select your script from the Script menu.
  8. Watch with awe as your clipboard is typed as keystrokes
  9. Be happy and be kind

And to echo @markmcjr, @sscotth I'm eternally grateful for this script. It's saved countless hours of frustration.

@xorwin
Copy link

xorwin commented May 7, 2022

@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. Open Script Editor
  2. Create "New Document"
  3. Paste the above script and adjust to taste
  4. Save as a "Script" to ~/Library/Scripts (I name mine "Paste as Keystrokes.scpt")
  5. In Script Editor Preferences ensure "Show Script in menu bar" is enabled
  6. In SystemPreferences -> Security & Privacy -> Privacy add "/System/Library/CoreServices/Script Menu.app"
  7. With some text in your clipboard and your cursor placed in a text field, select your script from the Script menu.
  8. Watch with awe as your clipboard is typed as keystrokes
  9. Be happy and be kind

And to echo @markmcjr, @sscotth I'm eternally grateful for this script. It's saved countless hours of frustration.

Hello, your method worked. Thank you. Is there a way to make it a shortcut? I do not have access to the menu bar in some fullscreen apps (Gforce NOW).

@sscotth
Copy link
Author

sscotth commented May 9, 2022

Is there a way to make it a shortcut? I do not have access to the menu bar in some fullscreen apps (Gforce NOW).

@xorwin Not sure if you mean "Keyboard" shortcut, but I'm not sure if you can with the Script Editor method. You can with the original Services method.

However, if you have the the new Apple Shortcuts app, it works the best. It gives you the services menu option, global menu bar (Script), and the ability to use a keyboard shortcut. You don't even need to dive into the Keyboard system preferences to setup the shortcut. Bonus points for the person who integrates their hue lights during the action.

image

Allow both Shortcuts.app and siriactionsd in System Preferences > Security & Privacy > Privacy > Accessibility and it should work globally

image

@Dream-Cypher
Copy link

Dream-Cypher commented May 15, 2022

I found I needed to add an additional Conversion for the ".", as vmware fusion would not accept URLs that had a period/decimal.

                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}}
                # convert decimal from keypad to ANSI
	        else if (cID = 46) then
		        key code 47		
                 else
			# Removed incorrect line key code cID ##
                         keystroke char

		end if

@sscotth
Copy link
Author

sscotth commented May 16, 2022

@Dream-Cypher Are you sure key code cID is what you meant? cID is the ascii character code, and key code uses a different scheme.

Some old references to the bug, nothing official posted from VMware

https://apple.stackexchange.com/questions/322008/cannot-print-period-from-simple-applescript-to-vmware-fusion-virtual-machi

https://apple.stackexchange.com/questions/142986/applescript-keystroke-ignoring-numbers

@Dream-Cypher
Copy link

Are you sure key code cID is what you meant? cID is the ascii character code, and key code uses a different scheme.

Nope... good catch! I entered the wrong line when I posted the comment. I did have "keystroke char" in my local copy.

Thanks,
Dave

@swaaap-nil
Copy link

TYSM!! worked for me!

@jlaundry
Copy link

With VMWare Fusion, I also had issues with grave, tilde, and pipe (was being received as keyCodes 193, Shift+193, and Shift+255, respectively):

else if (cID = 96) then
    # Fix grave (`)
    key code 50
else if (cID = 126) then
    # Fix tilde (~)
    key code 50 using {shift down}
else if (cID = 124) then
    # Fix pipe (|)
    key code 42 using {shift down}

@sscotth
Copy link
Author

sscotth commented Dec 20, 2022

👍

@SHREYAS-KS-CS104
Copy link

The equals '=' key doesn't work on Parallels desktop. Any idea on how to fix it ?

@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