Если нужно зацепиться за другой элемент, вбей
powershell gps | where {$_.MainWindowTitle } | select *
import asyncio
class timer_executer:
def __init__(self, appname, timeout, do):
self.appname = appname
self.timeout = timeout
self.do = do
async def get_active_apps(self):
cmd = 'powershell "gps | where {$_.MainWindowTitle } | select Path'
cmd_execution_result = await asyncio.create_subprocess_shell(cmd, shell=True, stdout=asyncio.subprocess.PIPE)
responce_raw, _ = await cmd_execution_result.communicate()
responce_splited = responce_raw.decode().split("\n")[3:]
return [responce_line.strip().lower() for responce_line in responce_splited if responce_line.strip()]
async def loop_await(self):
time_on_active = 0
while True:
active_apps = await self.get_active_apps()
if self.appname.lower() in active_apps:
time_on_active += 1
if time_on_active >= self.timeout:
self.do()
break
else:
time_on_active = 0
await asyncio.sleep(1)
async def start(self):
await self.loop_await()
def do_something():
print("Привет!")
async def main():
timer = timer_executer("c:\\program files\\windowsapps\\microsoft.windowsnotepad_11.2409.9.0_x64__8wekyb3d8bbwe\\notepad\\notepad.exe", 10, do_something)
await timer.start()
asyncio.run(main())