我想使用Perl / Shell脚本检查Mac OS中是否安装了特定的应用程序。 我正在使用PackageMaker编写包,在安装应用程序之前,我需要在几个应用程序中检查用户计算机。 所以我打算写一个脚本来为我检查这个。 如果我能以更好的方式执行此操作,请提供建议。
答案 0 :(得分:11)
以下是通用bash
函数,通过返回应用程序的路径或其捆绑ID(如果已安装)来扩展测试是否安装了应用程序。如果将它们放在bash配置文件中,它们也可以派上用场进行交互式使用。
这两种功能都可以用作测试是否安装了应用程序;例如: -
if ! whichapp 'someApp' &>/dev/null; then ... # not installed
这两个函数都不区分大小写,并且在指定名称时,.app
后缀是可选的。
但请注意,本地化的名称未被识别。
whichapp
通过 捆绑包或名称来定位应用程序的功能。 返回应用程序的路径(如果找到);否则,报告错误。
示例:
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() {
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
}
注意:该函数还会在给定的会话中找到从挂载的卷启动的应用程序谢谢,Wonder Dog。,但是由于此类应用程序未持久安装(未在macOS Launch Services中持续注册),因此在该事件中会发出警告。 如果需要,您可以轻松修改函数以报告错误。
bundleid() {
osascript -e "id of application \"$1\"" 2>/dev/null ||
{ echo "$FUNCNAME: ERROR: Application with specified name not found: $1" 1>&2; return 1; }
}
答案 1 :(得分:7)
以下bash脚本使用AppleScript根据a solution by Michael Pilat检查是否安装了应用程序:
#!/bin/bash
APPLESCRIPT=`cat <<EOF
on run argv
try
tell application "Finder"
set appname to name of application file id "$1"
return 1
end tell
on error err_msg number err_num
return 0
end try
end run
EOF`
retcode=`osascript -e "$APPLESCRIPT"`
exit $retcode
脚本需要一个应用程序标识符(例如com.apple.preview)。执行脚本后,如果安装了应用程序,retcode
包含1,如果未安装应用程序,则包含0。该脚本还会返回retcode
。
例如:
$ ./appinst.sh com.apple.preview
$ echo $?
1
$
或
$ ./appinst.sh nonono
$ echo $?
0
$
答案 2 :(得分:4)
您可以使用system_profiler
命令。尝试这样的事情:
system_profiler SPApplicationsDataType | grep AppName
答案 3 :(得分:0)
在大多数情况下,所有第三方应用程序都安装在Applications文件夹中。最简单的方法就是通过这个方式。
ls /Applications/ | grep -i APP_NAME
只需将APP_NAME替换为您要查找的内容即可。 “-i”使得grep大小写不敏感,因此如果安装它会禁止一些主要的拼写错误,它会列出文件。否则它什么也不会返回。
答案 4 :(得分:0)
我找到的最简单的解决方案是使用 AppleScript 来询问应用程序的“ID”。
这是一个可以在 Bash 脚本中使用以检查“QuickTime Player 7”是否可用的示例:
package.json
应用程序名称不区分大小写,因此“quicktime player 7”也可以使用。
但是,如果您想在脚本中为应用程序名称使用 Bash 变量,请注意 AppleScript 需要在应用程序名称周围使用双引号。所以你需要写这样的东西,转义双引号:
if osascript -e 'id of application "QuickTime Player 7"' >/dev/null 2>&1; then
echo "Yes, QT 7 is available"
else
echo "No, QT 7 not found"
fi