List The Foreground Apps Running On A Mac With JavaScript
Code
#!/usr/bin/env osascript -l JavaScript
const sysEvents = Application("System Events")
const allProcesses = sysEvents.processes
const foregroundProcesses = allProcesses.whose({"backgroundOnly": {'=': false }})
for (let pIndex = 0; pIndex < foregroundProcesses.length; pIndex ++ ) {
console.log(
`${pIndex} - ` +
`${foregroundProcesses[pIndex].name()} - ` +
`${foregroundProcesses[pIndex].displayedName()}`
)
}
Results
0 - Finder - Finder 1 - Google Chrome - Google Chrome 2 - sublime_text - Sublime Text 3 - GitHub Desktop - GitHub Desktop 4 - Vivaldi - Vivaldi 5 - Keychain Access - Keychain Access 6 - Soulver 3 - Soulver 3 7 - Stickies - Stickies 8 - iTerm2 - iTerm2 9 - Loopback - Loopback 10 - Activity Monitor - Activity Monitor 11 - Terminal - Terminal 12 - Electron - Code 13 - Spotify - Spotify 14 - TextEdit - TextEdit 15 - Adobe Photoshop 2023 - Adobe Photoshop 2023 16 - Safari - Safari 17 - firefox - Firefox
Notes
-
Some display names are different from the internally used names. For example, "Electron" and "Code"