Find Multiple Copies of an Application by Bundle ID?

(Christopher Stone) #1

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

0 Likes

(Shane Stanley) #2

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

#3

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

It’s available from macOS 10.10.

1 Like

(Shane Stanley) #4

It can’t be used from AppleScriptObjC.

1 Like

(CK) #5

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

(Christopher Stone) #6

Hey @CJK,

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

Thanks!

-Chris

1 Like

(CK) #7

I love the new avatar, by the way.

1 Like