Is there a MacOS equivalent of the Windows assoc command?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
1












I am looking to change a file association on the Mac using some sort of script. I know that I can do something like that in Windows using the assoc command.



Is there something scriptable on MacOS which will do a similar job? I don’t mind whether it’s in Bash (my preference), AppleScript or something in Automator.



I am aware of the dutil command but I want to be able to do it without additional software.










share|improve this question

























    up vote
    4
    down vote

    favorite
    1












    I am looking to change a file association on the Mac using some sort of script. I know that I can do something like that in Windows using the assoc command.



    Is there something scriptable on MacOS which will do a similar job? I don’t mind whether it’s in Bash (my preference), AppleScript or something in Automator.



    I am aware of the dutil command but I want to be able to do it without additional software.










    share|improve this question























      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      I am looking to change a file association on the Mac using some sort of script. I know that I can do something like that in Windows using the assoc command.



      Is there something scriptable on MacOS which will do a similar job? I don’t mind whether it’s in Bash (my preference), AppleScript or something in Automator.



      I am aware of the dutil command but I want to be able to do it without additional software.










      share|improve this question













      I am looking to change a file association on the Mac using some sort of script. I know that I can do something like that in Windows using the assoc command.



      Is there something scriptable on MacOS which will do a similar job? I don’t mind whether it’s in Bash (my preference), AppleScript or something in Automator.



      I am aware of the dutil command but I want to be able to do it without additional software.







      terminal applescript bash automator defaults






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 4 hours ago









      Manngo

      89051731




      89051731




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote













          Since the macOS system uses Launch Services database and utilities to map file types and secondarily, file extensions to applications that open them, the closest analog to assoc would be lsregister



          It's buried away inside the system folder, Core Services, framework that contains the LaunchServices.



          There are tons of good questions and answers here once you know the utility to search for as are there tons of excellent blog posts, developer guides and official documentation.



          Since I have /usr/local/bin in my path, I usually sym link it there so I can host type it:



          mac:~ me$ ln -s /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister /usr/local/bin
          mac:~ me$ lsregister
          lsregister: [OPTIONS] [ <path>... ]
          [ -apps <domain>[,domain]... ]
          [ -libs <domain>[,domain]... ]
          [ -all <domain>[,domain]... ]

          Paths are searched for applications to register with the Launch Service database.
          Valid domains are "system", "local", "network" and "user". Domains can also
          be specified using only the first letter.

          -kill Reset the Launch Services database before doing anything else
          -seed If database isn't seeded, scan default locations for applications and libraries to register
          -lint Print information about plist errors while registering bundles
          -lazy n Sleep for n seconds before registering/scanning
          -r Recursive directory scan, do not recurse into packages or invisible directories
          -R Recursive directory scan, descending into packages and invisible directories
          -f force-update registration even if mod date is unchanged
          -u unregister instead of register
          -v Display progress information
          -gc Garbage collect old data and compact the database
          -dump Display full database contents after registration
          -h Display this help





          share|improve this answer



























            up vote
            1
            down vote













            Changing the file association for a single file or a set of files (AppleScript)



            This script demonstrates first that the default application for some text file on my system was set to TextEdit. Then it changes the file association for that particular file so it now opens with Atom. Finally, it associates all text files on the desktop with the Atom application.



            tell application "System Events"
            get the default application of the file "/path/to/some file.txt"
            --> alias "Macintosh HD:Applications:TextEdit.app:" of application "System Events"

            # Individual file:
            set the default application of the file "/path/to/some file.txt" to ¬
            the path to the application named "Atom"

            # A set of files:
            set the default application of every file of the desktop folder whose ¬
            name extension = "txt" to the path to the application "Atom"
            end tell


            Changing the file associations for all files of a given type (JXA)



            Using JavaScript for Automation, you can implement Core Foundation functions in a way you cannot do with AppleScriptObjC, so as to interact with Launch Services at the system level and change the file association for a given file type.



            Here, I've targetted plain text files (these have extension .txt by default), and switched the default application that responds to them to Atom:



            ObjC.import('CoreServices');

            var contentType = 'public.plain-text';
            var bundleID = Application('Atom').id();

            $.LSSetDefaultRoleHandlerForContentType(
            contentType,
            $.kLSRolesAll,
            bundleID
            );


            The file type must be targeted by way of a Uniform Type Identifier. These are special text strings that uniquely identify a given class or type of item. The link will take you to a page that lists Apple's system-declared UTIs for all the likely file types that you'll be interested in. Be careful not to simply choose the one that appears to match your needs at first glance, as UTIs are structured in a kind of inheritance tree. Therefore, I could have lazily picked out public.text, which I saw first in the list, until on further reading, we learn that this UTI is a base type for all text, which would include HTML and RTF files.






            share|improve this answer






















              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "118"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fapple.stackexchange.com%2fquestions%2f341432%2fis-there-a-macos-equivalent-of-the-windows-assoc-command%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              3
              down vote













              Since the macOS system uses Launch Services database and utilities to map file types and secondarily, file extensions to applications that open them, the closest analog to assoc would be lsregister



              It's buried away inside the system folder, Core Services, framework that contains the LaunchServices.



              There are tons of good questions and answers here once you know the utility to search for as are there tons of excellent blog posts, developer guides and official documentation.



              Since I have /usr/local/bin in my path, I usually sym link it there so I can host type it:



              mac:~ me$ ln -s /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister /usr/local/bin
              mac:~ me$ lsregister
              lsregister: [OPTIONS] [ <path>... ]
              [ -apps <domain>[,domain]... ]
              [ -libs <domain>[,domain]... ]
              [ -all <domain>[,domain]... ]

              Paths are searched for applications to register with the Launch Service database.
              Valid domains are "system", "local", "network" and "user". Domains can also
              be specified using only the first letter.

              -kill Reset the Launch Services database before doing anything else
              -seed If database isn't seeded, scan default locations for applications and libraries to register
              -lint Print information about plist errors while registering bundles
              -lazy n Sleep for n seconds before registering/scanning
              -r Recursive directory scan, do not recurse into packages or invisible directories
              -R Recursive directory scan, descending into packages and invisible directories
              -f force-update registration even if mod date is unchanged
              -u unregister instead of register
              -v Display progress information
              -gc Garbage collect old data and compact the database
              -dump Display full database contents after registration
              -h Display this help





              share|improve this answer
























                up vote
                3
                down vote













                Since the macOS system uses Launch Services database and utilities to map file types and secondarily, file extensions to applications that open them, the closest analog to assoc would be lsregister



                It's buried away inside the system folder, Core Services, framework that contains the LaunchServices.



                There are tons of good questions and answers here once you know the utility to search for as are there tons of excellent blog posts, developer guides and official documentation.



                Since I have /usr/local/bin in my path, I usually sym link it there so I can host type it:



                mac:~ me$ ln -s /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister /usr/local/bin
                mac:~ me$ lsregister
                lsregister: [OPTIONS] [ <path>... ]
                [ -apps <domain>[,domain]... ]
                [ -libs <domain>[,domain]... ]
                [ -all <domain>[,domain]... ]

                Paths are searched for applications to register with the Launch Service database.
                Valid domains are "system", "local", "network" and "user". Domains can also
                be specified using only the first letter.

                -kill Reset the Launch Services database before doing anything else
                -seed If database isn't seeded, scan default locations for applications and libraries to register
                -lint Print information about plist errors while registering bundles
                -lazy n Sleep for n seconds before registering/scanning
                -r Recursive directory scan, do not recurse into packages or invisible directories
                -R Recursive directory scan, descending into packages and invisible directories
                -f force-update registration even if mod date is unchanged
                -u unregister instead of register
                -v Display progress information
                -gc Garbage collect old data and compact the database
                -dump Display full database contents after registration
                -h Display this help





                share|improve this answer






















                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  Since the macOS system uses Launch Services database and utilities to map file types and secondarily, file extensions to applications that open them, the closest analog to assoc would be lsregister



                  It's buried away inside the system folder, Core Services, framework that contains the LaunchServices.



                  There are tons of good questions and answers here once you know the utility to search for as are there tons of excellent blog posts, developer guides and official documentation.



                  Since I have /usr/local/bin in my path, I usually sym link it there so I can host type it:



                  mac:~ me$ ln -s /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister /usr/local/bin
                  mac:~ me$ lsregister
                  lsregister: [OPTIONS] [ <path>... ]
                  [ -apps <domain>[,domain]... ]
                  [ -libs <domain>[,domain]... ]
                  [ -all <domain>[,domain]... ]

                  Paths are searched for applications to register with the Launch Service database.
                  Valid domains are "system", "local", "network" and "user". Domains can also
                  be specified using only the first letter.

                  -kill Reset the Launch Services database before doing anything else
                  -seed If database isn't seeded, scan default locations for applications and libraries to register
                  -lint Print information about plist errors while registering bundles
                  -lazy n Sleep for n seconds before registering/scanning
                  -r Recursive directory scan, do not recurse into packages or invisible directories
                  -R Recursive directory scan, descending into packages and invisible directories
                  -f force-update registration even if mod date is unchanged
                  -u unregister instead of register
                  -v Display progress information
                  -gc Garbage collect old data and compact the database
                  -dump Display full database contents after registration
                  -h Display this help





                  share|improve this answer












                  Since the macOS system uses Launch Services database and utilities to map file types and secondarily, file extensions to applications that open them, the closest analog to assoc would be lsregister



                  It's buried away inside the system folder, Core Services, framework that contains the LaunchServices.



                  There are tons of good questions and answers here once you know the utility to search for as are there tons of excellent blog posts, developer guides and official documentation.



                  Since I have /usr/local/bin in my path, I usually sym link it there so I can host type it:



                  mac:~ me$ ln -s /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister /usr/local/bin
                  mac:~ me$ lsregister
                  lsregister: [OPTIONS] [ <path>... ]
                  [ -apps <domain>[,domain]... ]
                  [ -libs <domain>[,domain]... ]
                  [ -all <domain>[,domain]... ]

                  Paths are searched for applications to register with the Launch Service database.
                  Valid domains are "system", "local", "network" and "user". Domains can also
                  be specified using only the first letter.

                  -kill Reset the Launch Services database before doing anything else
                  -seed If database isn't seeded, scan default locations for applications and libraries to register
                  -lint Print information about plist errors while registering bundles
                  -lazy n Sleep for n seconds before registering/scanning
                  -r Recursive directory scan, do not recurse into packages or invisible directories
                  -R Recursive directory scan, descending into packages and invisible directories
                  -f force-update registration even if mod date is unchanged
                  -u unregister instead of register
                  -v Display progress information
                  -gc Garbage collect old data and compact the database
                  -dump Display full database contents after registration
                  -h Display this help






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 4 hours ago









                  bmike♦

                  152k46271596




                  152k46271596






















                      up vote
                      1
                      down vote













                      Changing the file association for a single file or a set of files (AppleScript)



                      This script demonstrates first that the default application for some text file on my system was set to TextEdit. Then it changes the file association for that particular file so it now opens with Atom. Finally, it associates all text files on the desktop with the Atom application.



                      tell application "System Events"
                      get the default application of the file "/path/to/some file.txt"
                      --> alias "Macintosh HD:Applications:TextEdit.app:" of application "System Events"

                      # Individual file:
                      set the default application of the file "/path/to/some file.txt" to ¬
                      the path to the application named "Atom"

                      # A set of files:
                      set the default application of every file of the desktop folder whose ¬
                      name extension = "txt" to the path to the application "Atom"
                      end tell


                      Changing the file associations for all files of a given type (JXA)



                      Using JavaScript for Automation, you can implement Core Foundation functions in a way you cannot do with AppleScriptObjC, so as to interact with Launch Services at the system level and change the file association for a given file type.



                      Here, I've targetted plain text files (these have extension .txt by default), and switched the default application that responds to them to Atom:



                      ObjC.import('CoreServices');

                      var contentType = 'public.plain-text';
                      var bundleID = Application('Atom').id();

                      $.LSSetDefaultRoleHandlerForContentType(
                      contentType,
                      $.kLSRolesAll,
                      bundleID
                      );


                      The file type must be targeted by way of a Uniform Type Identifier. These are special text strings that uniquely identify a given class or type of item. The link will take you to a page that lists Apple's system-declared UTIs for all the likely file types that you'll be interested in. Be careful not to simply choose the one that appears to match your needs at first glance, as UTIs are structured in a kind of inheritance tree. Therefore, I could have lazily picked out public.text, which I saw first in the list, until on further reading, we learn that this UTI is a base type for all text, which would include HTML and RTF files.






                      share|improve this answer


























                        up vote
                        1
                        down vote













                        Changing the file association for a single file or a set of files (AppleScript)



                        This script demonstrates first that the default application for some text file on my system was set to TextEdit. Then it changes the file association for that particular file so it now opens with Atom. Finally, it associates all text files on the desktop with the Atom application.



                        tell application "System Events"
                        get the default application of the file "/path/to/some file.txt"
                        --> alias "Macintosh HD:Applications:TextEdit.app:" of application "System Events"

                        # Individual file:
                        set the default application of the file "/path/to/some file.txt" to ¬
                        the path to the application named "Atom"

                        # A set of files:
                        set the default application of every file of the desktop folder whose ¬
                        name extension = "txt" to the path to the application "Atom"
                        end tell


                        Changing the file associations for all files of a given type (JXA)



                        Using JavaScript for Automation, you can implement Core Foundation functions in a way you cannot do with AppleScriptObjC, so as to interact with Launch Services at the system level and change the file association for a given file type.



                        Here, I've targetted plain text files (these have extension .txt by default), and switched the default application that responds to them to Atom:



                        ObjC.import('CoreServices');

                        var contentType = 'public.plain-text';
                        var bundleID = Application('Atom').id();

                        $.LSSetDefaultRoleHandlerForContentType(
                        contentType,
                        $.kLSRolesAll,
                        bundleID
                        );


                        The file type must be targeted by way of a Uniform Type Identifier. These are special text strings that uniquely identify a given class or type of item. The link will take you to a page that lists Apple's system-declared UTIs for all the likely file types that you'll be interested in. Be careful not to simply choose the one that appears to match your needs at first glance, as UTIs are structured in a kind of inheritance tree. Therefore, I could have lazily picked out public.text, which I saw first in the list, until on further reading, we learn that this UTI is a base type for all text, which would include HTML and RTF files.






                        share|improve this answer
























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Changing the file association for a single file or a set of files (AppleScript)



                          This script demonstrates first that the default application for some text file on my system was set to TextEdit. Then it changes the file association for that particular file so it now opens with Atom. Finally, it associates all text files on the desktop with the Atom application.



                          tell application "System Events"
                          get the default application of the file "/path/to/some file.txt"
                          --> alias "Macintosh HD:Applications:TextEdit.app:" of application "System Events"

                          # Individual file:
                          set the default application of the file "/path/to/some file.txt" to ¬
                          the path to the application named "Atom"

                          # A set of files:
                          set the default application of every file of the desktop folder whose ¬
                          name extension = "txt" to the path to the application "Atom"
                          end tell


                          Changing the file associations for all files of a given type (JXA)



                          Using JavaScript for Automation, you can implement Core Foundation functions in a way you cannot do with AppleScriptObjC, so as to interact with Launch Services at the system level and change the file association for a given file type.



                          Here, I've targetted plain text files (these have extension .txt by default), and switched the default application that responds to them to Atom:



                          ObjC.import('CoreServices');

                          var contentType = 'public.plain-text';
                          var bundleID = Application('Atom').id();

                          $.LSSetDefaultRoleHandlerForContentType(
                          contentType,
                          $.kLSRolesAll,
                          bundleID
                          );


                          The file type must be targeted by way of a Uniform Type Identifier. These are special text strings that uniquely identify a given class or type of item. The link will take you to a page that lists Apple's system-declared UTIs for all the likely file types that you'll be interested in. Be careful not to simply choose the one that appears to match your needs at first glance, as UTIs are structured in a kind of inheritance tree. Therefore, I could have lazily picked out public.text, which I saw first in the list, until on further reading, we learn that this UTI is a base type for all text, which would include HTML and RTF files.






                          share|improve this answer














                          Changing the file association for a single file or a set of files (AppleScript)



                          This script demonstrates first that the default application for some text file on my system was set to TextEdit. Then it changes the file association for that particular file so it now opens with Atom. Finally, it associates all text files on the desktop with the Atom application.



                          tell application "System Events"
                          get the default application of the file "/path/to/some file.txt"
                          --> alias "Macintosh HD:Applications:TextEdit.app:" of application "System Events"

                          # Individual file:
                          set the default application of the file "/path/to/some file.txt" to ¬
                          the path to the application named "Atom"

                          # A set of files:
                          set the default application of every file of the desktop folder whose ¬
                          name extension = "txt" to the path to the application "Atom"
                          end tell


                          Changing the file associations for all files of a given type (JXA)



                          Using JavaScript for Automation, you can implement Core Foundation functions in a way you cannot do with AppleScriptObjC, so as to interact with Launch Services at the system level and change the file association for a given file type.



                          Here, I've targetted plain text files (these have extension .txt by default), and switched the default application that responds to them to Atom:



                          ObjC.import('CoreServices');

                          var contentType = 'public.plain-text';
                          var bundleID = Application('Atom').id();

                          $.LSSetDefaultRoleHandlerForContentType(
                          contentType,
                          $.kLSRolesAll,
                          bundleID
                          );


                          The file type must be targeted by way of a Uniform Type Identifier. These are special text strings that uniquely identify a given class or type of item. The link will take you to a page that lists Apple's system-declared UTIs for all the likely file types that you'll be interested in. Be careful not to simply choose the one that appears to match your needs at first glance, as UTIs are structured in a kind of inheritance tree. Therefore, I could have lazily picked out public.text, which I saw first in the list, until on further reading, we learn that this UTI is a base type for all text, which would include HTML and RTF files.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 1 hour ago

























                          answered 3 hours ago









                          CJK

                          1,99811




                          1,99811



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fapple.stackexchange.com%2fquestions%2f341432%2fis-there-a-macos-equivalent-of-the-windows-assoc-command%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              List of Gilmore Girls characters

                              One-line joke