2

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)
Rohith Nambiar
  • 2,957
  • 4
  • 17
  • 37

3 Answers3

1

Under Windows PowerShell there is a Get-Command utility. Finding Windows executables using Get-Command is described nicely in this issue. Essentially it's just running

Get-Command *

Now you need to use this from python to get the results of command as a variable. This can be done by

import subprocess  
data = subprocess.check_output(['Get-Command', '*'])

Probably this is not the best, and not a complete answer, but maybe it's a useful idea.

  • Thanks for the idea, my plan is to first extract the name and paths of the installed programs, add them to a dict and then find the similarity between the name of the app and the query inputted by the user and execute the corresponding path, the problem is figuring out how to get the name and paths into a dict – Rohith Nambiar Mar 18 '22 at 14:51
1

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)
gnaanaa
  • 323
  • 4
  • 18
0

This can be accomplished via the following code:

    import os

    def searchfiles(extension, folder):
        with open(extension[1:] + "file.txt", "w", encoding="utf-8") as filewrite:
            for r, d, f in os.walk(folder):
                for file in f:
                    if file.endswith(extension):
                        filewrite.write(f"{r + file}\n")

    searchfiles('.exe', 'H:\\')

Inspired from: https://pythonprogramming.altervista.org/find-all-the-files-on-your-computer/

Dharman
  • 30,962
  • 25
  • 85
  • 135
Danny
  • 77
  • 1
  • 7
  • This will need to be rerun every time there is a new .exe (or some old one is deleted/moved etc.) and will take a shitload of time if you have a lot of files. – matszwecja Mar 02 '22 at 09:40
  • I missed the for loop back there, and I believe this is way efficient and faster than windows search – Danny Mar 02 '22 at 09:45
  • I'm pretty sure iterating through _every single file_ on the hard drive is NOT in any way more efficient nor faster than windows search. – matszwecja Mar 02 '22 at 09:52
  • Well, his question was only upto getting the app's .exe path and in case a file has been moved, the app that they are making would raise an exception and only at that point of time can we make the python file omit the path or tell if it is missing, so that code sequence ought to come later in the code – Danny Mar 02 '22 at 09:55
  • This takes a lot of time and I also need it as a dictionary or anything where I can loop through it to get the app's name and its respective path – Rohith Nambiar Mar 03 '22 at 05:24
  • About the time part, again, I am not sure of a better answer than this. To get it in a dictionary, just tweak it a little with your knowledge and dump all the paths into a dictionary – Danny Mar 03 '22 at 08:57
  • Hmm ok, I will try – Rohith Nambiar Mar 07 '22 at 03:25