Skip to content

Instantly share code, notes, and snippets.

@russau
Created Jun 3, 2022
Embed
What would you like to do?
from pynput import keyboard
from pynput.keyboard import Key
import subprocess
file1 = open('clipboard.txt', 'r')
lines = file1.readlines()
controller = keyboard.Controller()
def copyAndPasta(command):
# i think pasting looks nicer than controller.type(command)
subprocess.run("pbcopy", universal_newlines=True, input=command)
controller.release(Key.cmd)
controller.release(Key.shift)
controller.press(Key.cmd)
controller.press('v')
controller.release('v')
controller.release(Key.cmd)
def on_activate_i():
print("activating")
command=lines[0]
# print anything commented out
if lines[0].startswith("#"):
while lines[0].startswith("#"):
print(lines[0])
lines.pop(0)
return
# a hack for multiline
if "\"\"\"" in lines[0]:
command = command.replace("\"\"\"", "")
while True:
lines.pop(0)
command += lines[0]
if "\"\"\"" in lines[0]:
command = command.replace("\"\"\"", "")
break
lines.pop(0)
copyAndPasta(command)
return
# if there's a line continuation merge everything into one string
while lines[0].endswith(" \\\n"):
lines.pop(0)
command += lines[0]
lines.pop(0)
copyAndPasta(command)
with keyboard.GlobalHotKeys({
'<cmd>+<shift>+1': on_activate_i}) as h:
h.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment