In macOS, applications can be launched from the command line with open -a < application name>
.
I would like to know if there is a command that returns the executable within
the application bundle, when the application is not running, I'm hoping for something like this:
awesomeCommand <application name>
/Applications/***/MacOS/Content/application_executable
Best Answer
There is no such command, but I've written a shell script that does exactly that for any application bundle in the file system of your startup disk.
It uses several command line tools included in macOS and leverages Apple's documentation on Launch Services, Application Bundles and Core Foundation Keys. The script does the following:
/Applications
for the application bundle./Applications
, queries the Launch Services database, only considering application bundles on the startup disk.Info.plist
and looks for a key namedCFBundleExecutable
that stores the executable (see below in the background information section for more on that). Otherwise, print an error message an exit.The script understands both an application name with or without its extension, checks arguments, displays a usage message and a short description if run without arguments and returns an exit status with an error message if necessay.
Since the script searches
/Applications
and may do a time-consuming query to Launch Services, it isn't super fast (from ~ 1 sec to several seconds), but it was 100% accurate in my tests.This is the script, with explanations on what is done in every step:
You can save the file (for example as
pbex
for "print bundle executable") and make it executable as follows:If you prefer a function, use this instead:
Just add it to
.bashrc
, source it:to use the function.
If you need to execute it as a (rather long) "one-liner", use (replace
<app name>
with the name of the application without the .app extension):(You write in a comment that the "command has to be launched from within a QProcess"). You could do the following:
See https://stackoverflow.com/a/20901985 for more information.)
Examples:
BACKGROUND INFORMATION
The structure of a macOS bundle app bundle is well-known and documented by Apple.
According to The Structure of a macOS Application Bundle, the basic structure of a Mac app is:
where the
MacOS
folder "contains the application’s standalone executable code. Typically, this directory contains only one binary file with your application’s main entry point and statically linked code. However, you may put other standalone executables (such as command-line tools) in this directory as well."The
Info.plist
file is required for the Finder to recognize an application bundle as such. This information property list filecontains XML property-list data that identifies the configuration of your bundle. The key that is of interest for us isCFBundleExecutable
, which stores "the name of the main executable file. This is the code that is executed when the user launches your application."