7

In Mac OS X, from a shell script application's point of view, how can I get its bundle directory path for use in a shell script? When using the env command, it returns that the app's execution path is /, and no environment variable helps. I'm doing this because I would like to access some resources of the bundle, and because the app is not always installed in /Applications.

Thanks!

moala
  • 231

4 Answers4

6

DIR=$(cd "$(dirname "$0")"; pwd) will give the shell script's directory name. For a shell script app, this will be /path/to/Your shell script application.app/Contents/MacOS.

moala
  • 231
2

I was suggesting to try which, but it doesn't work for Mac applications. You can use find, keeping in mind that Mac applications are bundled, and a shell sees them as directory. Don't forget to add the extension for applications (.app).

avpaderno
  • 1,490
1

If you can get the application's absolute path to the executable, getting the bundle dir is as simple as appending that with /../.. that gets you the main bundle. If you want the resources however, just add to the above /Resources.

klep
  • 11
0

Edit: You want something completely different than my original answer. Keeping it below in case it is useful to anyone. Now I think you are looking for the basename command, although I'm still not certain and I think you'll need to specify more information about what you are doing before we'll be able to give you a decent answer.

I'm not sure I understand the question. Are you trying to locate the application directory? E.g. for Calculator, you want the directory that Calculator.app is in?

jed@jed-osx:~$ ls -la /Applications/Calculator.app/
total 0
drwxr-xr-x   3 root  wheel   102 Jun 22 11:27 .
drwxrwxr-x+ 46 root  admin  1564 Jun 18 23:19 ..
drwxr-xr-x  10 root  wheel   340 Jun 22 11:26 Contents

jed@jed-osx:~$ ls -la /Applications/Calculator.app/Contents/
total 8
drwxr-xr-x  10 root  wheel   340 Jun 22 11:26 .
drwxr-xr-x   3 root  wheel   102 Jun 22 11:27 ..
lrwxr-xr-x   1 root  wheel    28 Apr 17 15:15 CodeResources -> _CodeSignature/CodeResources
-rw-r--r--   1 root  wheel  1201 May  6 10:26 Info.plist
drwxr-xr-x   3 root  wheel   102 Jun 22 11:26 MacOS
-rw-r--r--   1 root  wheel     8 Jul  6  2009 PkgInfo
drwxr-xr-x   4 root  wheel   136 Jul  6  2009 PlugIns
drwxr-xr-x  42 root  wheel  1428 Apr 17 15:28 Resources
drwxr-xr-x   3 root  wheel   102 Jun 22 11:26 _CodeSignature
-rw-r--r--   1 root  wheel   451 May  6 10:27 version.plist

Or are you looking for something completely different?

Jed Daniels
  • 1,284