#!/bin/sh # This function performs app completion based on known applications # export appslist=~/.apps.list _make_app_list () { mdfind -onlyin /Applications \ -onlyin /Developer \ -onlyin /Applications2 \ "kMDItemContentType == 'com.apple.application-*'" | \ while read ; do echo "${REPLY##*/}" done |sort -i > "$appslist" } _apple_open () { local cur prev # 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 ]]; 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 "^$cur" "$appslist" | sed -e 's/ /\\ /g' ) ) fi else _filedir fi return 0 } complete -F _apple_open open