I wondered if it’s possible to get the bundle id of an app with ASObjC when the app is not running.
This is easily done with basic AppleScript:
set theApp to "Safari"
set appBundleID to id of application theApp
The following works but only if the app is running:
use framework "AppKit"
use framework "Foundation"
set theApp to "Safari"
set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set theRunningApps to theWorkspace's runningApplications()
set thePredicate to current application's NSPredicate's predicateWithFormat_("localizedName == %@", theApp)
set theApp to theRunningApps's filteredArrayUsingPredicate:thePredicate
set theBundleID to theApp's bundleIdentifier as text
BTW, @bmose posted a script here that returns the bundle id without launching the application, but, as far as I can ascertain, this script relies on basic AppleScript to get the bundle id.
use scripting additions
use framework "Foundation"
set appPath to "/Applications/Safari.app"
set appBundle to current application's NSBundle's bundleWithPath:appPath
set appBundleID to appBundle's bundleIdentifier() as text
Providing you know the application’s path or URL.
You can then create an NSBundle from either.
Thanks Mark. I’ve never used NSBundle before but your suggestion works great.
I think I can use the fullPathForApplication method to avoid having to specify the path.
use framework "AppKit"
use framework "Foundation"
use scripting additions
set theApp to "TextEdit"
set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set thePath to (theWorkspace's fullPathForApplication:theApp)
set appBundle to current application's NSBundle's bundleWithPath:thePath
set appBundleID to appBundle's bundleIdentifier() as text
@peavine I know you were looking for an ASObjC solution, but I figured you may be interested in this AppleScript solution, which allows you to get the bundle id of any app (running or not), without actually launching the app.
activate
set chosenApp to choose file with prompt ¬
"Choose An Application" of type "com.apple.application-bundle"
tell application "System Events" to set chosenApp to chosenApp's name
tell application chosenApp to set appBundleID to id
set the clipboard to appBundleID
display notification "The Bundle ID For " & chosenApp & ¬
" Was Copied To Your Clipboard" & linefeed & ¬
appBundleID with title "Get App's Bundle ID"
set {chosenApp, appBundleID} to {"", ""} -- Prevents App From Launching
This following version allows you to get the bundle id of any app (running or not), of the currently selected .app in the front most Finder window, without actually launching the app.
tell application "Finder" to set {chosenApp, nameExtension} to ¬
{name, name extension} of item 1 of (get selection)
if nameExtension ≠ "app" then
activate
display dialog "Selected file must be an Application." buttons ¬
{"Cancel", "OK"} default button "OK" giving up after 3
return
end if
tell application chosenApp to set appBundleID to id
set the clipboard to appBundleID
display notification "The Bundle ID For " & chosenApp & ¬
" Was Copied To Your Clipboard" & linefeed & ¬
appBundleID with title "Get App's Bundle ID"
set {chosenApp, appBundleID} to {"", ""} -- Prevents App From Launching
An alternative is the underrated API choose application of Scripting Additions.
activate
set chosenApp to choose application with prompt "Choose An Application"
set chosenApp to chosenApp's name
tell application chosenApp to set appBundleID to id
set the clipboard to appBundleID
display notification "The Bundle ID For " & chosenApp & ¬
" Was Copied To Your Clipboard" & linefeed & ¬
appBundleID with title "Get App's Bundle ID"
set {chosenApp, appBundleID} to {"", ""} -- Prevents App From Launching
Several years ago, I stopped using the choose application command because for whatever reason, every time the window popped up to choose the application, and I decided to click the “Browse” when I ran the code as part of an applet (outside of Script Editor.app) or from the Script Menu in the menu bar… Sometimes it would take up to 40 seconds for the new window to pop up to choose a different application. I just got used to avoiding the choose application of Scripting Additions command all together.
Things have changed dramatically since then. The problem was largely caused by AppleScript applets, which were all automatically regarded as being scriptable. Now they aren’t unless they have a scripting dictionary. It’s actually pretty snappy, and cached.