Is it possible to script what applications should open certain file extensions

applescriptautomationbash

I have a new Macbook Pro and I'm reminded that my development environment isn't tailored to what I setup on my previous Macbook. I was curious if it's possible to create a script in either AppleScript or Bash that I can run which I can define what file format should be designates as the Open with application, for all applications I usually use.

For example, when a file is right clicked, Get Info, Open with for HTML it's set as Safari. I know I can manually go to Open With and choose Visual Studio Code but I'd like to write something that will change all files throughout my system.

Is this possible? How do you target the Open With application?

Best Answer

As far as I know, all file associations are defined on the launchservices.secure.plist file.

For macOS Mojave/Catalina this file is located at:

~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist

You can read the file contents using:

defaults read ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist

This command will output on terminal the file contents, which has each associated application with the corresponding file types.

Sample output:

$ defaults read ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist

{
    LSHandlers =     (
                {
            LSHandlerContentType = "public.3gpp";
            LSHandlerPreferredVersions =             {
                LSHandlerRoleAll = "-";
            };
            LSHandlerRoleAll = "com.colliderli.iina";
        },
                {
            LSHandlerContentType = "org.7-zip.7-zip-archive";
            LSHandlerPreferredVersions =             {
                LSHandlerRoleViewer = "-";
            };
            LSHandlerRoleViewer = "com.aone.keka";
        },
                {
            LSHandlerPreferredVersions =             {
                LSHandlerRoleAll = "-";
            };
            LSHandlerRoleAll = "com.apple.dt.xcode";
            LSHandlerURLScheme = xcpref;
        },
                {
            LSHandlerContentType = "public.html";
            LSHandlerPreferredVersions =             {
                LSHandlerRoleAll = "-";
            };
            LSHandlerRoleAll = "com.apple.safari";
        }
    );
}

I cannot fully answer your question now, but this is the starting point to look at and understand how those parameters works, and after testing and figuring it out we can expand this information with complete details and some examples of edited working versions.


But for the specific situation, considering the case of making a kind of template to just to replicate, (or to have it stored as a backup):

I suggest you could manually customize one system the way you want it, by traditional Finder file associations methods, and once it is fine, you save that .plist file, then you can replace that .plist file on the new system, or on the new user profile, and [reboot?, logout?, or kill finder?] and login again, and this should be enough for the associations be replicated and ready.

[Comments and edits are welcome]