How to get the path of app in its launch script

finderscript

I made an app by following these steps

  1. Make folder Foo.app
  2. Make Subfolder Contents
  3. Put Info.Plist in Contents with

    ...
    <key>CFBundleExecutable</key>
    <string>Foo</string>
    ...
    
  4. Make MacOS/Foo as an executable shell script

Foo looks like

#!/bin/sh
echo $0 > /Users/me/temp/foo1
pwd > /Users/me/temp/foo2
export > /Users/me/temp/foo3

Clicking on the app in the finder then checking the results. The results are, foo1 is empty, foo2 is just / and foo3 looks like

export Apple_PubSub_Socket_Render="/private/tmp/com.apple.launchd.FgQwJ5EVCp/Render"
export HOME="/Users/me"
export LOGNAME="me"
export OLDPWD
export PATH="/usr/bin:/bin:/usr/sbin:/sbin"
export PWD="/"
export SHELL="/bin/zsh"
export SHLVL="1"
export SSH_AUTH_SOCK="/private/tmp/com.apple.launchd.4hbrl5p8fp/Listeners"
export TMPDIR="/var/folders/7h/wyw4jhd933z5jmqzsdx6r0sr0000gn/T/"
export USER="me"
export __CF_USER_TEXT_ENCODING="0x1F5:0x0:0x0"

No references to /Applications/Foo.app anywhere

Now do I get the path to my app in the launch script?

Best Answer

You can use this code:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $DIR >  /Users/me/temp/outLog.txt

It'll give you path to the MacOS folder of you app.

Here's great stackoverflow answer about this.