#!/usr/bin/env osascript (* Returns the bundle ID for the given apps, or an empty string if none is found. The apps can be a name, a path, or left empty to be prompted with a picker. For example: > ./appid.applescript "Visual Studio Code" > ./appid.applescript "/Users/benspaulding/Applications/BBEdit.app" > ./appid.applescript "com.apple.textedit" > ./appid.applescript Information is written to stderr and the id of each app (or an empty string if no app is found) is written to stdout, one ID per line, in the order the apps were given. *) on run(argv) -- To show a GUI alert of results if the user GUI-selects one. set prompted to false -- To store the bundle ID of each argument passed in. set appIDs to {} -- If no arguments were provided, prompt for one. if argv is equal to {} then log "No arguments provided -- prompting for input ..." set prompted to true set cTitle to "Select application(s) to see bundle ID" set cPrompt to "Select one or more apps for which to get a bundle ID:" set argv to choose application with title cTitle with prompt cPrompt ¬ with multiple selections allowed without importing end if -- Iterate on input to try to find a bundle ID for each item. repeat with appIsh in argv set appId to "" try set appId to getAppId(appIsh) end try set end of appIDs to appId set msg to ¬ "Given: " & appIsh & linefeed & ¬ "Found: " & appId & linefeed log msg if prompted then display alert msg end repeat set text item delimiters of AppleScript to {linefeed} return appIDs as text end run on getAppId(appIsh) -- appIsh may be an application object, ID, name, or path. -- We want to look up by any of the above criteria without prompting the -- user to find an application we cannot, or opening apps that we check. -- All thanks to https://stackoverflow.com/a/40346045, the way to do it -- is by running a shell script that calls AppleScript. Not intuitive, but -- the only way I have found that works. set appId to do shell script ¬ "osascript -e " & quoted form of ("id of application id " & quote & appIsh & quote) & " || " &¬ "osascript -e " & quoted form of ("id of application " & quote & appIsh & quote) & " || :" if (appId is not "") then return appId else -- Raise a reasonable error for this situation. return application id appIsh end if end getAppId