Mac OS X knows which application to launch when you double-click on a document icon or use the applescript “open” command on a document icon.
This application doesn’t necessarily correspond to creator code, file type, or extension (each of which may indicate a different application), but it always corresponds to the “open with” information in the get-info dialog.
How can I use Applescript to determine with certainty before-hand which application will launch on “open”-ing a given document file?
Or, to put it another way, how do I determine the default “open with” application for a given file?
I’m reasonably certain that takes place in LaunchServices, but don’t know the mechanism. For example, this script will get you the path to the default email application, but in modified form doesn’t work for all browsers.
set emailerPath to GetDefaultEmail()
to GetDefaultEmail()
set creatorType to word -1 of (do shell script ¬
"defaults read com.apple.LaunchServices |grep -C5 U:mailto | grep -w LSBundleSignature")
set {text:creatorType} to (text of creatorType) as text
tell application "Finder"
set {name_, container_path} to ¬
{name, container} of application file id creatorType
end tell
return (container_path as text) & name_
end GetDefaultEmail
It may happen in LaunchServices, but if you do a defaults read of it, you’ll see that not all the information needed is contained in the output. That is, I may have 100 text files, and 90 of them may have a default application of TextEdit, 9 may have a default application of BBEdit, and 1 may have a default application of Word. But that sort of information doesn’t seem to be in the defaults read of LaunchServices.
Not anything that I can find myself. The simplest question is “When I Get Info on a document, where does that service get it’s ‘Open with’ information, or if I set that 'Open with” application, where is that setting stored?" I don’t know the answers, but would really like to. I know there’s more than one database for this because, for example, in LS, Camino is said to have creator code “CHIM”, but if you use Xray or File Buddy to look, they say “MOZC”.
The record returned by ‘info for’ has a ‘default application’ property, which is an alias to the application file. You could give that a try:
set defaultApp to default application of (info for alias myDoc)
tell application (defaultApp as Unicode text) to activate
Alternatively, you could open the document in a suitable application of your own choice anyway:
tell application "Finder"
open document file "My doc.pdf" using application file ((path to applications folder as Unicode text) & "Preview.app")
end tell