来补充@Bvarious' 有用的答案 https://stackoverflow.com/a/8024765/45375:
这里有generic bash
功能通过返回应用程序的路径或其捆绑 ID(如果已安装)来扩展测试应用程序是否已安装。如果您将它们放在 bash 配置文件中,它们也可能会在交互式使用中派上用场。
这两个功能仍然可以用来测试应用程序是否已安装;例如。:
if ! whichapp 'someApp' &>/dev/null; then ... # not installed
这两个函数都不区分大小写,并且当指定name, the .app
后缀是可选的。
但请注意,本地化的名字是not认可。
whichapp
通过以下方式定位应用程序的功能either捆绑包 IDor姓名。
如果找到,则返回应用程序的路径;否则报错。
例子:
-
whichapp finder # -> '/System/Library/CoreServices/Finder.app/'
-
whichapp com.apple.finder # -> '/System/Library/CoreServices/Finder.app/'
bundleid
给定应用程序名称,返回其包 ID。
Example:
bundleid finder # -> 'com.apple.finder'
实施注意事项:在 AppleScript 代码中,人们很容易绕过 Finder 上下文并简单地使用例如application [id] <appNameOrBundleId>
and path to application [id] <appNameOrBundleId>
在全球范围内,但问题是,总是launches目标应用程序,这是不希望的。
来源: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 https://stackoverflow.com/users/2498116/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.
来源: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; }
}