# Script to fetch the applications running in the foreground on Mac

App management not only includes installing the essential applications but also the monitoring and controlling of the applications run on the device. As part of the app monitoring, if the IT admin would want to monitor the foreground applications running on the employees’ devices from time to time, here’s a script that can be useful. Executing the script, especially while in active hours, lets the organization keep track of the apps running in the foreground during productive time. It may help figure out:

- the apps the user is interacting with
- the non-productive apps, if any, running on the device
- the apps that could be in the run most of the time or consuming too much data

Such app-based monitoring further enables the admin to take proactive measures by configuring app-based restrictions, screen time restrictions, data usage policies, etc.

To execute the script, you can use the [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) action from the Hexnode UEM console.

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

 

Fetching the list of applications
---------------------------------

You can fetch the list of all the applications running in the foreground on your Mac by running the script given below:

\#!/bin/bash osascript <<EOF tell application "Finder" set visibleProcesses to name of every process whose visible is true return visibleProcesses end tell EOF 

   1

2

3

4

5

6

7

8

  \#!/bin/bash 

osascript <<EOF 

tell application "Finder"

set visibleProcesses to name of every process whose visible is true

return visibleProcesses 

end tell 

EOF 

   

 

 `osascript EOF`

This line tells Bash to run the next lines as an AppleScript command.

`tell application "Finder"`

The tell command is called a tell block. This is the start of an AppleScript block that tells the Finder application to perform certain actions. Everything within the ***tell*** block is directed at the Finder application.

`set visibleProcesses to name of every process whose visible is true`

This line of AppleScript creates a variable named ***visibleProcesses***. The purpose of this variable is to store the names of applications that meet a specific condition. The condition ***whose visible*** is true segregates the currently running applications in the foreground. The ***visible*** property, which takes a Boolean value, indicates whether the application is currently displayed on the screen. When the ***visible*** property is ***true***, it means the application window is visible, and if it is ***false***, not visible. So, the variable ***visibleProcesses*** holds the list of names for applications meeting this visibility criterion.

`return visibleProcesses`

This line returns the list of visible applications obtained in the previous line.

`end tell`

This line marks the end of the ***tell*** block.

What happens at the device end? 
--------------------------------

When the script is executed from the portal, two prompts appear on the device:

**Prompt 1:**

![hexnodeagentd requesting access from Finder](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/02/Prompt-to-give-access-to-Finder.png "Prompt to give access to Finder")[](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/02/Prompt-to-give-access-to-Finder.png)

“*hexnodeagentd* (Hexnode Device Management Agent) is requesting permission to control the “Finder” application. Granting permission will allow access to documents and data within ‘Finder’ and enable actions within the app”.

**Prompt 2:**

![hexnodeagentd requesting access from System Events](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/02/Prompt-to-give-access-to-System-Events.png "Prompt to give access to System Events")[](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/02/Prompt-to-give-access-to-System-Events.png)

“*hexnodeagentd* is requesting permission to control “System Events”. Granting permission allows the agent to perform automated actions and gather information about system events”.

The end-user needs to click ‘OK’ on both prompts to grant the necessary permissions. These permissions enable Hexnode to effectively manage and monitor your device. Once granted, future executions of this script won’t necessitate re-approval of these permissions.

After allowing hexnodeagentd access to control Finder and System Events, the IT admin can fetch the list of applications currently running in the foreground from the ***Action History*** tab of the same device from the Hexnode console. (Navigate to ***Manage*** > the device to which the action is pushed > ***Action History***. Click on ***Show Output***.)

![fetch the list of applications opened in foreground](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/02/Fetch-the-list-of-application-in-the-Output.png "Fetch the list of application in the Output")[](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/02/Fetch-the-list-of-application-in-the-Output.png)

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