Programmatically Change Default Application to Open a File With

Finder allows the user to manually change the default 'Open With' application for a file by going to 'Get Info'.

Is there a way to programmatically do this, for example, with AppleScript, in order to change the default 'Open With' application of specific files?

My use-case is changing default 'Open With' application for files that are in a certain directory. I prefer the user doesn't have to do this manually.

from NSWorkspace.h

  • (void)setDefaultApplicationAtURL:(NSURL *)applicationURL toOpenContentTypeOfFileAtURL:(NSURL *)url completionHandler:(void (^_Nullable)(NSError *_Nullable error))completionHandler API_AVAILABLE(macos(12.0));

before macOS 12, use LSSetDefaultRoleHandlerForContentType from LSInfo.h in the LaunchServices framework.

No idea how to do this with AppleScript. You could use Automator to make a Folder Action for any file matching a pattern in a particular directory. You'd probably want to put a Set Application for Files action after a Filter Finder Items action.

@ssmith_c, I will try this out. Thanks!

Programmatically Change Default Application to Open a File With
 
 
Q