[Tutor] How to interact with the result of subprocess.call()
Peter Otten
__peter__ at web.de
Mon Dec 26 05:48:58 EST 2016
Jim Byrnes wrote:
> Is there a way to terminate subprocess and still keep LO open so
> pykeyboard can send it keystrokes from the script?
In theory you can open Libre Office from another thread, wait a moment and
then send it keystrokes from the main thread. I managed to do this with the
script below.
However, the procedure is very brittle; while experimenting I managed to
"press" the control key without releasing it afterwards. The interval from
doing it to realizing what was going on to "fixing" it (reboot) was an
interesting experience...
from contextlib import contextmanager
import subprocess
import threading
import time
import pykeyboard
kb = pykeyboard.PyKeyboard()
@contextmanager
def press_key(key, kb=kb):
kb.press_key(key)
try:
yield
time.sleep(1)
finally:
kb.release_key(key)
def open_it(filename):
subprocess.call(["libreoffice", filename])
print("exiting libreoffice thread")
LONG_ENOUGH = 15 # seconds
# enter text into an existing odt file
filename = "demo.odt"
text = "hello and goodbye"
opener = threading.Thread(target=open_it, args=(filename,))
opener.start()
time.sleep(LONG_ENOUGH) # for libreoffice to start and open the file
kb.type_string(text)
with press_key(kb.control_key):
kb.tap_key("s")
with press_key(kb.alt_key):
kb.tap_key(kb.function_keys[4])
print("exiting main thread")
More information about the Tutor
mailing list