Getting the Name of the Frontmost App on macOS Using Shortcuts
TIPS
Tips and tricks to master your apps and be more productive.
Getting the Name of the Frontmost App on macOS Using Shortcuts
I was recently debugging one of my old shortcuts that integrated with macOS to get the name of the frontmost app using AppleScript and noticed that, at some point over the past two years, that script had stopped working. I tried fixing the AppleScript myself, but after five minutes spent trying random and outdated code snippets found on StackOverflow, it hit me: there was probably a way to do this using native Shortcuts actions, without having to rely on old scripting techniques at all. And I was right.
This realization goes to show that, sometimes, it’s useful not to think about Mac automation within the context of old scripting languages and legacy integrations. On the Mac, Shortcuts offers a ‘Find Windows’ action that returns a list of all the windows currently open on the system. This action contains some filters, too: specifically, there’s a way to sort windows by their window index and limit results to a specific number of windows. I remembered from my AppleScript days that the frontmost window has an index of 1
, so I figured that if I sorted windows by index with the smallest value first then limited results to one window only, I’d be able to get the Window
variable for the window in front of me.