Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
    
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

4 Answers 4

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

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|(.*/)(.*)|\\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'
share|improve this answer

mdls -name kMDItemCFBundleIdentifier -r SomeApp.app

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.