Shell script Mac OS X App : get its path

6

3

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

Posted 2010-06-24T09:07:50.853

Reputation: 221

1Does the shell script belong to the app resources? Anyway, this question belongs to Stackoverflow. – mouviciel – 2010-06-24T11:20:15.287

In my case, the shell script IS the App's executable. – moala – 2010-06-24T13:09:14.000

I've always wanted to know if/how I could do this. – Jeremy Banks – 2010-08-14T04:49:47.387

Answers

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

Posted 2010-06-24T09:07:50.853

Reputation: 221

1

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

kiamlaluno

Posted 2010-06-24T09:07:50.853

Reputation: 1 390

The problem with find is that it is VERY long, and may find another copy of the program... The problem with which is that it does not find any .app bundle... – moala – 2010-07-07T13:35:26.793

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

Posted 2010-06-24T09:07:50.853

Reputation: 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

Posted 2010-06-24T09:07:50.853

Reputation: 1 166

Completely different. Sorry for not being clear enough. It's from the app's executable point of view that I would like to get the path of the bundle... – moala – 2010-06-24T09:30:39.947

1AH. Ala basename in a bash script? It sounds like this question would be better placed over on Stack Overflow (it has probably already been asked and answered). – Jed Daniels – 2010-06-24T09:34:33.637

Mmmh not working with basename... I found a different solution: DIR=$(cd "$(dirname "$0")"; pwd) is it better or equivalent? – moala – 2010-06-24T13:07:44.983