julienXX / terminal-notifier

Send User Notifications on macOS from the command-line.
Other
6.38k stars 330 forks source link

Launcher Script #149

Closed An00bIS47 closed 8 years ago

An00bIS47 commented 9 years ago

This is not an issue, but i wanted to share this small launcher script.

Save this script for example as /usr/local/bin/terminal-notifier. It will look for the app bundle and call the actual command line tool.

#!/bin/sh

#  terminal-notifier launcher script
#  Terminal Notifier

# Array of possible application names.
appnames=("terminal-notifier")    

#
# Calls lsregister -dump and parses the output for "/Firefox.app", etc.
# Returns the very first result found.
#
function get_osx_appdir()
{
    # OSX Array of possible lsregister command locations
    # I'm only aware of this one currently
    lsregs=("/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister")

    for i in "${lsregs[@]}"; do
        for j in ${appnames[@]}; do
            if [ -f $i ]; then
                # Some logic to parse the output from lsregister
                appdir=$($i -dump |grep -E "/$j.app$" |cut -d'/' -f2- |head -1)
                appdir="/$appdir"
                return 0
            fi
        done
    done
    return 1
}

#echo "Searching for App..."
appdir=""

get_osx_appdir

#echo "Found application here: $appdir"

cd "$appdir/Contents/MacOS"
./terminal-notifier "$@"

You can add the following to the Build Phases -> Run Script for your Target and it will create the launcher automatically everytime you build the project

echo "Creating launcher script..."
cat << 'EOF' > /usr/local/bin/terminal-notifier
#!/bin/sh

#  terminal-notifier launcher script
#  Terminal Notifier

# Array of possible application names.
appnames=("terminal-notifier")    

#
# Calls lsregister -dump and parses the output for "/Firefox.app", etc.
# Returns the very first result found.
#
function get_osx_appdir()
{
    # OSX Array of possible lsregister command locations
    # I'm only aware of this one currently
    lsregs=("/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister")

    for i in "${lsregs[@]}"; do
        for j in ${appnames[@]}; do
            if [ -f $i ]; then
                # Some logic to parse the output from lsregister
                appdir=$($i -dump |grep -E "/$j.app$" |cut -d'/' -f2- |head -1)
                appdir="/$appdir"
                return 0
            fi
        done
    done
    return 1
}

#echo "Searching for App..."
appdir=""

get_osx_appdir

#echo "Found application here: $appdir"

cd "$appdir/Contents/MacOS"
./terminal-notifier "$@"

EOF

chmod +x /usr/local/bin/terminal-notifier
echo "Done!"
julienXX commented 9 years ago

Thanks :thumbsup: