Skip to content

Instantly share code, notes, and snippets.

@JakubAndrysek
Last active April 16, 2024 19:50
Show Gist options
  • Save JakubAndrysek/2f4f4de69443fc0438ddb6320caf2349 to your computer and use it in GitHub Desktop.
Save JakubAndrysek/2f4f4de69443fc0438ddb6320caf2349 to your computer and use it in GitHub Desktop.
Get JSON of running apps + paths on macOS

Running Processes to JSON Script on MacOS

Description

This AppleScript retrieves a list of all running processes on a macOS system that are not background processes and converts this list into a JSON format. The JSON output includes the title and path of each application.

Usage

To use this script:

  1. Open Script Editor on your Mac.
  2. Paste the script into the editor.
  3. Run the script to get the JSON output of running applications.

Source

-- Get JSON of runnin apps + paths on macOS
-- Author: JakubAndrysek + (https://www.alfredforum.com/topic/11318-solved-how-to-load-an-applications-icon-with-applescript/?do=findComment&comment=59154)
-- Version: 1.0
-- License: MIT
-- Link: https://gist.github.com/JakubAndrysek/2f4f4de69443fc0438ddb6320caf2349
-- Description: This script lists all running non-background processes and their paths in JSON format.
tell application "System Events"
set runningProcesses to (every process whose background only is false)
end tell
set appsJSON to "{\"apps\": [\n"
repeat with i from 1 to count runningProcesses
set runningProcess to item i of runningProcesses
set appName to name of runningProcess
set appPath to POSIX path of (file of runningProcess as alias)
set appsJSON to appsJSON & "{ \"title\": \"" & appName & "\", \"path\": \"" & appPath & "\"}"
if i is not count runningProcesses then
set appsJSON to appsJSON & ",\n"
end if
end repeat
set appsJSON to appsJSON & "\n]}"
return appsJSON
{"apps": [
{ "title": "Spotify", "path": "/Applications/Spotify.app/"},
{ "title": "webstorm", "path": "/Users/kuba/Applications/WebStorm.app/"},
{ "title": "phpstorm", "path": "/Users/kuba/Applications/PhpStorm.app/"},
{ "title": "Google Chrome", "path": "/Applications/Google Chrome.app/"},
{ "title": "Finder", "path": "/System/Library/CoreServices/Finder.app/"}
]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment