Applescript : Getting List of Id's of Visible windows ( windows shown on the desktop ) I am trying to get the list of visible windows, i.e the windows which are shown on the desktop and not minimized.

So, far I have this script which gives me the correst list of processes which are running and have a windows associated with it. But I am not able to get the id's of visible windows associated with these processes.

Here's the script :

set visibleWindows to ""
set message to ""

tell application "System Events"
    set listOfProcesses to (name of every process where background only is false)
    repeat with visibleProcess in listOfProcesses
        try
            tell process visibleProcess to set visibleWindows to visibleWindows &amp; (id of windows whose visible is true)
        on error someError
            set message to "Some error occured :" &amp; someError
        end try
    end repeat
end tell

return {visibleWindows, listOfProcesses, message}


You can try this in script editor but its always giving a empty list.

Any help/suggestions would be much appreciated.
I modified your code, making the necessary changes for it to at least run (work) through the code to gather the information, while adding some additional code to handle the reporting of more then one message by having a separator between them. However, you can change it to something other then what I made it.

That said though, the output for the visibleWindows is just a string of numbers representing the concatenated id's, as that's how it's written to output. I'm not sure if that's the output you expected, however if the code as you wrote it had ran through, the outcome would have been the same for what's returned for visibleWindows.

on run
    set visibleWindows to ""
    set message to ""

    tell application "System Events"
        set listOfProcesses to (name of every process where background only is false)
    end tell

    repeat with visibleProcess in listOfProcesses
        try
            tell application visibleProcess to set visibleWindows to visibleWindows &amp; (id of windows whose visible is true)
        on error someError
            set message to message &amp; "Some error occurred: " &amp; someError &amp; "; "
        end try
    end repeat

    return {visibleWindows, listOfProcesses, message}
end run


In the code above, if you change set visibleWindows to "" to set visibleWindows to {}, then visibleWindows returns as a list of the id's not just a string of numbers.

If you also change:

tell application visibleProcess to set visibleWindows to visibleWindows &amp; (id of windows whose visible is true)


To:

tell application visibleProcess to set visibleWindows to visibleWindows &amp; visibleProcess &amp; (id of windows whose visible is true)


You get the application's name followed by a list of its window's id, so at least the data returned make more sense, then just a string of numbers or a list of id's not knowing which belongs to what.

The bottom line is, get the listOfProcesses separately and then let the application not the process get the id and do so outside of the "System Events" tell block. Every combination I tried while leaving the remaining code within the "System Events" tell block failed. So I moved the rest of it outside of it and changed tell process to tell application and it worked. Then I tweaked the code it a bit.