Lion: changing a file's default launcher via AppleScript?

I’m trying to figure out how to change a file’s default launcher program via AppleScript under Lion.

What I want is to have a special folder on my system into which I can put video files. I’d like all videos that live in that folder to always be opened by a special viewing application. All videos living in all other folders should be opened in the normal, default manner. If I can change a file’s default launcher via AppleScript, then I can write a Folder Action which makes this change whenever I put files into this special folder, thereby causing all files in that folder to be launched using my special viewer.

Can this even be done in AppleScript? If not, I’m happy to use ASOC or even pure Cocoa.

Or is there perhaps a completely different way to accomplish what I want?

Thanks in advance for any suggestions or pointers to docs.

I believe it’s a private API. (Think about the security implications otherwise…)

Perhaps, but after posting my original message here, I stumbled upon this:

The routines LSSetDefaultHandlerForURLScheme and LSSetDefaultRoleHandlerForContentType that are described therein might do what I want. However, I don’t have time to test them right now. I’ll get to them over the next few days and report back with my findings.

They will let you set the default app for an extension, but not the app for a particular file.

Oh, now I see. Thanks.

So how about this idea, which just occurred to me? …

Suppose my special video viewing program is called I could associate it with a special suffix, say, *.vidview.

I could then write a Folder Action for my special folder which does the following when a file gets dropped into the folder. For the sake of this discussion, suppose the file is called

  1. Make a subdirectory in the special folder called .vidview, if one doesn’t already exist.
  2. Move the dropped file into that .vidview subdirectory.
  3. Create a symlink in the special directory called which points to .vidview/

Then, when I double-click on in the special folder, a reference to the original file will be passed to, who then can open it in its special way.

Later this week when I have more time, I’m going to try this.

With System Events on 10.6.x you can set a default application for each document. This way you can with an folder action or whatever set the application to open the document with to another application than the default application.

tell application "System Events"
	set default application of disk item theFile to disk item "Macintosh HD:Applications:Logic"
end tell

Good find, DJ. I see it’s still ink the dictionary with 10.7 – are you suggesting it no longer works, or have you only tested it in 10.6.x?

I only tested it in 10.6.8.

Setting the application for a specified file is not defined by launch services (in LS it is only binded with a file extension/type). When you want to set a specific application for a given file you need to add an extended attribute named to the file.

To get rid of the default application, simply remove the extended attribute from the file. Note: the recource fork attribute can contain other data that will be removed as well.

So presumably setxattr could be used, yes?

Yes it should be possible, it is the usro property that needs to be set in the attribute.