Created
August 15, 2010 17:37
-
-
Save ttscoff/525723 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# This function performs app completion (open -a) based on known applications | |
# Originally by Kim Holburn http://www.holburn.net/ | |
# Modified by Brett Terpstra because it wasn't working on 10.6. | |
# Added case insensitivity and LC_ALL='C' because unicode chars were breaking it. | |
# Then I went and modified the completion command | |
# even though I have little idea what I'm doing. | |
# | |
# also add 'o' to complete because I alias o to "open -a" | |
export appslist=~/.apps.list | |
_make_app_list () { | |
local LC_ALL='C' | |
mdfind -onlyin /Applications -onlyin /Developer "kMDItemContentType == 'com.apple.application-*'" | \ | |
while read ; do | |
echo "${REPLY##*/}" | |
done |sort -i > "$appslist" | |
} | |
_apple_open () | |
{ | |
local cur prev | |
local LC_ALL='C' | |
# renew appslist if it's older than a day | |
if ! /usr/bin/perl -e ' | |
my $ARGV = $ARGV[0]; | |
if (-e $ARGV) { if (time - (stat $ARGV)[9] <= 86400) { exit (0); } } | |
exit 1; | |
' "$appslist" ; then | |
_make_app_list | |
fi | |
COMPREPLY=() | |
cur=${COMP_WORDS[COMP_CWORD]} | |
prev=${COMP_WORDS[COMP_CWORD-1]} | |
# do not attempt completion if we're specifying an option | |
[[ "$cur" == -* ]] && return 0 | |
if [[ "$prev" == -a || "$prev" == 'o' ]]; then | |
# If we have an appslist | |
if [ -s "$appslist" -a -r "$appslist" ]; then | |
# Escape dots in paths for grep | |
cur=${cur//\./\\\.} | |
# COMPREPLY=( $( grep "^$cur" "$appslist" ) ) | |
local IFS=" | |
" | |
COMPREPLY=( $( grep -i "^$cur" "$appslist" | sed -e 's/ /\\ /g' ) ) | |
fi | |
else | |
_filedir | |
fi | |
return 0 | |
} | |
# complete -F _apple_open open | |
complete -o bashdefault -o default -o nospace -F _apple_open open 2>/dev/null || complete -o default -o nospace -F _apple_open open | |
complete -o bashdefault -o default -o nospace -F _apple_open o 2>/dev/null || complete -o default -o nospace -F _apple_open o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment