pythonfileoperating-system

How do you run a python script when an app runs


I was wondering if it is possible to run a certain python code I wrote when an app runs. I am on a Windows machine I don't want to keep some script running all the time on my computer that checks if a certain app has been opened, so that's why i have this question. I want my program to run when an app opens, do its job and be done with it.

I have tried designing some code that runs a loop that checks if an application has been opened. If that happens, I would "break" out of the loop do some things and reenter the loop when the application closes. The problem is that i don't want some program taking my memory with its loop all the times.


Solution

  • I'm not sure if this completely answers your question but you could try the pywinauto or appscript python packages to interact with your OS to reach the app launch hooks.

    Alternatively you could set your script up as a cron job that checks at a set interval and exits to avoid the resource drain you are concerned with a continuous loop.