Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can check if particular application/software is installed in Mac OS

I want to check if particular application is installed in Mac OS using Perl/Shell scripts. I am writing package using PackageMaker in which i need to check user machine for few applications before installing the application. So am planning to write a script that will check this for me. Please advice if I can perform this in better way.

like image 279
batwadi Avatar asked Jul 13 '11 16:07

batwadi


People also ask

How do I know if an application is installed on my Mac?

To see the apps available on your Mac, click the Launchpad icon in the Dock. To open an app, click its icon in the Dock or use Launchpad. Tip: Every app that comes with your Mac includes built-in help. To learn how to use an app, open the app, then choose Help in the menu bar.


1 Answers

To complement @Bavarious' helpful answer:

Here are generic bash functions that expand on testing just whether an application is installed by returning either an application's path or its bundle ID, if installed. If you place them in your bash profile, they may come in handy for interactive use, too.

Either function can still also be used as a test for whether an application is installed; e.g.:
if ! whichapp 'someApp' &>/dev/null; then ... # not installed

Neither function is case-sensitive, and, when specifying a name, the .app suffix is optional. Note, however, that localized names are not recognized.

whichapp

A function for locating applications by either bundle ID or name. Returns the application's path, if found; otherwise, reports an error.

Examples:

  • whichapp finder # -> '/System/Library/CoreServices/Finder.app/'
  • whichapp com.apple.finder # -> '/System/Library/CoreServices/Finder.app/'

bundleid

Given an application's name, returns its bundle ID.

Example:

  • bundleid finder # -> 'com.apple.finder'

Implementation note: In the AppleScript code, it's tempting to bypass the Finder context and simply use e.g. application [id] <appNameOrBundleId> and path to application [id] <appNameOrBundleId> in the global context, but the problem is that that invariably launches the targeted application, which is undesired.

source: whichapp

whichapp() {
  local appNameOrBundleId=$1 isAppName=0 bundleId
  # Determine whether an app *name* or *bundle ID* was specified.
  [[ $appNameOrBundleId =~ \.[aA][pP][pP]$ || $appNameOrBundleId =~ ^[^.]+$ ]] && isAppName=1
  if (( isAppName )); then # an application NAME was specified
    # Translate to a bundle ID first.
    bundleId=$(osascript -e "id of application \"$appNameOrBundleId\"" 2>/dev/null) ||
      { echo "$FUNCNAME: ERROR: Application with specified name not found: $appNameOrBundleId" 1>&2; return 1; }
  else # a BUNDLE ID was specified
    bundleId=$appNameOrBundleId
  fi
    # Let AppleScript determine the full bundle path.
  fullPath=$(osascript -e "tell application \"Finder\" to POSIX path of (get application file id \"$bundleId\" as alias)" 2>/dev/null ||
    { echo "$FUNCNAME: ERROR: Application with specified bundle ID not found: $bundleId" 1>&2; return 1; })
  printf '%s\n' "$fullPath"
  # Warn about /Volumes/... paths, because applications launched from mounted
  # devices aren't persistently installed.
  if [[ $fullPath == /Volumes/* ]]; then
    echo "NOTE: Application is not persistently installed, due to being located on a mounted volume." >&2 
  fi
}

Note: The function also finds applications launched from a mounted volume in a given sessionThanks, Wonder Dog., but since such applications aren't persistently installed (not persistently registered with the macOS Launch Services), a warning is issued in that event.
If desired, you can easily modify the function to report an error instead.

source: bundleid

bundleid() {
  osascript -e "id of application \"$1\"" 2>/dev/null || 
    { echo "$FUNCNAME: ERROR: Application with specified name not found: $1" 1>&2; return 1; }
}
like image 151
mklement0 Avatar answered Oct 08 '22 14:10

mklement0