0

Using Javascript for Automation (JXA) I want to identify the window the user has just clicked on. In other words, I want to identify the window that has just come to the front. How can I do that?

So far, I identify the front application, and try to get the window of that application which is in focus, like so:

var frontAppName = Application("System Events").processes.whose({frontmost: {'=': true }})[0].name();  
var frontApp = Application(frontAppName); 
var theCurrentWindow = frontApp.windows[0]

This successfully identifies the application in focus, but frontApp.windows[0] is not quite right, because it is tied to the first window the application originally opened, whether or not that window is in front. (In applescript, "window 1" would probably work, but I'm hoping for a JXA solution.)

So, if I have several windows opened in an application, I need a way to identify which one I am clicking on. How can I do that?

SeanRtS
  • 1,005
  • 1
  • 13
  • 31

1 Answers1

0

It is known bug of JXA. You can solve issue using abilities of AppleScript execution from JXA code. Following script will return front window of frontmost application:

(() => {
    'use strict';

    // evalAS :: String -> IO String
    const evalAS = s => {
        const
            a = Application.currentApplication(),
            sa = (a.includeStandardAdditions = true, a);
        return sa.doShellScript(
            ['osascript -l AppleScript <<OSA_END 2>/dev/null']
            .concat([s])
            .concat('OSA_END')
            .join('\n')
        );
    };

    var frontAppName = Application("System Events").processes.whose({frontmost: {'=': true }})[0].name();  
    var frontApp = Application(frontAppName);
    return evalAS('tell application \"' + frontAppName + '\" to front window');

})();
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
  • Thanks for your reply. I wasn't familiar before with "AppleScript execution from JXA code." Does that mean I can just run any applescript I want inside of JXA? That would be great. – SeanRtS Jun 02 '21 at 16:08
  • Your code works in my script editor. I'm also trying to get it to work with nodejs: https://github.com/sindresorhus/run-jxa normally works to allow jxa code to run, but the evalAS statement at the end in this code returns an error: 'SyntaxError: Unexpected EOF (-2700)'. – SeanRtS Jun 02 '21 at 20:34
  • 1
    Hi, @9gt53wS, you asked if you can just run any applescript you want inside of JXA. My answer is why not. – Robert Kniazidis Jun 03 '21 at 06:10
  • 1
    Now, about run-jxa. Unfortunately, I am not familiar with this third-party software product at all, especially since I do not know its features. You'd better ask the author of the product why the code executed in the Script Editor is not being executed with his application. – Robert Kniazidis Jun 03 '21 at 06:18
  • 1
    @9gt53wS, it's not a bug in JXA, it's how JavaScript works. `frontApp = Application(frontAppName)` assigns to `frontApp` a snapshot of the object `Application(frontAppName)`. `windows` is a property, not a getter function, so it will only enumerate the list of windows at the point of assignment. If you are looking for deferred evaluation (which you are), then take your original code and wrap it in a function that you can call at whatever point in time you need to obtain the currently active window. That's why the "solution" above works, but it contains a load of garbage you don't need. – CJK Jul 27 '21 at 05:11