Getting the bundle identifier of an OS X application in a shell script
up vote
44
down vote
favorite
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
add a comment |
up vote
44
down vote
favorite
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
1
Usingdefaults 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 theosascript
solution. How many times a second do you need to run this though?
– arya
Mar 6 '15 at 8:00
add a comment |
up vote
44
down vote
favorite
up vote
44
down vote
favorite
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
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
macos
edited Jun 2 '12 at 19:22
asked Oct 14 '11 at 1:10
user495470
30.8k586125
30.8k586125
1
Usingdefaults 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 theosascript
solution. How many times a second do you need to run this though?
– arya
Mar 6 '15 at 8:00
add a comment |
1
Usingdefaults 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 theosascript
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
add a comment |
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
add a comment |
up vote
17
down vote
mdls -name kMDItemCFBundleIdentifier -r SomeApp.app
add a comment |
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.
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 uselsappinfo
– user1641838
Jan 25 '17 at 17:25
1
lsappinfo
only works on currently-running apps.
– mh.
Sep 5 '17 at 20:39
add a comment |
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)"
add a comment |
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'
add a comment |
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
add a comment |
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
add a comment |
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
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
answered May 30 '13 at 22:25
surryhill
33133
33133
add a comment |
add a comment |
up vote
17
down vote
mdls -name kMDItemCFBundleIdentifier -r SomeApp.app
add a comment |
up vote
17
down vote
mdls -name kMDItemCFBundleIdentifier -r SomeApp.app
add a comment |
up vote
17
down vote
up vote
17
down vote
mdls -name kMDItemCFBundleIdentifier -r SomeApp.app
mdls -name kMDItemCFBundleIdentifier -r SomeApp.app
answered Nov 19 '14 at 10:30
Sean
27124
27124
add a comment |
add a comment |
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.
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 uselsappinfo
– user1641838
Jan 25 '17 at 17:25
1
lsappinfo
only works on currently-running apps.
– mh.
Sep 5 '17 at 20:39
add a comment |
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.
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 uselsappinfo
– user1641838
Jan 25 '17 at 17:25
1
lsappinfo
only works on currently-running apps.
– mh.
Sep 5 '17 at 20:39
add a comment |
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.
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.
edited Nov 20 at 7:33
Mureinik
2,23351525
2,23351525
answered Nov 24 '15 at 7:22
user1641838
1884
1884
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 uselsappinfo
– user1641838
Jan 25 '17 at 17:25
1
lsappinfo
only works on currently-running apps.
– mh.
Sep 5 '17 at 20:39
add a comment |
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 uselsappinfo
– 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
add a comment |
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)"
add a comment |
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)"
add a comment |
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)"
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)"
answered Feb 12 '13 at 10:43
elmimmo
11113
11113
add a comment |
add a comment |
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'
add a comment |
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'
add a comment |
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'
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'
edited Feb 12 '13 at 14:29
answered Jul 20 '12 at 16:01
user495470
30.8k586125
30.8k586125
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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