8

In macOS, one of the options to open an application via "open" command would be like so:

$ open -a "Google Chrome"
$ open -a "GIMP"

But I realized that the command above works even if the apps weren't in /Applications nor ~/Applications directory.

So, "how can I get the path of this app" using a command line?

I tried to use the find command and search the whole machine, but I thought that there would be an easier way to find it.

The reason is that I need to access the bin file inside the ".app" directory like below:

$ /path/to/GIMP.app/Contents/MacOS/GIMP-bin --help-all

Since the command below doesn't seem to parse the arguments.

$ open -a "GIMP" --args --help-all

  • macOS HighSierra (OSX 10.13.5)
KEINOS
  • 1,050
  • 2
  • 13
  • 32

5 Answers5

6

I think I found another answer.

This script won't launch the app while checking the paths like the Applescript one.

Since it just greps the lsregister's dump result, it might not be as accurate as the Applescript, but so far it seems to work.

Script:

An alternative way to find the application path with bash (without using AppleScript) using lsregister command.

#!/bin/bash

# findApp.sh
# ==========
NAME_APP=$1
PATH_LAUNCHSERVICES="/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"
${PATH_LAUNCHSERVICES} -dump | grep -o "/.*${NAME_APP}.app" | grep -v -E "Caches|TimeMachine|Temporary|/Volumes/${NAME_APP}" | uniq

Usage:

$ ./findApp.sh "Google Chrome"
/Applications/Google Chrome.app
$ ./findApp.sh GIMP
/Volumes/External_HDD/Applications/GIMP/GIMP_v2.8/GIMP.app
KEINOS
  • 1,050
  • 2
  • 13
  • 32
3

I think I found the answer.

Use osascript command to get the application's path associated with the system.

Shell:

$ osascript -e 'POSIX path of (path to application "GIMP")'
/Volumes/External_HDD/Applications/GIMP/GIMP_v2.8/GIMP.app/

or

$ open -a "GIMP"
$ osascript -e 'tell application "System Events" to POSIX path of (file of process "GIMP" as alias)'
/Volumes/External_HDD/Applications/GIMP/GIMP_v2.8/GIMP.app/

Sample script:

Sample shell-script that fetches the GIMP's binary and makes it work as CLI application. (Tested on macOS HighSierra OSX 10.13.6)

#!/bin/bash

PATH_GIMP=`osascript -e 'POSIX path of (path to application "GIMP")'`

echo `$PATH_GIMP/Contents/MacOS/GIMP-bin --help-all`

KEINOS
  • 1,050
  • 2
  • 13
  • 32
1

In my experience, the quick way to find out where an app is installed is to run:

which <app_name>
> /location/where/running/from

In the event the returned file is a symlink, run the following:

readlink /location/where/running/from
> /the/real/source/where/running/from

In the event you are dealing with a python app, the following might also be useful:

pip show <app_name>

> Name: ...
> Version: ...
> ...
> Location: /location/of/the/app

Edmund's Echo
  • 766
  • 8
  • 15
0

The open command on macOS won't provide that information. But since you presumably know you are running on macOS just do the test yourself. That is, test whether /Applications/GIMP.app or ~/Applications/GIMP.app exists. The reason your attempt to use --args --help-all doesn't work is that the app is not run within the context of your shell with its stdout and stderr streams inherited from your shell. It is run as a child of the "init" process. So the --help-all argument doesn't do anything useful since the process exits without displaying any information in your shell (which assumes GIMP isn't already running). The open command is the wrong tool for what you are trying to do.

Kurtis Rader
  • 6,734
  • 13
  • 20
  • `"It is run as a child of the 'init' process."` <- A-ha! I got it. That's why I can't parse the arguments via `open` command to GIMP in my case. I tried to dig directories but a bunch of _GIMP.app_ with a different version I found. Like `/Volumes/EXT_HDD/GIMP_Vxx/GIMP.app`. But I want the current associated one. So there's no command to find out where the default application is placed in? Like some kind of POSIX path env settings? – KEINOS Jul 15 '18 at 01:56
0

It is just easy by checking the process name with ps aux. For instance, when my Atom (a text editor app) is opened, I see that the actual application is launched from /Applications

> ps aux | grep Atom

junji            63388   0.3  1.4 41357168 460532   ??  S     3:46PM   0:02.96 /Applications/Atom.app/Contents/MacOS/Atom

Junji Shimagaki
  • 286
  • 2
  • 9