83

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?

2
  • 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, 2011 at 22:15
  • I like the osascript solution. How many times a second do you need to run this though?
    – arya
    Mar 6, 2015 at 8:00

5 Answers 5

52

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
3
  • Can I get the bundle identifier for a deleted app?
    – zyy
    Jan 18, 2021 at 4:22
  • +1 Worked for AnyDesk.
    – RHaguiuda
    Jan 31, 2022 at 12:05
  • defaults read /Applications/Safari.app/Contents/Info.plist CFBundleIdentifier does the same trick
    – DawnSong
    Mar 30, 2022 at 4:34
37

mdls -name kMDItemCFBundleIdentifier -r SomeApp.app

20

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.

4
  • 1
    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, 2017 at 1:10
  • @user137369 Could you please tell me what's that app? BTW, the app has to be launched to use lsappinfo
    – C.W.
    Jan 25, 2017 at 17:25
  • 7
    lsappinfo only works on currently-running apps.
    – mh.
    Sep 5, 2017 at 20:39
  • lsappinfo outputs only a blank line for my currently-running apps.
    – Devon
    May 6, 2022 at 0:17
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)"
0

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|(.*/)(.*)|\\1\t\\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'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .