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 & (id of windows whose visible is true)
on error someError
set message to "Some error occured :" & 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.
Best Answer
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 concatenatedid
'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 forvisibleWindows
.In the code above, if you change
set visibleWindows to ""
toset visibleWindows to {}
, thenvisibleWindows
returns as a list of theid
's not just a string of numbers.If you also change:
To:
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 ofid
's not knowing which belongs to what.The bottom line is, get the
listOfProcesses
separately and then let the application not the process get theid
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 changedtell process
totell application
and it worked. Then I tweaked the code it a bit.