Getting the bundle identifier of an OS X application in a shell script
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
mdls -name kMDItemCFBundleIdentifier -r SomeApp.app
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.