Haven't read the entire thread, so perhaps you've obtained a solution already. If not, the JXA (JavaScript for Automation) code snippet below should—barring any idiosyncratic anomalies depending on the specific version of macOS you're using—return an array of strings, where each string has the following format:
"[${z-index}|${application-name}|${window-id}]${window-title}"
${z-index}
: the 1-based array index of the string item.
${application-name}
: the name of the application to which the window belongs. This may or may not have ".app"
affixed at the end.
${window-id}
: the ID number of the window. This is a unique, persistent value assigned for the lifetime of the window. If the app is (Apple)scriptable, this value will typically match the value stored by the id
property of the equivalent window
class object belonging to the AppleScript application
element representing the scriptable app.
${window-title}
: the title of the window. If the window has a titlebar, this is what will be displayed. However, a window without a titlebar will often still have a title.
Each of these strings pertains to a single, on-screen window. For example, from the array returned for me, the following string (excluding the bold/italicised formatting) represents the window into which I am currently typing:
"[2|Arc|1937]Is it Possible to Identify the *Second* Window in a Desktop/Space When it is in a Different Application from the frontmost App? - Questions & Suggestions - Keyboard Maestro Discourse"
From this, you can see that I'm using the Arc browser, and the title of the current tab is that of this post. Arc is one app where the window number doesn't correlate with an AppleScript id
for any tab
or window
object, although Arc's AppleScripting implementation contains many badly-coded object references, so this may be simply be one among them. You can also see that this string item is the second item that appears in the returned array, the first being this:
"[1|Script Editor|6972]Untitled 242.scpt"
This reflects the fact that I am executing the JXA code below from within Script Editor, having clicked away from this browser window and into the specific Script Editor document containing the code for testing. That is to say, when I execute the code, the Script Editor window is at the front, and this Arc window sits just behind, hence the returned array contains the Script Editor window as its first element, and this Arc window as its second element.
The array returned by the code below reflects the z-ordered arrangement of on-screen windows, ordered front-to-back. Thus, the %{z-index}
represents the positional index of the item in the array as well as the position in the front-to-back ordering of all on-screen windows (which is equivalent to the order of most recently-active):
((O = "") => {
ObjC.import('CoreGraphics');
Ref.prototype.deref = function(Ω = this){
try { Ω = ObjC.castRefToObject(this); }
finally { return ObjC.deepUnwrap(Ω); }
}
let Wᵢ = $.CGWindowListCopyWindowInfo(
$.kCGWindowListOptionOnScreenOnly,
$.kCGNullWindowID).deref().filter(
(_w) => _w.kCGWindowLayer==0).map(
(_w, index) => `[${ 1 + index }|${
_w.kCGWindowOwnerName }|${
_w.kCGWindowNumber }]${
_w.kCGWindowName }`);
return O ? Wᵢ : Wᵢ.join('\n');
})([]);
Bear in mind that it is a JavaScript array that is returned. If you'd prefer it to return a unary string value, with each array item placed on a new line, then replace in the final line the pair of square brackets ([]
) with a pair of double quotes (""
). This will produce a string similar to the following:
"[1|Script Editor|6972]Untitled 242.scpt
[2|Arc|1937]Is it Possible to Identify the *Second* Window in a Desktop/Space When it is in a Different Application from the frontmost App? - Questions & Suggestions - Keyboard Maestro Discourse
[3|Music|5968]Music
[4|iTerm.app|247]-fish — /dev/ttys004
[5|Script Editor|6908]Library
[6|Shortcuts|6644]QuickLook Contents of Folder
[7|Finder|5602]osa
[8|Finder|4271]Pictures
[9|Shortcuts|6313]All Shortcuts
[10|iTerm.app|248]~/#/o/ascr
[11|Session|6269]Session
[12|VLC|5606]VLC media player
[13|Usenapp|5101]
[14|iTerm.app|246]~/L/M/c/#/o/ascr
[15|Telegram|4652]Telegram
[16|Signal|4651]Signal
[17|Terminal.app|195]~ — ttys000 — 60×41"
System version: 12.6.3
(Monterey)