Skip to content

Instantly share code, notes, and snippets.

@TrevCan
Forked from ethack/TypeClipboard.md
Last active October 22, 2023 11:47
Show Gist options
  • Save TrevCan/8af5e1f8ff15f76d8f1b0ea71ecaa64e to your computer and use it in GitHub Desktop.
Save TrevCan/8af5e1f8ff15f76d8f1b0ea71ecaa64e to your computer and use it in GitHub Desktop.
type-clip: scripts that simulate typing the clipboard contents. Useful when pasting is not allowed.
#!/bin/bash
# File Name: type-clip
# Description:
# type-clip: scripts that simulate typing the clipboard contents. Useful when pasting is not allowed.
# Original Author: @ethack (https://gist.github.com/ethack)
# Edited by: @Trevcan (https://gist.github.com/trevcan)
# License: The MIT License
# source:
# https://gist.github.com/ethack/110f7f46272447828352768e6cd1c4cb (TypeClipboard.md, 2020)
# last edited by @Trevcan < heror0484 [at] proton mail *dot* com > (July, 2021)
# https://gist.github.com/TrevCan/8af5e1f8ff15f76d8f1b0ea71ecaa64e (TypeClipboard.md & type-clip, 2021)
VERSION_TYPE_CLIP=0.15 #no of revisions
LICENSE="Copyright © 2021 @ethack, Hector Canizales @TrevCan\n
\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
case "$1" in
"-p" )
passmenu --type;
;;
"--h" | "--help" | "-help" | "-h" )
echo -e "type-clip:\tScript that simulates typing on the clipboard contents. Useful when pasting is not allowed."
echo ;
echo -e "Version $VERSION_TYPE_CLIP\n\tForked and last edited by \n\t\t@Trevcan (github/TrevCan)\n\tOriginal Author: @ethack (github/ethac)"
echo "Licensed under The MIT License. To view, use: -license"
echo ;
echo Usage:
echo -e "-p\tUse the standard unix password manager *pass* to type out your passwords to the current window.\n\tSame as using: passmenu --type"
echo -e "-s\tSelect a window with your mouse and type out the contents of your clipboard."
echo ;
echo -e "without arguments:\tType out the contents of your keyboard on the current window."
echo -e "-license\tView License"
;;
"-s" )
xclip -selection clipboard -out |\
tr \\n \\r | \
xdotool selectwindow windowfocus \
type --clearmodifiers --delay 25 \
--window %@ --file -
;;
"-license" )
echo -e $LICENSE
;;
* )
xclip -selection clipboard -out |\
tr \\n \\r | \
xdotool \
type --clearmodifiers --delay 25 \
--file -
esac
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
#NoTrayIcon ; Hide the tray icon
^+v::Send {Raw}%Clipboard%

It "types" the contents of the clipboard.

Why can't you just paste the contents you ask? Sometimes pasting just doesn't work.

  • One example is in system password fields on OSX.
  • Sometimes you're working in a VM and the clipboard isn't shared.
  • Other times you're working via Remote Desktop and again, the clipboard doesn't work in password boxes such as the system login prompts.
  • Connected via RDP and clipboard sharing is disabled and so is mounting of local drives. If the system doesn't have internet access there's no easy way to get things like payloads or Powershell scripts onto it... until now.

Windows

The Windows version is written in AutoHotKey and easily compiles to an executable. It's a single line script that maps Ctrl-Shift-V to type the clipboard.

^+v::Send {Raw}%Clipboard%

Linux

The following should work on Linux, provided you have xdotool and xclip installed. This version lets you select the window you want to send the keystrokes to.

xclip -selection clipboard -out | tr \\n \\r | xdotool selectwindow windowfocus type --clearmodifiers --delay 25 --window %@ --file -

Explanation of the flags used:

  • xclip -selection clipboard gets the contents of the clipboard.
  • -out writes the text to stdout.
  • tr \\n \\r replaces newlines with carriage returns to ensure they don't get missed in some applications.
  • selectwindow allows you to pick a window to send text to. This means you don't have to have the window active when you run the command.
  • windowfocus focuses the selected window. Most apps I tried would ignore keystroke events if they weren't in focus.
  • --clearmodifiers makes sure that no modifier keys are pressed before typing.
  • --delay 25 was the best balance between speed and not missing keystrokes in the applications I was using. This shouldn't be noticable for short text, but makes a difference with longer text.
  • --window $@ means that keystrokes will only be sent to that window. If you focus a different window the typed keystrokes won't suddenly be sent to your new window.
  • --file - reads from stdin.

The following is a version that just "pastes" immediately to the active window.

xclip -selection clipboard -out | tr \\n \\r | xdotool type --clearmodifiers --delay 25 --file -

I saved this to a script and then mapped the script to a hotkey using Gnome's custom keyboard shortcuts.

OSX

The Mac version is writtern in AppleScript.

tell application "System Events" to keystroke the clipboard as text

The equivalent one-liner from the command line would be:

osascript -e 'tell application "System Events" to keystroke the clipboard as text'

To bind this to a keyboard shortcut you have several options. Sticking with builtin OSX utilities you can follow this guide.

Otherwise, you can use a third party program that lets you set custom hotkeys such as: BetterTouchTool, Keyboard Maestro, or Hammerspoon

Credits:

  • @Indigo744 for the suggestion to use {Raw} in the Windows version
  • @L3vi47h4N for the Linux version
  • @brabster for the --clearmodifiers suggestion in the Linux version
  • @ethack for creating the original gist file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment