Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically open an application by name on macOS?

Tags:

python

macos

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.

On Linux, I can simply do:

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

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

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

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

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

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

like image 371
Jaap Joris Vens Avatar asked Dec 31 '20 05:12

Jaap Joris Vens


1 Answers

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

where arg1, arg2 and arg3 get passed on to DOSBox.


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

osascript -e 'id of app "DOSBox"'

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
like image 117
Mark Setchell Avatar answered Nov 07 '22 22:11

Mark Setchell