Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Get full path to executing script

#!/opt/local/bin/bash

declare script_dir script_path


# man bash 2>/dev/null | less -p 'BASH_SOURCE'
[[ ${BASH_VERSINFO[0]} -le 2 ]] && echo 'The BASH_SOURCE array variable is only available for Bash 3.0 and higher!' && exit 1

[[ "${BASH_SOURCE[0]}" != "${0}" ]] && echo "script ${BASH_SOURCE[0]} is running sourced ..."



# cf. Bash get self directory trick,
# http://stevemorin.blogspot.com/2007/10/bash-get-self-directory-trick.html

script_path="$(cd $(/usr/bin/dirname "${BASH_SOURCE[0]}"); pwd -P)/$(/usr/bin/basename "${BASH_SOURCE[0]}")"

[[ ! -f "$script_path" ]] && script_path="$(cd $(/usr/bin/dirname "$0"); pwd -P)/$(/usr/bin/basename "$0")"

[[ ! -f "$script_path" ]] && script_path="" && echo 'No full path to running script found!' && exit 1



# full path to executing script's directory
script_dir="${script_path%/*}"
echo "script_dir: ${script_dir}"


# full path to executing script
echo "script_path: ${script_path}"


exit 0

AppleScript to test if application is running

By name:
tell application "System Events" to (name of processes) contains "iTunes"


By creator type, in case it might have a version # appended:
tell application "System Events" to (creator type of processes) contains "InDn"


Grab creator type dynamically like so:
tell application "Finder" to creator type of (selection as alias)


From http://applescriptsourcebook.com/viewtopic.php?pid=46830