Opening applications by only saying the app name in python

Question:

I just want to open application with only saying the name of the application and not by typing/saying the path or location. Also i want to open application which have their name with spaces like “google chrome”, “snipping tool” etc.
This works with excel, spotify, cmd, notepad and some other programs but does not work with word, powerpoint etc.

elif "start" in text:
    app_name = text.strip("start ")
    app = app_name
    os.system(app)

I just want it to work with two-word applications like google chrome and apps like Word, powerpoint etc.

Asked By: uravgcodddingnoob

||

Answers:

Every executable app running has a one-word name. You can check this by going to the Task manager and tab “details”. There you have all your running programs and their names. For Google Chrome you mentioned it’s “chrome.exe”, for Snipping Tool it’s “snippingtool.exe” etc.

Then it’s easy, you just need to write name of the program and call os.system. Don’t forget the start word there.

app_name = "chrome.exe"
os.system(f"start {app_name}")
Answered By: arbyys

I’m not sure which operating system you are on but for Mac OS this code will work for any application wether the name has a space or not.

app_name = text.strip("start ")
app = app_name
os.system("open -a ""+app+""")

Putting the name in quotations allows the use of spaces in the text and the -a just tells it that you are trying to open an application rather than a file. Hope this helps.

Answered By: user11112512
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.