[Tutor] How to interact with the result of subprocess.call()
Jim
jf_byrnes at comcast.net
Wed Feb 1 16:18:18 EST 2017
On 12/26/2016 04:48 AM, Peter Otten wrote:
> 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")
>
>
Peter,
My apologies for taking so long to get back and thank you for the code.
With the holidays and moving to a new computer/OS I didn't have much
time to work on my little project.
I was able to use your code and send the necessary keystrokes to
manipulate LO. Thanks again.
Regards, Jim
More information about the Tutor
mailing list