Find Multiple Copies of an Application by Bundle ID?

Hey Folks,

You can do something like this to find a single app by Bundle ID with AppleScriptObjC:

use framework "Foundation"
use scripting additions

set appBundleID to "com.barebones.bbedit"
set nsWS to current application's NSWorkspace's sharedWorkspace()
set appURL to nsWS's URLForApplicationWithBundleIdentifier:appBundleID

Is there a neat trick to finding ALL apps on a startup-disk that have that Bundle ID?

I 'spect I can make do with Spotlight if necessary.

mdfind -onlyin / 'kMDItemCFBundleIdentifier == "com.barebones.bbedit"'

-Chris

I suspect Spotlight is your best bet:

use script "Metadata Lib" version "2.0.0"

set thePaths to perform search in folders {path to startup disk} predicate string "kMDItemCFBundleIdentifier  == 'com.barebones.bbedit'"
1 Like

There’s also LSCopyApplicationURLsForBundleIdentifier (although I never tried to use it in AppleScriptObjC).

It’s available from macOS 10.10.

1 Like

It can’t be used from AppleScriptObjC.

1 Like

You can’t use if from AppleScriptObjC, but you can call it from JXA:

ObjC.import('CoreServices');

(()=>{
	const id  = "com.barebones.bbedit";
	const nil = $();
	
	const CFArray = $.LSCopyApplicationURLsForBundleIdentifier(id, nil);
	const NSArray = $.NSArray.arrayWithArray(CFArray).valueForKey('path');
	return ObjC.deepUnwrap(NSArray);
})();
1 Like

Hey @CJK,

I just got a chance to test this, and it works perfectly.

Thanks!

-Chris

1 Like

I love the new avatar, by the way.

1 Like

To follow up on this: i just found out that mdfind isn’t 100% reliable.

A user recently informed me that mdfind couldn’t find his InDesign installations by the bundle identifier com.adobe.InDesign. After further testing, we found out that mdfind couldn’t find other Adobe apps either - and most likely any other app as well.

Switching to LSCopyApplicationURLsForBundleIdentifier solved the issue.

I’ll submit a bug to Apple on this.

Absolutely. It relies on the Spotlight index being turned on, up-to-date and uncorrupted, and the volume you’re searching supporting it. But that’s not really a bug (apart from any corruption, perhaps) — it’s the way Spotlight works. It’s not 100% — but barring unsupported volumes, it tends to be either 100% or 0%.

It’s also possible for the Launch Services database to become corrupted — and in that case LSCopyApplicationURLsForBundleIdentifier will also be unreliable.

2 Likes