Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I want to create an AppleScript that gets the Finder selection and displays a list of applications that are capable of opening it.

In other words, is there any way to get the list of apps that appears when you use Finder's "Open With >" contextual submenu?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

One option would be to use AllApplications in a shell script:

tell application "Finder"
    set p to POSIX path of (item 1 of (get selection) as text)
end tell
set l to do shell script "~/bin/AllApplications -path " & quoted form of p & " | sed 's/.*\\///g;s/\\.app$//g' | sort -f | uniq"
set answer to choose from list (paragraphs of l) without multiple selections allowed
if answer is false then return
set a to item 1 of answer
set p to path to application a
tell application "Finder" to open selection using p

You could also use System Events to get the applications from the Open With menu:

tell application "System Events" to tell process "Finder"
    set l to name of menu items of menu 1 of menu item "Open With" of menu 3 of menu bar 1
end tell
set text item delimiters to linefeed
set l2 to do shell script "grep -vx 'missing value' <<< " & quoted form of (l as text) & " | grep -vx Other… | sed -E 's/ \\([^)]*\\)$//g;s/ \\(default\\)$//g;s/\\.app$//g' | sort -f | uniq"
set answer to choose from list (paragraphs of l2) without multiple selections allowed
if answer is false then return
set a to item 1 of answer
set p to (path to application a)
tell application "Finder" to open selection using p

I didn't really test the scripts, but they're probably affected by rdar://9406282: Finder scripting selection may refer to bogus value not correlated with UI.

Related questions at Super User:

share|improve this answer
    
Holy cow! Fantastic! They both work; the AllApplications method is much faster. Thank you! –  Ze'ev Aug 23 '12 at 4:35

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.