8

I'm using Übersicht and I've created a custom menubar. I want to show the title of the current active app but my search-fu isn't great. I must be searching for it incorrectly so I figured I'd ask here.

Does such a command exist?

1
  • If you're in Terminal, then Terminal is the frontmost app...
    – Tetsujin
    Commented May 12, 2017 at 19:35

2 Answers 2

9

Well there is, however if you run it directly in Terminal it's just going to return Terminal, as that's the frontmost application process that's visible.

osascript -e 'tell application "System Events" to get name of application processes whose frontmost is true and visible is true'

If you want the second most, use:

osascript -e 'tell application "System Events" to get name of second application process whose visible is true'
4
  • Your answer, my comment - seconds apart ;-) I do have an answer here somewhere that finds the 'second frontmost' by Applescript - I'll have to try find it...
    – Tetsujin
    Commented May 12, 2017 at 19:36
  • 1
    Yeah, I was going to write it as a comment however if he's calling it in a script that's running in the background then it might have some use. :) Commented May 12, 2017 at 19:37
  • Yup, it works perfectly. Awesome! Commented May 12, 2017 at 19:40
  • We are trying to use this in some MDM automations/remediations and so the scripts are running as root. We're hitting Execution error: Not authorized to send Apple events to System Events. (-1743). Is there any way to get the frontmost app without using osacript/System Events?
    – luckman212
    Commented Aug 17, 2023 at 18:00
1

To get the active application:

➜ osascript -e 'tell application "System Events" to tell (first process whose frontmost is true) to return name'

To get the active window's title:

➜ osascript -e 'tell application "System Events" to tell (first process whose frontmost is true) to return name of window 1'

And to get both (to avoid a race condition where the user changes windows between the two calls):

➜ osascript -e 'tell application "System Events" to tell (first process whose frontmost is true) to return {name, name of window 1}'

Source: https://forum.keyboardmaestro.com/t/how-do-i-get-the-name-of-the-frontmost-window/2711/2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .