Skip to content

Instantly share code, notes, and snippets.

@7shi
Last active September 2, 2024 04:02
Show Gist options
  • Save 7shi/56eb227b2a84b1e2e1ad9188acf51708 to your computer and use it in GitHub Desktop.
Save 7shi/56eb227b2a84b1e2e1ad9188acf51708 to your computer and use it in GitHub Desktop.
[py] a script that calls Python on the Windows side to operate the clipboard from WSL
#!/bin/sh
python.exe ~/share/winclip.py $@
import sys, pyperclip
texts = []
inputs = []
outputs = None
try:
it = iter(sys.argv[1:])
while arg := next(it, None):
if arg == "-i":
if texts:
raise Exception("extra args")
inputs = list(it)
if not inputs:
raise Exception("`-i` no args")
elif arg == "-o":
if texts:
raise Exception("extra args")
outputs = list(it)
if len(outputs) > 1:
raise Exception("`-o` too many outputs")
else:
texts.append(arg)
if not texts and not inputs and outputs is None:
raise Exception()
except Exception as e:
if e.args:
print("error:", e)
print(f"usage: python {sys.argv[0]} text [...] | -i file [...] | -o [file]")
sys.exit(1)
if texts:
pyperclip.copy(" ".join(texts))
elif inputs:
text = ""
for file in inputs:
with open(file, "r", encoding="utf-8") as f:
text += f.read().replace("\r\n", "\n")
pyperclip.copy(text)
elif outputs:
with open(outputs[0], "wb") as f:
f.write(pyperclip.paste().replace("\r\n", "\n").encode("utf-8"))
else:
print(pyperclip.paste().rstrip())
@7shi
Copy link
Author

7shi commented Sep 2, 2024

Examples:

winclip hello world
winclip -i 1.txt 2.txt 3.txt
winclip -o
winclip -o output.txt

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