简体   繁体   中英

How to check if a application has been recently opened using python?

I would like to know when a application was recently opened by the user, for instance, 'notepad.exe' if opened then the script should be able to detect that it was opened recently without iterating over all the processes again and again.

def checkIfApplicationOpened():
    for proc in psutil.process_iter():
        try:
            pinfo = proc.as_dict(attrs=['name'])
            if(pinfo['name'] == 'notepad.exe'):
                return True
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            return False
    return False    

while True:
    print(checkIfApplicationOpened())

Currently, this is my approach but it is very inefficient. Can someone please provide a better way of doing this? Thanks in advance.

how about using pywinauto library.

like this

import pywinauto

pywinauto.timings.wait_until_passes(20, 0.5, lambda: pywinauto.findwindows.find_windows(best_match=u'notepad.exe')[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM