Last active
November 19, 2021 10:27
-
-
Save BelkaDev/d449eb0b62671457398ee9e325398122 to your computer and use it in GitHub Desktop.
A script to paste text/ run commands into active window
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/bash | |
############ | |
# @Description Paste stdin to current window | |
# @Author https://github.com/BelkaDev | |
# @Requires xclip,xdotool | |
# @Usage ${0} <string> | |
# @Usage <cmd> | ${0} | |
# @Example ${0} "hi" | |
# @Example curl -s "cht.sh/$(xclip -o -sel)?T?Q" | ${0} | |
############ | |
CONTEXT=$(ps -o stat= -p $PPID) | |
if [ -p /dev/stdin ]; then | |
while IFS= read line; do | |
STDOUT="$STDOUT${line}\n" | |
done | |
elif [ "$#" -gt 0 ]; then | |
STDOUT="$*" | |
fi | |
[[ -z $STDOUT ]] && exit 1 | |
TMP="$(xclip -o -sel clip)" | |
printf "$STDOUT" | xclip -selection c | |
# Safe paste for shells | |
if [[ $CONTEXT == "Ss" || $CONTEXT == "Ss+" ]]; then | |
printf "$STDOUT" | |
exit 0 | |
fi | |
#Append to EOF | |
#xdotool key --clearmodifiers 0xffe3+0xff57 | |
xdotool key --clearmodifiers 0xffe1+0xff9e | |
#Restore previous clip | |
echo "$TMP" | xclip -selection c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment