Getting the bundle identifier of an OS X application in a shell script











up vote
44
down vote

favorite
22












One option would be to use AppleScript:



$ osascript -e 'id of app "Finder"'
com.apple.finder


You could also do something like this:



$ bundle=$(mdfind -onlyin / kMDItemKind==Application | grep -i "/Finder.app$" | head -1)
$ defaults read "$bundle/Contents/Info" CFBundleIdentifier
com.apple.finder


Both of these are fairly slow (about 0.05-0.2s on my Air) though. Are there any faster or less hacky options?










share|improve this question




















  • 1




    Using defaults read seems like the right way to do it (or else querying LaunchServices via Obj-C) - why do you consider 0.1s slow?
    – Asmus
    Oct 16 '11 at 22:15










  • I like the osascript solution. How many times a second do you need to run this though?
    – arya
    Mar 6 '15 at 8:00















up vote
44
down vote

favorite
22












One option would be to use AppleScript:



$ osascript -e 'id of app "Finder"'
com.apple.finder


You could also do something like this:



$ bundle=$(mdfind -onlyin / kMDItemKind==Application | grep -i "/Finder.app$" | head -1)
$ defaults read "$bundle/Contents/Info" CFBundleIdentifier
com.apple.finder


Both of these are fairly slow (about 0.05-0.2s on my Air) though. Are there any faster or less hacky options?










share|improve this question




















  • 1




    Using defaults read seems like the right way to do it (or else querying LaunchServices via Obj-C) - why do you consider 0.1s slow?
    – Asmus
    Oct 16 '11 at 22:15










  • I like the osascript solution. How many times a second do you need to run this though?
    – arya
    Mar 6 '15 at 8:00













up vote
44
down vote

favorite
22









up vote
44
down vote

favorite
22






22





One option would be to use AppleScript:



$ osascript -e 'id of app "Finder"'
com.apple.finder


You could also do something like this:



$ bundle=$(mdfind -onlyin / kMDItemKind==Application | grep -i "/Finder.app$" | head -1)
$ defaults read "$bundle/Contents/Info" CFBundleIdentifier
com.apple.finder


Both of these are fairly slow (about 0.05-0.2s on my Air) though. Are there any faster or less hacky options?










share|improve this question















One option would be to use AppleScript:



$ osascript -e 'id of app "Finder"'
com.apple.finder


You could also do something like this:



$ bundle=$(mdfind -onlyin / kMDItemKind==Application | grep -i "/Finder.app$" | head -1)
$ defaults read "$bundle/Contents/Info" CFBundleIdentifier
com.apple.finder


Both of these are fairly slow (about 0.05-0.2s on my Air) though. Are there any faster or less hacky options?







macos






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 2 '12 at 19:22

























asked Oct 14 '11 at 1:10









user495470

30.7k586125




30.7k586125








  • 1




    Using defaults read seems like the right way to do it (or else querying LaunchServices via Obj-C) - why do you consider 0.1s slow?
    – Asmus
    Oct 16 '11 at 22:15










  • I like the osascript solution. How many times a second do you need to run this though?
    – arya
    Mar 6 '15 at 8:00














  • 1




    Using defaults read seems like the right way to do it (or else querying LaunchServices via Obj-C) - why do you consider 0.1s slow?
    – Asmus
    Oct 16 '11 at 22:15










  • I like the osascript solution. How many times a second do you need to run this though?
    – arya
    Mar 6 '15 at 8:00








1




1




Using defaults read seems like the right way to do it (or else querying LaunchServices via Obj-C) - why do you consider 0.1s slow?
– Asmus
Oct 16 '11 at 22:15




Using defaults read seems like the right way to do it (or else querying LaunchServices via Obj-C) - why do you consider 0.1s slow?
– Asmus
Oct 16 '11 at 22:15












I like the osascript solution. How many times a second do you need to run this though?
– arya
Mar 6 '15 at 8:00




I like the osascript solution. How many times a second do you need to run this though?
– arya
Mar 6 '15 at 8:00










5 Answers
5






active

oldest

votes

















up vote
33
down vote













How about reading the bundle identifier from the application's Info.plist file directly using PlistBuddy (8):



/usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' /Applications/Safari.app/Contents/Info.plist





share|improve this answer




























    up vote
    17
    down vote













    mdls -name kMDItemCFBundleIdentifier -r SomeApp.app






    share|improve this answer




























      up vote
      4
      down vote













      Use lsappinfo



      CC@~ $ lsappinfo info -only bundleid Finder
      "CFBundleIdentifier"="com.apple.finder"


      To get only the bundleid value, add | cut -d '"' -f4 to that command



      CC@~ $ lsappinfo info -only bundleid Finder | cut -d '"' -f4
      com.apple.finder


      You don't have to handle your code with the path of that application, even the path changes.



      As long as the application is started, you got an value.



      Though it is not as fast as @surry's answer, but it's fast enough.






      share|improve this answer























      • I’m not the downvoter, but this does not work reliably for me (while the others methods do). It’s working with some apps but not all.
        – user137369
        Jan 25 '17 at 1:10










      • @user137369 Could you please tell me what's that app? BTW, the app has to be launched to use lsappinfo
        – user1641838
        Jan 25 '17 at 17:25






      • 1




        lsappinfo only works on currently-running apps.
        – mh.
        Sep 5 '17 at 20:39


















      up vote
      1
      down vote













      Values of kMDItemKind depend on the current localization.



      How about this?



      mdls -name kMDItemCFBundleIdentifier 
      -raw "$(mdfind "(kMDItemContentTypeTree=com.apple.application) && (kMDItemDisplayName == 'photoshop*'cdw)" | head -1)"





      share|improve this answer




























        up vote
        0
        down vote



        accepted










        If showing all filename extensions is enabled, kMDItemDisplayName contains .app for some applications but not others. This would also escape names that contain ', ", or :



        a="Consultant's Canary"; a="${a//'/'}.app"; a=${a//"/\"}; a=${a//\/\\}; mdls -name kMDItemCFBundleIdentifier -raw "$(mdfind 'kMDItemContentType==com.apple.application-bundle&&kMDItemFSName=="'"$a"'"' | head -n1)"



        Another option:



        a=Finder; mdls -name kMDItemCFBundleIdentifier -raw "$(mdfind kMDItemContentType==com.apple.application-bundle | sed -E $'s|(.*/)(.*)|\1t\2|' | grep -F $'t'"$a".app -m1 | tr -d 't')"



        A single osascript command might also be faster:



        osascript -e 'on run args
        set output to {}
        repeat with a in args
        set end of output to id of app a
        end
        set text item delimiters to linefeed
        output as text
        end' Finder 'AppleScript Editor'





        share|improve this answer























          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fsuperuser.com%2fquestions%2f346369%2fgetting-the-bundle-identifier-of-an-os-x-application-in-a-shell-script%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          33
          down vote













          How about reading the bundle identifier from the application's Info.plist file directly using PlistBuddy (8):



          /usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' /Applications/Safari.app/Contents/Info.plist





          share|improve this answer

























            up vote
            33
            down vote













            How about reading the bundle identifier from the application's Info.plist file directly using PlistBuddy (8):



            /usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' /Applications/Safari.app/Contents/Info.plist





            share|improve this answer























              up vote
              33
              down vote










              up vote
              33
              down vote









              How about reading the bundle identifier from the application's Info.plist file directly using PlistBuddy (8):



              /usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' /Applications/Safari.app/Contents/Info.plist





              share|improve this answer












              How about reading the bundle identifier from the application's Info.plist file directly using PlistBuddy (8):



              /usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' /Applications/Safari.app/Contents/Info.plist






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered May 30 '13 at 22:25









              surryhill

              33133




              33133
























                  up vote
                  17
                  down vote













                  mdls -name kMDItemCFBundleIdentifier -r SomeApp.app






                  share|improve this answer

























                    up vote
                    17
                    down vote













                    mdls -name kMDItemCFBundleIdentifier -r SomeApp.app






                    share|improve this answer























                      up vote
                      17
                      down vote










                      up vote
                      17
                      down vote









                      mdls -name kMDItemCFBundleIdentifier -r SomeApp.app






                      share|improve this answer












                      mdls -name kMDItemCFBundleIdentifier -r SomeApp.app







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 19 '14 at 10:30









                      Sean

                      27124




                      27124






















                          up vote
                          4
                          down vote













                          Use lsappinfo



                          CC@~ $ lsappinfo info -only bundleid Finder
                          "CFBundleIdentifier"="com.apple.finder"


                          To get only the bundleid value, add | cut -d '"' -f4 to that command



                          CC@~ $ lsappinfo info -only bundleid Finder | cut -d '"' -f4
                          com.apple.finder


                          You don't have to handle your code with the path of that application, even the path changes.



                          As long as the application is started, you got an value.



                          Though it is not as fast as @surry's answer, but it's fast enough.






                          share|improve this answer























                          • I’m not the downvoter, but this does not work reliably for me (while the others methods do). It’s working with some apps but not all.
                            – user137369
                            Jan 25 '17 at 1:10










                          • @user137369 Could you please tell me what's that app? BTW, the app has to be launched to use lsappinfo
                            – user1641838
                            Jan 25 '17 at 17:25






                          • 1




                            lsappinfo only works on currently-running apps.
                            – mh.
                            Sep 5 '17 at 20:39















                          up vote
                          4
                          down vote













                          Use lsappinfo



                          CC@~ $ lsappinfo info -only bundleid Finder
                          "CFBundleIdentifier"="com.apple.finder"


                          To get only the bundleid value, add | cut -d '"' -f4 to that command



                          CC@~ $ lsappinfo info -only bundleid Finder | cut -d '"' -f4
                          com.apple.finder


                          You don't have to handle your code with the path of that application, even the path changes.



                          As long as the application is started, you got an value.



                          Though it is not as fast as @surry's answer, but it's fast enough.






                          share|improve this answer























                          • I’m not the downvoter, but this does not work reliably for me (while the others methods do). It’s working with some apps but not all.
                            – user137369
                            Jan 25 '17 at 1:10










                          • @user137369 Could you please tell me what's that app? BTW, the app has to be launched to use lsappinfo
                            – user1641838
                            Jan 25 '17 at 17:25






                          • 1




                            lsappinfo only works on currently-running apps.
                            – mh.
                            Sep 5 '17 at 20:39













                          up vote
                          4
                          down vote










                          up vote
                          4
                          down vote









                          Use lsappinfo



                          CC@~ $ lsappinfo info -only bundleid Finder
                          "CFBundleIdentifier"="com.apple.finder"


                          To get only the bundleid value, add | cut -d '"' -f4 to that command



                          CC@~ $ lsappinfo info -only bundleid Finder | cut -d '"' -f4
                          com.apple.finder


                          You don't have to handle your code with the path of that application, even the path changes.



                          As long as the application is started, you got an value.



                          Though it is not as fast as @surry's answer, but it's fast enough.






                          share|improve this answer














                          Use lsappinfo



                          CC@~ $ lsappinfo info -only bundleid Finder
                          "CFBundleIdentifier"="com.apple.finder"


                          To get only the bundleid value, add | cut -d '"' -f4 to that command



                          CC@~ $ lsappinfo info -only bundleid Finder | cut -d '"' -f4
                          com.apple.finder


                          You don't have to handle your code with the path of that application, even the path changes.



                          As long as the application is started, you got an value.



                          Though it is not as fast as @surry's answer, but it's fast enough.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 20 at 7:33









                          Mureinik

                          2,20351525




                          2,20351525










                          answered Nov 24 '15 at 7:22









                          user1641838

                          1784




                          1784












                          • I’m not the downvoter, but this does not work reliably for me (while the others methods do). It’s working with some apps but not all.
                            – user137369
                            Jan 25 '17 at 1:10










                          • @user137369 Could you please tell me what's that app? BTW, the app has to be launched to use lsappinfo
                            – user1641838
                            Jan 25 '17 at 17:25






                          • 1




                            lsappinfo only works on currently-running apps.
                            – mh.
                            Sep 5 '17 at 20:39


















                          • I’m not the downvoter, but this does not work reliably for me (while the others methods do). It’s working with some apps but not all.
                            – user137369
                            Jan 25 '17 at 1:10










                          • @user137369 Could you please tell me what's that app? BTW, the app has to be launched to use lsappinfo
                            – user1641838
                            Jan 25 '17 at 17:25






                          • 1




                            lsappinfo only works on currently-running apps.
                            – mh.
                            Sep 5 '17 at 20:39
















                          I’m not the downvoter, but this does not work reliably for me (while the others methods do). It’s working with some apps but not all.
                          – user137369
                          Jan 25 '17 at 1:10




                          I’m not the downvoter, but this does not work reliably for me (while the others methods do). It’s working with some apps but not all.
                          – user137369
                          Jan 25 '17 at 1:10












                          @user137369 Could you please tell me what's that app? BTW, the app has to be launched to use lsappinfo
                          – user1641838
                          Jan 25 '17 at 17:25




                          @user137369 Could you please tell me what's that app? BTW, the app has to be launched to use lsappinfo
                          – user1641838
                          Jan 25 '17 at 17:25




                          1




                          1




                          lsappinfo only works on currently-running apps.
                          – mh.
                          Sep 5 '17 at 20:39




                          lsappinfo only works on currently-running apps.
                          – mh.
                          Sep 5 '17 at 20:39










                          up vote
                          1
                          down vote













                          Values of kMDItemKind depend on the current localization.



                          How about this?



                          mdls -name kMDItemCFBundleIdentifier 
                          -raw "$(mdfind "(kMDItemContentTypeTree=com.apple.application) && (kMDItemDisplayName == 'photoshop*'cdw)" | head -1)"





                          share|improve this answer

























                            up vote
                            1
                            down vote













                            Values of kMDItemKind depend on the current localization.



                            How about this?



                            mdls -name kMDItemCFBundleIdentifier 
                            -raw "$(mdfind "(kMDItemContentTypeTree=com.apple.application) && (kMDItemDisplayName == 'photoshop*'cdw)" | head -1)"





                            share|improve this answer























                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              Values of kMDItemKind depend on the current localization.



                              How about this?



                              mdls -name kMDItemCFBundleIdentifier 
                              -raw "$(mdfind "(kMDItemContentTypeTree=com.apple.application) && (kMDItemDisplayName == 'photoshop*'cdw)" | head -1)"





                              share|improve this answer












                              Values of kMDItemKind depend on the current localization.



                              How about this?



                              mdls -name kMDItemCFBundleIdentifier 
                              -raw "$(mdfind "(kMDItemContentTypeTree=com.apple.application) && (kMDItemDisplayName == 'photoshop*'cdw)" | head -1)"






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Feb 12 '13 at 10:43









                              elmimmo

                              11113




                              11113






















                                  up vote
                                  0
                                  down vote



                                  accepted










                                  If showing all filename extensions is enabled, kMDItemDisplayName contains .app for some applications but not others. This would also escape names that contain ', ", or :



                                  a="Consultant's Canary"; a="${a//'/'}.app"; a=${a//"/\"}; a=${a//\/\\}; mdls -name kMDItemCFBundleIdentifier -raw "$(mdfind 'kMDItemContentType==com.apple.application-bundle&&kMDItemFSName=="'"$a"'"' | head -n1)"



                                  Another option:



                                  a=Finder; mdls -name kMDItemCFBundleIdentifier -raw "$(mdfind kMDItemContentType==com.apple.application-bundle | sed -E $'s|(.*/)(.*)|\1t\2|' | grep -F $'t'"$a".app -m1 | tr -d 't')"



                                  A single osascript command might also be faster:



                                  osascript -e 'on run args
                                  set output to {}
                                  repeat with a in args
                                  set end of output to id of app a
                                  end
                                  set text item delimiters to linefeed
                                  output as text
                                  end' Finder 'AppleScript Editor'





                                  share|improve this answer



























                                    up vote
                                    0
                                    down vote



                                    accepted










                                    If showing all filename extensions is enabled, kMDItemDisplayName contains .app for some applications but not others. This would also escape names that contain ', ", or :



                                    a="Consultant's Canary"; a="${a//'/'}.app"; a=${a//"/\"}; a=${a//\/\\}; mdls -name kMDItemCFBundleIdentifier -raw "$(mdfind 'kMDItemContentType==com.apple.application-bundle&&kMDItemFSName=="'"$a"'"' | head -n1)"



                                    Another option:



                                    a=Finder; mdls -name kMDItemCFBundleIdentifier -raw "$(mdfind kMDItemContentType==com.apple.application-bundle | sed -E $'s|(.*/)(.*)|\1t\2|' | grep -F $'t'"$a".app -m1 | tr -d 't')"



                                    A single osascript command might also be faster:



                                    osascript -e 'on run args
                                    set output to {}
                                    repeat with a in args
                                    set end of output to id of app a
                                    end
                                    set text item delimiters to linefeed
                                    output as text
                                    end' Finder 'AppleScript Editor'





                                    share|improve this answer

























                                      up vote
                                      0
                                      down vote



                                      accepted







                                      up vote
                                      0
                                      down vote



                                      accepted






                                      If showing all filename extensions is enabled, kMDItemDisplayName contains .app for some applications but not others. This would also escape names that contain ', ", or :



                                      a="Consultant's Canary"; a="${a//'/'}.app"; a=${a//"/\"}; a=${a//\/\\}; mdls -name kMDItemCFBundleIdentifier -raw "$(mdfind 'kMDItemContentType==com.apple.application-bundle&&kMDItemFSName=="'"$a"'"' | head -n1)"



                                      Another option:



                                      a=Finder; mdls -name kMDItemCFBundleIdentifier -raw "$(mdfind kMDItemContentType==com.apple.application-bundle | sed -E $'s|(.*/)(.*)|\1t\2|' | grep -F $'t'"$a".app -m1 | tr -d 't')"



                                      A single osascript command might also be faster:



                                      osascript -e 'on run args
                                      set output to {}
                                      repeat with a in args
                                      set end of output to id of app a
                                      end
                                      set text item delimiters to linefeed
                                      output as text
                                      end' Finder 'AppleScript Editor'





                                      share|improve this answer














                                      If showing all filename extensions is enabled, kMDItemDisplayName contains .app for some applications but not others. This would also escape names that contain ', ", or :



                                      a="Consultant's Canary"; a="${a//'/'}.app"; a=${a//"/\"}; a=${a//\/\\}; mdls -name kMDItemCFBundleIdentifier -raw "$(mdfind 'kMDItemContentType==com.apple.application-bundle&&kMDItemFSName=="'"$a"'"' | head -n1)"



                                      Another option:



                                      a=Finder; mdls -name kMDItemCFBundleIdentifier -raw "$(mdfind kMDItemContentType==com.apple.application-bundle | sed -E $'s|(.*/)(.*)|\1t\2|' | grep -F $'t'"$a".app -m1 | tr -d 't')"



                                      A single osascript command might also be faster:



                                      osascript -e 'on run args
                                      set output to {}
                                      repeat with a in args
                                      set end of output to id of app a
                                      end
                                      set text item delimiters to linefeed
                                      output as text
                                      end' Finder 'AppleScript Editor'






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Feb 12 '13 at 14:29

























                                      answered Jul 20 '12 at 16:01









                                      user495470

                                      30.7k586125




                                      30.7k586125






























                                           

                                          draft saved


                                          draft discarded



















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f346369%2fgetting-the-bundle-identifier-of-an-os-x-application-in-a-shell-script%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                                          Mangá

                                           ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕