#!/bin/zsh -f

#  Generate a list of all OS X applications that can be opened with "open -a"

##################################################################################

#     print "\e[41m\e[1mLoading application name completions for 'open -a' command...\e[0m"

    # -----------------------------------------------------------------------
        
        #       Function _comp_appgetter
        
        #       October 11, 2004 version 1.0.3,  W. G. Scott et al.
        
        #  Function to generate a listing of applications for command completion of the
        #
        #      open -a   application\ name
        #
        #  command.  
        
        #  Based on a version by Marius van Wyk, but now more general.  I also received
        #  invaluable help from Gary Kerbaugh, Nathan Hackett, Wataru Kagawa, and others.
        #
        #  This script takes about 3 seconds to run during shell startup on my 
        #  G4 800 laptop.  The slow point is having to use the file command to obtain
        #  Carbon apps like Microsoft Word.  The rest is based on locate and thus requires
        #  the locatedb to exist and to be updated regularly.
        #
        #  Add directories to the ones below as appropriate:

        function FindCarbonApps {
        cd /Applications
        # This is the slow part.  There must be a better way.
        file * | grep header | perl -pi -e 's;header for PowerPC PEF executable;;g' \
         | perl -pi -e 's;:;.app;g' 
        file */* | grep header | perl -pi -e 's;header for PowerPC PEF executable;;g' \
         | perl -pi -e 's;:;.app;g'
        file */*/* | grep header | perl -pi -e 's;header for PowerPC PEF executable;;g' \
         | perl -pi -e 's;:;.app;g'
        # This one causes some people problems, but if applications are missing from your list
        #  try using it...
        # file */*/*/* | grep header | perl -pi -e 's;header for PowerPC PEF executable;;g' \
        # | perl -pi -e 's;:;.app;g'
        # This finds the adobe apps:
        /usr/bin/locate "/Applications**/Contents" | grep -v .app | grep -v Plug-Ins\
         | grep -v plugin | grep -v Support | grep -v Documentation | grep -v bundle\
          | grep -v service | grep -v help | perl -pi -e 's;/Contents;.app;g'
        }
        
        
        function FindCocoaApps {
        /usr/bin/locate "/Applications**/*.app" | grep -v Contents
        
        if [[ -d $HOME/Applications ]]; then
            /usr/bin/locate "$HOME/Applications**/*.app" | grep -v Contents
        fi
        
        if [[ -d /Developer/Applications ]]; then
            /usr/bin/locate "/Developer/Applications**/*.app" | grep -v Contents
        fi
        }
        
        
        
        # This stuff runs the application finding functions and then strips off the 
        # basename, the .app suffixes, and escapes the spaces in the application names.
        
        (FindCarbonApps; FindCocoaApps) \
        | sed 's|^.*/\([^/]*\)\.app.*|\1|' \
        | perl -pi -e "s/([ ')(])/\\\\\$1/g" \
        | perl -pi -e 's;EndNote\\\ 7.0;EndNote\\\ 7.0.app.app;g' \
        | perl -pi -e 's;Adobe\\\ Reader\\\ 6.0;Adobe\\\ Reader\\\ 6.0.app.app;g' \
        | perl -pi -e 's;Adobe\\\ Reader\\\ 7.0;Adobe\\\ Reader\\\ 7.0.app.app;g' \
        | sort -u 
      
#    }  ### function _comp_appgetter -- end of definition
    # -----------------------------------------------------------------------
    
