6

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.)

Jaap Joris Vens
  • 3,382
  • 2
  • 26
  • 42

2 Answers2

5

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
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

Most applications on a Mac are kept in /Applications, so /Applications/dosbox.app/Contents/MacOS/DOSBox seems like the correct filepath to the executable most of the time to me. I did try installing DOSBox on my Mac and the application is usually called DOSBox.app, however.

It is true that a Mac user could theoretically put the application anywhere, which might mean using os.walk would be the way to find it. I don't know of a way to lookup an application in macOS except by recursive searching, which is how I believe the system itself does it when it needs to. What I would try is using os.path.isfile to check for if the executable exists in the usual location, then searching for it in the directory ~. Directories outside the current user's directory are unlikely to contain this application.

hexagon
  • 101
  • 6
  • I don't believe an exhaustive search of the filesystem is the correct answer. There must be a way to search for installed programs only. – Jaap Joris Vens Dec 31 '20 at 11:51