Access Finder's "Open With" menu from the commandline (for tab completion)

5

6

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?

ShreevatsaR

Posted 2011-07-29T05:01:20.990

Reputation: 776

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 – 2011-07-29T14:25:03.043

Answers

5

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.)

Lri

Posted 2011-07-29T05:01:20.990

Reputation: 34 501

1Thanks 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 – 2011-07-29T19:39:09.887

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 – 2011-08-12T16:23:42.703

0

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.

barbaz

Posted 2011-07-29T05:01:20.990

Reputation: 2 696

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 – 2011-07-29T18:03:14.513

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 – 2011-07-29T18:05:20.203

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 – 2011-07-29T18:07:33.797

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 – 2011-07-29T18:48:42.177

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 – 2011-11-30T11:10:23.990