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

时间:2015-07-27 15:31:58

标签: python macos subprocess

所以我需要能够通过基于用户输入运行的python脚本打开应用程序。 (即如果有人输入“Word”,程序会打开Microsoft Word)

那么使用subprocess.Popen()打开像Word或MySqlWorkbench等应用程序的适当上下文是什么?任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

这应该有助于您入门:

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'

示例输入:Photo B,应该打开Photo Booth.app