Generating wait until an application opens

Viewed 1801

I am using subprocess to open a file with its default program and then sending keystrokes to it, using the pyautogui library.

My problem is, when you open a file, it takes time to open up in its default program, and of course that time period varies for different files.

So, how can I generate a pause in my script so that the file must open first and keystrokes are sent to it?

Note: sleep() function is not what I am expecting. I need a sort of asynchronisation between the application launched and my script.

from subprocess import Popen
from sys import argv
from pyautogui import press,hotkey

if len(argv)>0:

    word = Popen(['start',argv[1]],shell=True)
    press("f12")
    press("tab")
    press("right")
    for i in range(5): press("down")
    press("enter")
    hotkey("alt","f4")
    word = None

    pdf = Popen(["C:\Program Files\Nitro\Pro 9\NitroPDF.exe",argv[1]])
    hotkey("ctrl","shift","i")
    press("enter")
    hotkey("alt","f4")

I am creating a program to open a Word document and sending keystrokes to it, to save it as pdf. Then I open another PDF editor to export an image from there.

1 Answers
Related