How do I list all window titles of running programs in OS X?

bash, macos, operating-system, shell, user-interface

Solution

Probably the simplest way of doing this "with no additional downloads" is to use AppleScript:

tell application "System Events"
    get name of every window of every process
end tell

Since the Applescript syntax is pretty obtuse, the equivalent Javascript is:

var SE = new Application("System Events");
SE.processes.windows.name()

This will return a structure of the form:

[[], [], ["Stack Overflow"], ["iTunes", "MiniPlayer"], ...]

where each array entry represents one running application, and each string within those arrays represents one window. Empty arrays indicate applications with no open windows.

(Note that this Javascript must be run in Script Editor, not in a web browser. Components of the Scripting Bridge, including System Events, are not available from web browsers.)

Problem

I know how to list all window titles in Windows, for example using Python via the Win32 API. Or eventually I could write it in C/C++ directly. How do I accomplish this for Mac OS X? It doesn't necessarily have to be in Python, and it doesn't have to be cross-platform. Preferably it would run without requiring any additional downloads (like an applescript file or a bash file using included commands only), but that's not required.

Original source