Category filter
Script to get versions of installed apps on macOS
In this document, you will learn how to create and deploy a script to get the versions of apps installed on macOS devices.
The application version helps users, developers, and support teams identify and differentiate between various releases of the same software. Identifying the currently running version of the application ensures that the applications are running according to organizational requirements. Retrieving an application version can be easy when the devices are readily available to administrators. However, in situations where the devices are remote, it can be more challenging. For macOS devices, you can retrieve the application versions of the installed apps remotely using a custom script. IT administrators can use Hexnode’s Execute Custom Script remote action to deploy the following script to get versions of installed apps on macOS devices.
Scripting language – Bash
File extension – .sh
Retrieving the application versions
The following script uses the mdls command to print the values of all the metadata attributes related to the specified applications. Then the script retrieves the kMDItemVersion attribute using mdls command, where the application version is stored. The script retrieves a list of specified applications with their names and versions as output. If an application version cannot be retrieved, the script returns an error message: ‘Unable to retrieve version for the specific application.’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#!/bin/bash applications=("Application1" "Application2" "Application3") heading_printed=false for app in "${applications[@]}"; do version=$(sudo -u Username mdls -name kMDItemVersion "/Applications/${app}.app" 2>/dev/null | grep -o '".*"' | sed 's/"//g') if [ -z "$version" ]; then echo "Unable to retrieve version for ${app}" else if [ "$heading_printed" = false ]; then echo "Application Versions:" heading_printed=true fi echo "${app}: ${version}" fi done |
Before executing the script, make sure to replace Application1, Application2, and Application3 with the required application names. You can specify as many applications as needed. Replace Username with the account name of the user under which the specified applications are installed on the macOS device.
Upon successful script execution, the installed application versions will be retrieved and displayed in the Hexnode console. To view the output, navigate to the device details page by going to Manage > Devices and selecting your device. Then, click on Show Output for the corresponding action in the Action History tab.