SCRIPT: Get List of All Window Names of "Windowed" Apps using JXA

UPDATED: 2018-11-16 16:15 GMT-6

Use Case

  • Get a List of All Window Names in All "Windowed" Running Apps
    (those apps that are running in the GUI front end of the macOS, and show in the Dock when running)
    • Does NOT include windows for background only apps (like those in the Apple menu)
  • Show How to use JXA
  • Demo using a couple of interesting, powerful JavaScript functions
    • reduce() -- to flatten an array
    • filter() -- to remove unwanted elements of an array
var ptyScriptName   = "Get List of All Window Names of Windowed Apps"
var ptyScriptVer     = "1.2"  //  Revise title
var ptyScriptDate   = "2018-11-16"
var ptyScriptAuthor = "JMichaelTX"


'use strict';
(function myMain() {      // this will auto-run when script is executed

/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
RETURNS:  CSV List of Window Names

REQUIRED:
  1.  macOS 10.11.6+

TAGS:  @Lang.JXA @CAT.Windows @CAT.UI @type.Example @Auth.JMichaelTX

REF:  The following were used in some way in the writing of this script.

  1.  2018-11-03, JMichaelTX, Stack Overflow
      osascript is very slower than Script Editor
      https://stackoverflow.com/a/53136902/915019

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

var winList = Application("System Events").processes.whose(
    {backgroundOnly: {'=': false} }).windows.name();

// REF: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

//--- Flatten 2D Array into 1D ---
var winList2 = winList.reduce(
        function(accumulator, currentValue) {
          return accumulator.concat(currentValue);
        },
        []
      );

//--- Remove Windows with No Title ---
winList2 = winList2.filter(e => (e !== ""));

//--- Create CSV List of Window Names ---
//    (use .join('\n') if you'd prefer each name on separate line)
var scriptResults = winList2.join(',')

return scriptResults;

}  // END of function myMain()
)();  // autorun

Questions?

1 Like

concatMap is another useful and fundamental abstraction, which (like both map and filter) can be defined in terms of reduce:

(() => {
    'use strict';

    const main = () =>
        concatMap(filter(s => 0 < s.length))(
            Application('System Events').processes.whose({
                backgroundOnly: false
            }).windows.name()
        ).join('\n')


    // GENERIC --------------------------------------------
    // https://github.com/RobTrew/prelude-jxa

    // ( 'Curried' versions here, all in terms of reduce )

    // concatMap :: (a -> [b]) -> [a] -> [b]
    const concatMap = f => xs =>
        xs.reduce((a, x) => a.concat(f(x)), []);


    // filter :: (a -> Bool) -> [a] -> [a]
    const filter = p => xs =>
        xs.reduce((a, x) => p(x) ? a.concat(x) : a, []);

    // For reference, not used here:

    // map :: (a -> b) -> [a] -> [b]
    const map = f => xs =>
        xs.reduceRight((a, x) => [f(x)].concat(a), []);

    return main();
})();

3 Likes

Incidentally, we could simplify exclusion of empty strings (nameless windows) by writing:

const main = () =>
    concatMap(filter(Boolean))(
        Application('System Events').processes.where({
            backgroundOnly: false
        }).windows.name()
    ).join('\n')

@JMichaelTX, I might have my brain switched off today, but what purpose does filtering by backgroundOnly: false serve that its omission wouldn't ?

This whose clause eliminates all processes that don't have a UI, and thus would not have any windows, which makes the script run faster.

To clarify, this script:

Application("System Events").processes
                            .whose({
                                 backgroundOnly: false
                            }).windows.name();

is demonstrably and noticeably quicker to execute than this script ?:

Application("System Events").processes.windows.name();

I'm unable to convince myself this is the case on my system, but perhaps I need to open many, many more windows.

One reason I bring this up is that backgroundOnly, whilst perhaps an efficient filter, is not consistently the most reliable for targeting windowed processes, having a tendency to report false negatives for some applications that live in the menu bar. Examples of this include SnippetsLab, Flume, Typinator, and others, which don't cease to be reported as being backgroundOnly even when they are brought into the foreground for interaction.

Omitting the filter does manage to catch these programs' windows when they are visible, although I noticed one false positive report for SnippetsLab whose window had been closed, but remained in the list for a period of time.

Here, FWIW, it does seem to be worthwhile – seems to eliminate about 80% of the run-time needed on this system. Perhaps I have a lot of background processes ...

(based on a few runs with an unsophisticated form of testing):

carbon-2

and using this definition of main(), alternately with and without the where clause:

carbon-3

1 Like

Remember the objective here was to get the name of all open windows.
I believe for that purpose it does a good job. If you believe the results are more accurate not using it, and you don't mind (or don't see) the extra execution time, then don't use it.

Which I believe it doesn’t achieve in instances of menu bar apps that have open windows, yet are registered as background only nonetheless, e.g. Typinator, SnippetsLab, ...

1 Like

So if you know you might have those type of windows, then don't use the "background only" clause.

I was providing feedback, not stuck in an "if...then...else" dilemma wondering how to get out.

Got this to work fine, and it shows me all windows EXCEPT for the Scrivener application. What should I do now?

I have no idea since I do NOT have the Scrivener app -- thus no ability to test and inspect.

Unless you have a specific need to use JXA, I'd use AppleScript with the Script Debugger 7 app to develop and test.

Do some searching on the forum and you will find other tools that display all windows.

Mea culpa. This was my fault, as it is working OK now.

How to get an app with full window set?

What do you mean?