如何在macOS上以编程方式按名称打开应用程序? [英] How to programmatically open an application by name on macOS?

查看:53
本文介绍了如何在macOS上以编程方式按名称打开应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个跨平台的Python应用程序,它充当 DOSBox 的前端.它需要使用许多命令行参数来调用DOSBox可执行文件.我不想对DOSBox的特定路径进行硬编码,因为它可能取决于用户在何处安装了它.

I'm writing a cross-platform Python application that acts as a frontend for DOSBox. It needs to call the DOSBox executable with a number of command line arguments. I don't want to hardcode a specific path to DOSBox because it might depend on where the user has installed it.

在Linux上,我可以简单地做到:

On Linux, I can simply do:

import subprocess
subprocess.run(['dosbox'] + args)

但是,在macOS上,我当前使用以下代码:

On macOS, however, I currently use the following code:

import subprocess
subprocess.run(['/Applications/dosbox.app/Contents/MacOS/DOSBox'] + args)

哪个似乎非常具体,我什至不确定它是否有效,因为我没有要测试的Mac.

Which seems awfully specific and I'm not even sure whether it works, since I don't have a mac to test on.

在macOS上按名称打开应用程序 的正确方法是什么?

What is the correct way to open an application by name on macOS?

(注意:对于Windows,我也曾问过这个同级问题.)

(NB: I have also asked this sibling question for Windows.)

推荐答案

我不知道 DOSBox 或在我的Mac上不需要它,但是总的来说,当您在macOS上安装应用程序时其中的属性列表" 文件或 plist "info.plist" .在此处,开发人员应该放置一个名为 CFBundleIdentifier 捆绑包标识符" 键.这在所有应用程序中必须是唯一的,因此对于 DOSBox ,它应该类似于:

I don't know DOSBox or want it on my Mac, but in general, when you install an application on macOS it has a "property list" file, or plist or "info.plist" in it. In there, the developer is supposed to put a "bundle identifier" key called CFBundleIdentifier. This must be unique across all applications, so for DOSBox it should be something like:

<key>CFBundleIdentifier</key>
<string>com.dosboxinc.dosbox</string>

让您的用户之一找到它,然后无论安装位置在哪里,都可以使用捆绑包标识符将其打开:

Get one of your users to find that, then you can use the bundle identifier to open it like this regardless of installation location:

open -b BUNDLEIDENTIFIER --args arg1 arg2 arg3

其中 arg1 arg2 arg3 传递给 DOSBox .

可以通过在 Terminal (终端)中运行它来获取捆绑包标识符:

You may be able to get the bundle identifier by running this in Terminal:

osascript -e 'id of app "DOSBox"'

但是请注意,如果此命令有效,则意味着我已经正确猜到了应用程序名称"DOSBox" ,这意味着您可以将应用程序名称与 open ,而不是像这样的包标识符:

Note, however, that if this command works, it means I have correctly guessed the app name "DOSBox", which means that you could just use the app name with open, rather than the bundle identifier like this:

open -a DOSBox --args arg1 arg2 arg3

这篇关于如何在macOS上以编程方式按名称打开应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆