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'
defaults readseems 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:15osascriptsolution. How many times a second do you need to run this though? – arya Mar 06 '15 at 08:00