gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:57:55 27 4
gpt4 key购买 nike

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

最佳答案

补充@Bavarious' helpful answer :

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

任一功能仍可用作测试是否安装了应用程序;例如。:if ! whichapp 'someApp' &>/dev/null; then ... # not installed
这两个函数都不区分大小写,并且在指定名称时,.app后缀是可选的。
但是请注意,无法识别本地化名称。
whichapp
用于通过包 ID 或名称定位应用程序的功能。
如果找到,则返回应用程序的路径;否则,报错。

例子:

  • whichapp finder # -> '/System/Library/CoreServices/Finder.app/'
  • whichapp com.apple.finder # -> '/System/Library/CoreServices/Finder.app/'
  • bundleid
    给定应用程序的名称,返回其包 ID。

    例子:
  • bundleid finder # -> 'com.apple.finder'


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

    来源: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
    }

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

    来源: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; }
    }

    关于macos - 如何检查 Mac OS 中是否安装了特定的应用程序/软件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6682335/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com