# Script to view the last opened time of apps/files on macOS

This document helps administrators fetch the last opened time of apps or files from macOS devices.

Administrators have to identify when apps or files were last accessed on the device for various reasons. Managing resource-intensive modern apps is crucial for optimizing system performance. A rise in the number of apps on the device can have significant impacts like system slowdown or increased boot times. Also, monitoring the app access behavior on the device makes it easier to draw usage patterns, especially during work hours. Based on the conclusions outlined, admins may choose to [uninstall](https://www.hexnode.com/mobile-device-management/help/script-to-uninstall-apps-on-mac/) unnecessary apps, freeing up space and improving efficiency. On the other hand, keeping track of the last opened time of files can contribute to basic device/user logging. Admins can execute the scripts given below to find out when an app was last opened using Hexnode UEM’s [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) action for Macs.

Scripting language – Bash

File extension – .sh

 Disclaimer: 
The sample scripts provided below are adapted from third-party open-source sites.

 

Fetch the timestamp when the app/file was last opened
-----------------------------------------------------

You can deploy the following script to fetch the timestamp of an app or file.

Script to view the last opened time of apps/files on macOS

\#!/bin/bash datetime=$(mdls "$1" -name kMDItemLastUsedDate | awk '{print $3,$4}') echo "'$1' was last opened on (UTC):" echo $datetime echo "'$1' was last opened on (Local Time):" echo $(date -jf "%Y-%m-%d %H:%M:%S %z" "$datetime +0000" +"%Y-%m-%d %H:%M:%S")

   1

2

3

4

5

6

  \#!/bin/bash

datetime=$(mdls "$1" -name kMDItemLastUsedDate | awk '{print $3,$4}')

echo "'$1' was last opened on (UTC):"

echo $datetime 

echo "'$1' was last opened on (Local Time):"

echo $(date -jf "%Y-%m-%d %H:%M:%S %z" "$datetime +0000" +"%Y-%m-%d %H:%M:%S")

   

 

  

As an argument in the *Arguments* field while deploying the script, provide the [file path](https://www.hexnode.com/mobile-device-management/help/script-to-find-file-path-on-mac/) for the app or file whose last opened timestamp you want to fetch.

[![Specify app path as an argument as you execute the script to view the last opened time of a macOS app](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/06/Provide-app-path-as-arguments-to-verify-the-last-opened-time-of-a-macOS-app.png "Provide app path as arguments to verify the last opened time of a macOS app")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/06/Provide-app-path-as-arguments-to-verify-the-last-opened-time-of-a-macOS-app.png)

The output obtained provides the last opened timestamp, both according to UTC and the device’s local time.

[![Find the last opened time of macOS apps from the Output tab after script execution](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/06/The-last-opened-time-of-macOS-apps-is-shown-in-the-Output-tab.png "The last opened time of macOS apps is shown in the Output tab")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/06/The-last-opened-time-of-macOS-apps-is-shown-in-the-Output-tab.png)

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.