0

I want to get the foreground window title in macOS.

I have tried using AppleScript for this, it works but is very slow.

Here is the AppleScript code:

tell application "System Events"
    set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
    if the (count of windows) is not 0 then
        set window_name to name of front window
    end if
end tell

It takes a lot of time when we run this using Java.

Is there any other efficient solution to this?

user3439894
  • 7,266
  • 3
  • 17
  • 28
Nirman
  • 103
  • 2
  • 11
  • The first thing to be aware of is that your code is only going to work if the front application happens to be scriptable. If you want it to work for applications regardless of scriptability, then: `tell application "System Events" to tell process 1 whose frontmost = true to tell window 1 to if (exists) then return the name`. Let me know how performant that is (it should be faster, but by how much, I don't know), and I can go over what non-AppleScript methods I'm aware of that allow one to retrieve this information. – CJK Jul 25 '19 at 14:41
  • I am running this AppleScript through a java application, in which this processing takes more than 2 sec, which is very inefficient, that's why I needed some better solution for this, if we run appleScript through Script Editor it doesnt take that long to return the output, but through java, it takes a lot more time. – Nirman Jul 26 '19 at 17:50

2 Answers2

0

I haven't used Java in ages, so I don't remember how it accesses frameworks (assuming I ever knew), but the framework you want is Quartz Window Services. These methods give you access to the window server, which manages all of the onscreen windows.

Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17
0

This AppleScript code works for me using the latest version of macOS Mojave.

For me, this AppleScript code is lightning fast.

try
    tell application "System Events" to tell (process 1 whose it is frontmost) ¬
        to tell (window 1 whose value of attribute "AXMain" is true) ¬
        to set windowTitle to value of attribute "AXTitle"
end try
wch1zpink
  • 3,026
  • 1
  • 8
  • 19
  • There was no appreciable difference in speed between the AppleScript code in the OP and your answer when run from Script Editor. There may be some difference when run from Java, although doubtful; however, the OP is asking for a way **other then** AppleScript, not an AppleScript answer. – user3439894 Jul 26 '19 at 01:22
  • You should **try** to get out of the habit of using these superfluous `try...end try` blocks. It's unnecessary, and makes it obvious when someone isn't thinking about the how and when a script will go wrong, which will lead to them going wrong more often. On a minor point of curiosity, I'm wondering why after constructing a multi-level one-liner `tell` command, one would then decide that `set windowTitle to the result` specifically needed its own line, rather than `...to set windowTitle to the value of attribute "AXTitle"`; or simply omitting the `set...` altogether, as it's also unnecessary. – CJK Jul 26 '19 at 11:37
  • @CJK as much as I aspire to achieving the same level of the coding skills in which you possess, there are times in which you are just a little too quick to constructively criticize my code. I don't believe I haphazardly insert `try` blocks where they are not needed. After testing my solution with `if window 1 exists then...` and `if the (count of windows) is not 0 then...`, I stumbled across a situation where the app in question was frontmost, was visible, and window 1 existed but still threw an error. I did figure out what the problem was and how to prevent it from happening. – wch1zpink Jul 26 '19 at 17:53
  • continued... After about an hour of different failed attempts to add the solution to the code, I reluctantly added the `try` block. However, you are correct, I did drop the ball by setting the variable in an additional line of code. – wch1zpink Jul 26 '19 at 17:53