简体   繁体   English

在Mac上使用python打开应用程序

[英]Open application using python on Mac

So I need to be able to open applications through a python script that runs based off of user input. 因此,我需要能够通过根据用户输入运行的python脚本打开应用程序。 (ie if someone types 'Word' the program opens Microsoft Word) (即,如果有人键入“ Word”,程序将打开Microsoft Word)

So what is the proper context using subprocess.Popen() to open an application like Word or MySqlWorkbench or something? 那么使用subprocess.Popen()打开诸如Word或MySqlWorkbench之类的应用程序的正确上下文是什么? any help is appreciated 任何帮助表示赞赏

This should help get you started: 这应该有助于您入门:

import subprocess

def get_app_name(appNamesList, app):
    for appName in appNamesList:
        if app in appName:
            return appName
    return ""


p = subprocess.Popen(["ls",  "/Applications/"], stdout=subprocess.PIPE)
appNames = p.communicate()[0].split("\n")

appNameEntered = raw_input('What app would you like to open?')

appName = get_app_name(appNames, appNameEntered)

if appName != "":
    p = subprocess.Popen(["open", "-n", "/Applications/" + appName], stdout=subprocess.PIPE)
else:
    print 'No app with that name installed'

Example input: Photo B , which should open Photo Booth.app 输入示例: Photo B ,应打开Photo Booth.app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM