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开发者_Go百科 better way.
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; }
}
The following bash script uses AppleScript to check if an application is installed, based on 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
The script expects an application identifier (e.g. com.apple.preview). After executing the script, retcode
contains 1 if the application is installed and 0 if the application isn’t installed. The script also returns retcode
.
For example:
$ ./appinst.sh com.apple.preview
$ echo $?
1
$
or
$ ./appinst.sh nonono
$ echo $?
0
$
You can use the system_profiler
command for this. Try something like this:
system_profiler SPApplicationsDataType | grep AppName
For the most part, all third party apps are installed within the Applications folder. The simplest way would just be to grep through that.
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.
You can use AppleScript to ask for the "ID" of the application. (But see the update below for a more reliable way!)
Here is an example that can be used in a Bash script to check if "QuickTime Player 7" is available:
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
The application name is not case-sensitive, so "quicktime player 7" also works.
If you want to use a Bash variable for the app name in your script, be aware that AppleScript requires double-quotes around the app name. So you would need to write something like this, escaping the double quotes:
app='QuickTime Player 7'
if osascript -e "id of application \"$app\"" >/dev/null 2>&1; then
#...
fi
Update:
However, as I discovered with this particular example which I was using, the application name may change! On a different machine, the same QT 7 app was named "QuickTime 7 Pro" instead of "QuickTime Player 7".
So the more reliable solution is to know the ID of the application, which should remain the same across different installs. Then you can check for the existence of that ID.
if osascript -e 'exists application id "com.apple.quicktimeplayer"' >/dev/null 2>&1; then
((debug)) && echo "OK: Quicktime 7 found" >&2
else
echo "ERROR: Cannot find Quicktime 7" >&2
fi
精彩评论