Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Perform zsh / spotlight / application completion UNIX
I was recently inspired to realize my goal of intelligent completion of the open(1) command using the built-in zsh facilities (man zshcompctl) and Spotlight. It is believed that the command...
 compctl -f -x 'c[-1,-a]' -M 'b:=*' -k _apps -- open
...provides this. I believe what it does is:
  • Per default completion on filenames and directories
  • If the previous word is -a, then perform completion using the _apps arrary.
This means that without -a, filenames and directories are completed, and that after the application has been completed, then filenames and directories will be completed; therefore an argument can be supplied to the application.

To populate the _apps array, I use these commands:
$ typeset -a _apps
$ _apps=(${(f)"$(mdfind "kMDItemKind == 'Application'")"})
An example run is below.

[bpm@snafu] c:~>typeset -a _apps
[bpm@snafu] c:~>_apps=(${(f)"$(mdfind "kMDItemKind == 'Application'")"})
[bpm@snafu] c:~>echo ${#_apps}
398
[bpm@snafu] c:~>echo ${_apps[0]}
/Applications/iPhoto.app
[bpm@snafu] c:~>compctl -f -x 'c[-1,-a]' -M 'b:=*' -k _apps -- open
[bpm@snafu] c:~>open -a /Library/Application Support/Adobe/Workflow/Adobe Workgroup Helper
[bpm@snafu] c:~>open -a Photo
/Applications/Adobe Photoshop Elements 2/Photoshop Elements 2.0
/Applications/Hewlett-Packard/HP Photo and Imaging Software/HP E-mail Portal/HP E-mail Portal
...
/Applications/Hewlett-Packard/HP Photo and Imaging Software/HP Tools/HP Image Dropper.app
/Applications/iPhoto.app
/Library/Documentation/Applications/iPhoto/iPhoto Getting Started.app
[robg adds: This worked as described when I tested it using the zsh shell...]
    •    
  • Currently 1.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[8,580 views]  

Perform zsh / spotlight / application completion | 6 comments | Create New Account
Click here to return to the 'Perform zsh / spotlight / application completion' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Perform zsh / spotlight / application completion
Authored by: 31d1 on Nov 23, '05 08:12:16AM

fink has a bash-completion package that works just like linux versions, once installed you have to source /sw/etc/bash-completion in your .bashrc file. That file will also source some more mac specific completion scripts which are included in the /sw/etc/bash-completion.d folder.

It completes all sorts of things, including apps with 'open -a'



[ Reply to This | # ]
Perform zsh / spotlight / application completion
Authored by: chtito on Nov 26, '05 02:05:33AM
Actually zsh is shipped on Mac OS X with completion for open -a. So i guess that the point of this hint is only to make that completion faster for open -a (the built-in one is indeed slow).

[ Reply to This | # ]
Perform zsh / spotlight / application completion
Authored by: bpm on Nov 23, '05 08:24:10AM
Since -a searches for the Application the full pathnames can be shortened to just the Application name. For example:

$ typeset -a _apps
$ _apps=(${(f)"$(mdfind "kMDItemKind == 'Application'" | perl -MFile::Basename -pe '$_ = &basename($_, ".app")')"})                                              
$ compctl -f -x 'c[-1,-a]' -k _apps -- open
$open -a W
WMV Player             Windows Media Player


[ Reply to This | # ]
Perform zsh / spotlight / application completion
Authored by: Stereo on Nov 24, '05 06:55:41PM
For some reason, this doesn't work for me:

% _apps=(${(f)"$(mdfind "kMDItemKind == 'Application'" | perl -MFile::Basename -pe '$_ = &basename($_, ".app")')"}) 
zsh: unknown sort specifier


[ Reply to This | # ]
Perform zsh / spotlight / application completion
Authored by: bpm on Nov 27, '05 08:00:07PM
Only thing I can think of is the version. I have:

$ zsh --version
zsh --version
zsh 4.2.3 (powerpc-apple-darwin8.0)


[ Reply to This | # ]
Perform zsh / spotlight / application completion
Authored by: wgscott on Nov 23, '05 01:03:24PM
We've done the work for you. Please feel free to use any or all of it as you see fit, if it might be of use:

[ Reply to This | # ]