Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

On Mac OS X, I know one can open a file from the commandline with its default application with open, and with other applications with open -a <application name>. Thus,

open movie.avi
open -a VLC movie.avi
open movie.avi -a VLC

all work. My only complaint is that when I type

open movie.avi -a <TAB>

it completes over all applications that exist on the computer — for instance in zsh I get

zsh: do you wish to see all 793 possibilities (200 lines)? 

— while what I would like is for it to complete over only the handful of applications that Finder's "Open With" menu would have shown.

So is there a way to access this list that Finder knows, and make my shell (zsh, but I can switch to bash if it helps) aware of the list, in such a way that tab completion would show only those applications?

share|improve this question
    
Well you could think about using lsregister to read out the various application claims. I don´t have an idea on how to parse the lsregister dump beautifully, but as a start, try lsregister -dump | grep -B 40 "\.avi" and try to incorporate that in a script. –  Asmus Jul 29 '11 at 14:25

2 Answers 2

up vote 5 down vote accepted

AllApplications

$ AllApplications -h

Created 03 March 2011 by Hank McShane
version 0.1
requires Mac OS X 10.4 or higher

Use this command line tool to get the path to all applications that can open a file from Launch Services.

Usage: AllApplications -path path/to/file

$ ext=png; f=/tmp/allapps.$ext; touch $f; AllApplications -path $f; rm $f
/Applications/Preview.app
/2/copies/Safari.app
/Applications/TeX/LaTeXiT.app
/Applications/WebKit.app
/Applications/Sequential.app
/Applications/ImageOptim.app
/Applications/Acorn.app
/Developer/Applications/Graphics Tools/Core Image Fun House.app
/2/copies/Preview.app
/Developer/Applications/Dashcode.app
/Applications/GraphicConverter.app
/Applications/Google Chrome.app
/Applications/Adobe Photoshop CS5/Adobe Photoshop CS5.app
/Applications/Utilities/QuickTime Player 7.app
/Applications/Utilities/ColorSync Utility.app
/Applications/Safari.app
/Applications/Adobe Device Central CS5/Adobe Device Central CS5.app
/Applications/Firefox.app

duti

$ duti -d public.png
com.apple.Preview
$ duti -l public.png
com.SequentialX.Sequential
com.flyingmeat.Acorn
net.pornel.ImageOptim
com.apple.system-library
com.apple.ColorSyncUtility
com.apple.Preview

(It's missing some apps that are displayed in the open with menu though.)

share|improve this answer
    
Thanks a lot; this helps and gives exactly what I wanted. Gives me only about a dozen applications instead of several hundred. Looking at the source, the magic function seems to be LSCopyApplicationURLsForURL. I'll see tomorrow if I can figure out how to incorporate the output of this program into zsh's completion. –  ShreevatsaR Jul 29 '11 at 19:39
    
Thanks. I got halfway through incorporating it with open -a, but then I got busy. Maybe I'll post it when I finish. This answers exactly what I asked, though, so I'm accepting it. –  ShreevatsaR Aug 12 '11 at 16:23

To me it appears that

it completes over all applications that exist on the computer — for instance in zsh I get

is not true. There does not seem to be any awareness for the open command in bash or zsh. So pressing simply uses the fallback and completes over all the files in your current working directory.

If you want bash to be aware of expected arguments to come after the -a switch of the open command, you will need to write a bash completion pattern for that.

To do so, you would need to familiarize yourself with the complete shell builtin

$ help complete
complete: complete [-abcdefgjksuv] [-pr] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [name ...]
    For each NAME, specify how arguments are to be completed.
    If the -p option is supplied, or if no options are supplied, existing
    completion specifications are printed in a way that allows them to be
    reused as input.  The -r option removes a completion specification for
    each NAME, or, if no NAMEs are supplied, all completion specifications.

OS X Lion simply points the user to a dialog showing the /Applications folder. To gain the list of Apps in there, you can simply use

basename -a -s.app /Applications/*.app

So a very rudimentary completion for open that does not check the -a switch nor handles spaces correctly would look like this

_openComp() 
{
        local WLIST cur
        COMPREPLY=()
        cur="${COMP_WORDS[COMP_CWORD]}"
        WLIST=`basename -a -s.app /Applications/*.app`
        COMPREPLY=( $(compgen -W "${WLIST}" -- ${cur}) )
        return 0
}
complete -F _openComp open

This applies to bash, i'm not familar with zsh's completion behaviour.

share|improve this answer
    
What? You're claiming that I'm lying about what happens on my computer?! This is a bizarre answer. In any case, my question is precisely about how to get Finder's list of applications, so that I can write a bash/zsh completion pattern. I know how to write completion patterns; I don't know how to get the data. –  ShreevatsaR Jul 29 '11 at 18:03
    
Well, I simply tried that out and neither on my 10.6 nor on my 10.7 system the TAB completion list contains apps or binaries - it simply contains the files contained in the current working directory. On 10.7 the "Open with..." dialog simply opens a folder listing for the /Applications folder anyway, so there is no great magic in how to get that listing. –  barbaz Jul 29 '11 at 18:05
    
Did you try open <TAB>, or open filename -a <TAB> or open -a <TAB>? And you seem to be right about Bash completing only the files in the current directory in all three cases; but Zsh (as I said) completes applications if you hit TAB after -a. –  ShreevatsaR Jul 29 '11 at 18:07
    
Can't reproduce that on zsh either, independently from a filename being present before the -a switch or not. I added some suggestions on how to get the list of Applications. –  barbaz Jul 29 '11 at 18:48
    
I guess you didn't have "autoload compinit" and "compinit" in your .zshrc. I don't know why they're not included in the default zsh config, but they're necessary to get good completion. –  ShreevatsaR Nov 30 '11 at 11:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.