macos 如何检查 Mac OS 中是否安装了特定的应用程序/软件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6682335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:10:03  来源:igfitidea点击:

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

macosinstallation

提问by batwadi

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.

我想使用 Perl/Shell 脚本检查特定应用程序是否安装在 Mac OS 中。我正在使用 PackageMaker 编写包,在安装应用程序之前我需要检查用户机器上的几个应用程序。所以我打算写一个脚本来帮我检查这个。请建议我是否可以以更好的方式执行此操作。

回答by mklement0

To complement @Bavarious' helpful answer:

补充@Bvarious'有用的答案

Here are generic bashfunctionsthat 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.

以下是通过返回应用程序的路径或其包 ID(如果已安装)来扩展测试应用程序是否已安装的通用bash函数。如果您将它们放在您的 bash 配置文件中,它们也可能会在交互式使用中派上用场。

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

任一功能仍可用作测试是否安装了应用程序;例如:
if ! whichapp 'someApp' &>/dev/null; then ... # not installed

Neither function is case-sensitive, and, when specifying a name, the .appsuffix is optional. Note, however, that localizednames are notrecognized.

这两个函数都不区分大小写,并且在指定name 时.app后缀是可选的。但是请注意,无法识别本地化名称。

whichapp

whichapp

A function for locating applications by eitherbundle ID orname. Returns the application's path, if found; otherwise, reports an error.

一种用于通过定位应用功能要么包ID名称。如果找到,则返回应用程序的路径;否则,报错。

Examples:

例子:

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

bundleid

bundleid

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

给定应用程序的名称,返回其包 ID。

Example:

例子:

  • bundleid finder # -> 'com.apple.finder'
  • 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 launchesthe targeted application, which is undesired.

实现说明:在 AppleScript 代码中,很容易绕过 Finder 上下文并在全局上下文中简单地使用 egapplication [id] <appNameOrBundleId>path to application [id] <appNameOrBundleId>,但问题是这总是会启动目标应用程序,这是不希望的。

source: whichapp

来源:whichapp

whichapp() {
  local appNameOrBundleId= 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 volumein a given sessionThanks, Wonder Dog., but since such applications aren't persistently installed (not persistently registered with the macOS Launch Services), a warningis issued in that event.
If desired, you can easily modify the function to report an error instead.

注意:该函数还查找在给定会话中从已安装卷启动的应用程序谢谢,Wonder Dog,但由于此类应用程序未永久安装(未永久注册到 macOS 启动服务),因此会在该事件中发出警告
如果需要,您可以轻松修改该函数以报告错误。

source: bundleid

来源:bundleid

bundleid() {
  osascript -e "id of application \"\"" 2>/dev/null || 
    { echo "$FUNCNAME: ERROR: Application with specified name not found: " 1>&2; return 1; }
}

回答by batwadi

The following bash script uses AppleScript to check if an application is installed, based on a solution by Michael Pilat:

以下 bash 脚本使用 AppleScript 根据Michael Pilat 的解决方案检查是否安装了应用程序:

#!/bin/bash

APPLESCRIPT=`cat <<EOF
on run argv
  try
    tell application "Finder"
      set appname to name of application file id ""
      return 1
    end tell
  on error err_msg number err_num
    return 0
  end try
end run
EOF`

retcode=`osascript -e "$APPLESCRIPT"`
exit $retcode

The script expects an application identifier (e.g. com.apple.preview). After executing the script, retcodecontains 1 if the application is installed and 0 if the application isn't installed. The script also returns retcode.

该脚本需要一个应用程序标识符(例如 com.apple.preview)。执行脚本后,retcode如果安装了应用程序,则包含 1,如果未安装应用程序,则包含 0。该脚本也返回retcode.

For example:

例如:

$ ./appinst.sh com.apple.preview
$ echo $?
1
$

or

或者

$ ./appinst.sh nonono
$ echo $?
0
$

回答by Andrew Vit

You can use the system_profilercommand for this. Try something like this:

您可以system_profiler为此使用该命令。尝试这样的事情:

system_profiler SPApplicationsDataType | grep AppName

回答by SuperFamousGuy

For the most part, all third party apps are installed within the Applications folder. The simplest way would just be to grep through that.

大多数情况下,所有第三方应用程序都安装在 Applications 文件夹中。最简单的方法就是通过 grep。

ls /Applications/ | grep -i APP_NAME

Just replace APP_NAME with whatever you're looking for. The "-i" makes the grep case insensitive so barring some major spelling errors if it's installed it'll list out the file. Otherwise it'll return nothing.

只需将 APP_NAME 替换为您要查找的任何内容。“-i”使 grep 不区分大小写,因此除非安装了一些主要的拼写错误,否则它会列出文件。否则它什么也不会返回。