Welcome, guest | Sign In | My Account | Store | Cart

This script allows one to launch or kill several applications at once. It works on MS Windows.

Python, 36 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from subprocess import Popen
from win32com.client import GetObject
from string import replace

def isRunning(app):
    WMI = GetObject('winmgmts:')
    app = replace(app,"\\","\\\\")
    return(len(WMI.ExecQuery('select * from Win32_Process where ExecutablePath="%s"'%app))!=0)

def execAll(appList):
    for app in appList:
        if not isRunning(app):
            Popen(app)

def killAll(appList):
    WMI = GetObject('winmgmts:')
    for app in appList:
        app = replace(app,"\\","\\\\")
        processes = WMI.ExecQuery('select * from Win32_Process where ExecutablePath="%s"'%app)
        for process in processes:
            try:
                process.Terminate()
            except TypeError:
                raise

def main():
    myApps = ["C:\\Program Files\\Pidgin\\pidgin.exe",
        "C:\\Program Files\\Opera 10\\opera.exe",
        "C:\\Program Files\\Vuze\\Azureus.exe",
        "C:\\Program Files\\Winamp\\winamp.exe"]
    unwantedApps = ["C:\\WINDOWS\\system32\\notepad.exe"]
    execAll(myApps)
    killAll(unwantedApps)
    
if __name__ == "__main__":
    main()

I usually need to launch several application when I decide to do something with my computer. For instance, when I want to use the web, I launch not only my web browser but also my instant messenger, my audio player, my downloader and so on.
Launching each application one by one is tedious. As well, I may forget to launch an application, say my IM.
This script allows one to launch several apps at once. It launches them only if they re not already running. It also shutdown apps that one does want (I want to shutdown my IM when I watch a movie).
It uses Windows Management Interface to know whether an app is running or shutting it down.
Beware of the shutdown function: it does not allow the target app to shut down gracefully and might lead to data loss, e.g. if one kills notepad while there is unsaved text in it, the script won't give the chance to save this text. If someone knows a way to fix this, please tell. Also, I didn't find a way to prevent process.Terminate() from raising a TypeError exception.