Find specific apps by name, getting their version, via command line

applicationscommand linelaunch-services

For a support information gathering tool, I like to be able to look up several apps by name (or rather by their bundle ID) and fetch their versions.

I don't like to use the find command for this as that's not going to find all possible apps, or it'll take forever.

There's lsregister -dump, which knows about all those apps that interest me, but that command seems to offer no option to look up information only about specific apps. I'd have to filter the result with grep, but can't figure out how to filter it so that I can look for an app's bundle ID but also get the version (which appears in a different line).

There's also mdfind, which might be suitable for my purpose.

Let's, for instance, say I want to know about all installed "Word" apps, such as the 2008, 2011 and 365 versions.

With lsregister -dump | grep 'com\.microsoft\.Word', I'd get these lines:

identifier:    com.microsoft.Word (0x800460c2)
activityTypes: NOTIFICATION#:com.microsoft.Word, pv-e851f8544284d1

That's not helpful to inquire the version, is it?

With mdfind "kMDItemCFBundleIdentifier == com.microsoft.Word" I'd get the paths:

/Applications/Microsoft Office 2008/Microsoft Word.app
/Applications/Microsoft Office 2011/Microsoft Word.app

But how would I get the version from that? I thought I'd filter the resulting paths with xargs but that doesn't work with the spaces in the paths.

Best Answer

A solution using mdfind and mdls as a one-liner:

mdfind "kMDItemCFBundleIdentifier == com.microsoft.Word" | while IFS= read -r path ; do echo "$path"; mdls -name kMDItemVersion "$path"; done

This would print (if both Office 2008 and 2011 are installed):

/Applications/Microsoft Office 2008/Microsoft Word.app
kMDItemVersion = "12.3.6"
/Applications/Microsoft Office 2011/Microsoft Word.app
kMDItemVersion = "14.6.9"

That's good enough for my needs. I still wonder if there's a way to get the information from lsregister or a similar tool that looks into the Launch Services registry.