Skip to content

Instantly share code, notes, and snippets.

@sathishvj
Last active August 4, 2023 05:57
Show Gist options
  • Save sathishvj/2a8398ffafac9b5a463a1fc6670730f4 to your computer and use it in GitHub Desktop.
Save sathishvj/2a8398ffafac9b5a463a1fc6670730f4 to your computer and use it in GitHub Desktop.
Open MacOS Application from command line based on pattern
# open matching application on MacOS based on part of the pattern
# case insensitive
# errors out if there is less than or more than a single match
# app is forwarded all parameters after pattern
# example usage: a gimp pic.jpeg
function a() {
local app="$1"
# if no dir is specified, then it is the current dir.
if [ -z "$app" ]; then
echo "You have to specify a part of an app name to start it."
return 1
fi
# length=`echo $matches | wc -l`
local length=`ls /Applications | grep -i -c "$app"`
if [[ $length -eq 0 ]]; then
echo "No matching apps."
return 1
fi
if [[ $length -gt 1 ]]; then
local matches=`ls /Applications | grep -i "$app"`
echo "More than 1 matching apps."
echo $matches
return 1
fi
if [[ $length -eq 1 ]]; then
local match=`ls /Applications | grep -i "$app"`
match=`echo "$match" | sed -e "s/\/$//"`
# remove @ of linked files
if [[ "$match" == *"@" ]]; then
match=${match%"@"}
fi
open -a "$match" "${@:2}"
return 0
fi
echo "other"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment