Last active
September 2, 2024 04:02
-
-
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
This file contains 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
#!/bin/sh | |
python.exe ~/share/winclip.py $@ |
This file contains 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
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples: