How know what window is on focus?

Hi,

I'm developing a software which needs get list of all windows are open.

To this I'm using the follow script:

tell application "System Events"
set arr to {}
repeat with proc in application processes
tell proc
repeat with win in windows
set arr to arr & {{name of win, size of win, position of win}}
end repeat
end tell
end repeat

return arr

end tell

This script return me:
{{"files", {1483, 746}, {75, 158}}, {"sidneivl Info", {265, 824}, {10, 32}}, {"Apple - Support - Discussions - Post Message: New Topic", {1679, 972}, {0, 22}}, {"Skype™", {315, 822}, {1363, 22}}}

But I need know what window is on focus, what the property say me it?

I tried use frontmost, (set arr to arr & {{name of win, size of win, position of win, frontmost}})

but the front most set true for all windows from the same application, right???

How can I get what window is on top using my code?

I read this topic: http://discussions.apple.com/thread.jspa?messageID=10469903&#10469903
but I didn't understand...

well, if someone know how do it I will be glad...

Thanks

Macbook pro, Mac OS X (10.6.2)

Posted on Jan 11, 2010 2:57 PM

Reply
8 replies

Jan 11, 2010 4:34 PM in response to sidneivl

You're going to have a hard time here.

The issue is that there is no standard 'frontmost' property for a window which makes it difficult to query the app.

In your existing trial:

set arr to arr & {{name of win, size of win, position of win, frontmost}}


What you're getting is the frontmost of the application process, not the window (which is why it's always true). The rules of inheritence in AppleScript say that AppleScript will work its way up the 'tell' chain to find something that will respond to the property. Since 'window' doesn't have a 'frontmost' property the query is passed up to the app and that's where you get 'true'.

Your code would be technically valid if you specified the window:

set arr to arr & {{name of win, size of win, position of win, frontmost of win}}


in the same way you do with 'name', 'size' and 'position' (where, incidentally, '...of win' isn't needed), but that would rely on 'frontmost' being valid. Since it isn't in many apps, the code will fail.

Off-hand I can't see an easy workaround, but I'll think about it some more while I let others chime in.

Jan 11, 2010 6:30 PM in response to sidneivl

The code that you'll find in [this post of mine|http://discussions.apple.com/message.jspa?messageID=10471981#10471981] should display a list of all the visible open windows, from front to back. You just have to copy and paste the script into the AppleScript Editor and launch it. You could also add the script to the Script menu. Even if you don't understand the code, begin by seeing if the script does what your are asking for.

Message was edited by: Pierre L.

Jan 12, 2010 6:25 AM in response to Camelot

Hi Camelot

There aren't some property that indicate the window is on focus?

because if I set frontmost of win in my sentence i get the error:
error "System Events got an error: Can’t get frontmost of item 1 of every window of item 4 of every application process." number -1728 from frontmost of item 1 of every window of item 4 of every application process

It's so basic, should have some property for it...

Jan 12, 2010 6:30 AM in response to Pierre L.

Hi Pierre

I understand your code now, it is work, but not for me...
because my code needs return a array to my application with all windows and just set true or false to property that indicate if this window is on top.

Your solution is very good, but need refresh all windows and have a little delay, my application request this script each 1 minute, imagine all windows refreshing all the time, the user won't like use it, hehehehe

but ok, with your solution I'll think in someway to do it...

Thanks...

Jan 12, 2010 7:12 AM in response to sidneivl

Below is a script I wrote on Tiger some time ago. I don't have time right now to test it, so I can't be certain it will run on 10.5 or 10.6. Even if it does, I don't know whether it will give you useful information (for your purposes).

But try it. Let me know what you find.


tell application "System Events" to set VisApp_ to name of every application process whose visible is true
set VisAppCnt_ to count items of VisApp_
set VisAppWinNameList_ to ""
repeat with i_ from 1 to VisAppCnt_
set VisAppName_ to item i_ of VisApp_
try
set EWofVisAppName_ to name of every window of application VisAppName_
if EWofVisAppName_ is not {} then set EWofVisAppNameCnt_ to count items of EWofVisAppName_

repeat with i_ from 1 to EWofVisAppNameCnt_
set WinName_ to item i_ of EWofVisAppName_
if VisAppWinNameList_ does not contain VisAppName_ then set VisAppWinNameList_ to VisAppWinNameList_ & VisAppName_ & " (Window Names Below)" & return
if WinName_ is not "" then set VisAppWinNameList_ to VisAppWinNameList_ & tab & WinName_ & return
end repeat
end try
end repeat

Jan 12, 2010 7:58 AM in response to Noaks

well

The Noak's solution was helpful...

with this code I observed that the windows are listed on order from top to back into a application return...

so, using my code I reached the follow solution:

tell application "System Events"

set arr to {}

repeat with proc in application processes
tell proc
set topWin to 0
repeat with win in windows
if frontmost and topWin is 0 then
set onTop to true
set topWin to 1
else
set onTop to false
end if

set arr to arr & {{name of win, size of win, position of win, onTop}}
end repeat
end tell
end repeat

return arr

end tell


Thanks for all....

Jan 12, 2010 8:11 AM in response to sidneivl

There aren't some property that indicate the window is on focus?


There is no standard for this, no.

Since the window is a property of the application, it's up to the application to decide what data to make available via AppleScript. Some apps do include a 'frontmost' property for the window, others (most?) don't.

Since you can't rely on it being there, you can't use it.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How know what window is on focus?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.