12

In Mac OSX lion, I'm trying to set default application for specific file types. Using the below apple script, we can set the default application for the specific "file.abc".

tell application "System Events"
set default application of file "/Users/test/Desktop/file.abc" to "/Applications/TextEdit.app"
end tell

But I want to set the same application as default for all the files having the filetype or extension as "abc".

I have tried the following to get it done. It added an entry in <HOME>/Library/Preferences/com.apple.LaunchServices.plist. But the files are not opened with the specified application.

defaults write com.apple.LaunchServices LSHandlers -array-add "<dict><key>LSHandlerContentTag</key><string>abc</string><key>LSHandlerContentTagClass</key><string>public.abc</string><key>LSHandlerRoleAll</key><string>com.apple.textedit</string></dict>"

Hope somebody knows what i m missing to achieve it.


Answer Found :

defaults write com.apple.LaunchServices LSHandlers -array-add "<dict><key>LSHandlerContentTag</key><string>ugurugu</string><key>LSHandlerContentTagClass</key><string>public.filename-extension</string><key>LSHandlerRoleAll</key<string>org.videolan.vlc</string></dict>"
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user

3 Answers 3

6

Maybe you're doing nothing wrong but that the com.apple.launchservices file just needs to be reloaded. You can logout, wait a few minutes or force launchservices to restart. In the following example I say that public.comma-separated-values-text files (note:This doesn't mean that every CSV file is this content-type) must be opened with TextEdit instead of Excel.

do shell script "defaults write com.apple.LaunchServices LSHandlers -array-add '{ LSHandlerContentType = \"public.comma-separated-values-text\"; LSHandlerRoleAll = \"com.apple.TextEdit\"; }'"

do shell script "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user"
7
  • Tried above scripts by creating a filetype "xyz" with application "com.apple.textedit". But not working. Are you sure above two line scripts works... Feb 7, 2012 at 9:52
  • I'm sure it works because we're using that script. Are you sure that you're using a correct file type 'xyz' and bundle identifier. To determine the content type you can use mdls and look what it returns. Even if since SL creator code support has dropped, it is possible that a file from an 10.5 (or older) system can't alter it's default application by LSHandlers. Open it on a 10.6 machine or newer and re-save the file. Feb 7, 2012 at 10:40
  • Thanks for ur kind help & time. With few changes with ur answer worked and it has been updated above. Feb 7, 2012 at 11:49
  • 1
    @GrahamPerrin : change will be reflected only when launch service register get reloaded. Its bcoz ur changes will not be available in already loaded cache. May 25, 2012 at 5:51
4

I'm not sure if you're only trying to do this programmatically. Are you?

Otherwise:

On the file, choose "get info", and under "open with" select the name of the application. Click on the button "Change All"

2
  • I want to do that "change All" under "open with" programatically. Feb 7, 2012 at 7:52
  • 1
    Thanks for ur kind help & time. Working answer in my mac has been updated above. Feb 7, 2012 at 11:49
0

You might want to take a look at RCDefaultApp and its source code. It's a program that lets you set which file types are opened by which apps in Launch Services.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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