Applescript : Getting List of Id's of Visible windows ( windows shown on the desktop )

Solution 1:

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 & (id of windows whose visible is true)
        on error someError
            set message to message & "Some error occurred: " & someError & "; "
        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 & (id of windows whose visible is true)

To:

tell application visibleProcess to set visibleWindows to visibleWindows & visibleProcess & (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.