#!/usr/bin/env bash
{ set +x; } 2>/dev/null

usage() {
    echo "usage: $(basename $0) app_name" 1>&2
    [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; exit
}

[[ $1 == "-h" ]] || [[ $1 == "--help" ]] && usage "$@"

[[ $# != 1 ]] && usage

[[ $1 != *.app ]] && set -- "$1".app

# 1) osascript -e 'tell application "System Events" to POSIX path of (file of process "<name>" as alias)'
# 2) parse `lsregister -dump`

PATH_LAUNCHSERVICES="/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"
dump="$(${PATH_LAUNCHSERVICES} -dump)" || exit

echo "$dump" | grep -i "/$1"$ | grep -v -E "Caches|TimeMachine|Temporary|/Volumes/$1" | uniq | awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}'
