Search code examples
pythonautomation

Open installed apps on Windows intelligently


I am coding a voice assistant to automate my pc which is running Windows 11 and I want to open apps using voice commands, I don't want to hard code every installed app's .exe path. Is there any way to get a dictionary of the app's name and their .exe path. I am able to get currently running apps and close them using this:

def close_app(app_name):
    running_apps=psutil.process_iter(['pid','name'])
    found=False
    for app in running_apps:
        sys_app=app.info.get('name').split('.')[0].lower()

        if sys_app in app_name.split() or app_name in sys_app:
            pid=app.info.get('pid')
            
            try:
                app_pid = psutil.Process(pid)
                app_pid.terminate()
                found=True
            except: pass
            
        else: pass
    if not found:
        print(app_name + " is not running")
    else:
        print('Closed ' + app_name)

Solution

  • Possibly using both wmic and use either which or gmc to grab the path and build the dict? Following is a very basic code, not tested completely.

    import subprocess
    import shutil
    
    Data = subprocess.check_output(['wmic', 'product', 'get', 'name'])
    a = str(Data)
    appsDict = {}
    
    x = (a.replace("b\\'Name","").split("\\r\\r\\n"))
    
    for i in range(len(x) - 1):
        appName = x[i+1].rstrip()
        appPath = shutil.which(appName)
        appsDict.update({appName: appPath})
    
    print(appsDict)