frontmost window, overall

The code below does a credible job (possibly with some exceptions) of identifying windows and their applications. My question: is it possible to determine which of the windows is frontmost overall?


set ExstWinList_ to ""
tell application "System Events" to set VisApp_ to every application process whose visible is true
set VisAppCnt_ to count items of VisApp_
repeat with i_ from 1 to VisAppCnt_
set VisAppNm_ to name of item i_ of VisApp_
try
tell application VisAppNm_ to set FWNm_ to name of window 1
if FWNm_ is not "" then
try
tell application VisAppNm_ to set FWNm_ to name of document 1
end try
end if
if FWNm_ is not "" then set ExstWinList_ to ExstWinList_ & "Application "" & VisAppNm_ & "" has a frontmost window named "" & FWNm_ & "."" & return & return
end try
end repeat
display dialog "The following applications each have at least one window on the desktop: " & return & return & ExstWinList_

MAC pro, Mac OS X (10.4.11)

Posted on Oct 21, 2009 7:23 PM

Reply
10 replies

Oct 26, 2009 5:01 PM in response to Noaks

This is a reply to Pierre L. Sorry, there is no reply option associated with his response.

Thanks, Pierre, but I think that's territory we covered weeks ago.

The frustrating thing is this: if there is a variety of windows on the desktop (originating from a variety of apps) and Script Editor.app is opened,
the focus shifts to Script Editor. At that point, any legitimate code (which doesn't open yet another window) can be written; if it ends with

tell app "System Events" to keystroke tab using command down

focus reverts to the frontmost window before Script Editor was opened AND the tool bar displays the name of the application associated with the
frontmost window -- that application being the one that is sought. But, as I see it, there's no way to capture that application name directly -- the code
"menubaritem2 of menubar1" must necessarily end in "of process XXXX" where XXXX is the name of the application that we can now see but can't say, if you will.

Oh, well, I'll mark this as answered.

Oct 27, 2009 3:45 AM in response to Noaks

The code below does a credible job (possibly with some exceptions) of identifying windows and their applications… +from front to back+.

--BEGINNING OF SCRIPT:
tell application "System Events"
set theForegroundProcesses to the processes whose background only is false
set theNamesOfTheVisibleProcesses to the name of the processes whose visible is true

-- List the visible processes, from front to back:
set theProcesses to {}
repeat (count theForegroundProcesses) times
keystroke tab using {shift down, command down} -- backwards
delay 0.1 -- adjust if necessary
set the currentFrontProcess to (process 1 whose frontmost is true)
if the name of the currentFrontProcess is in theNamesOfTheVisibleProcesses then
copy the currentFrontProcess to the beginning of theProcesses
end if
end repeat

-- Hide again the applications which were initially hidden:
repeat with theCurrentProcess in theForegroundProcesses
if the name of the theCurrentProcess is not in theNamesOfTheVisibleProcesses then
set visible of the theCurrentProcess to false
end if
end repeat

-- List the windows:
set theWindows to "FROM FRONT TO BACK:" & return & return
repeat with theCurrentProcess in theProcesses
if window 1 of theCurrentProcess exists then
set the windowName to name of window 1 of theCurrentProcess
set the applicationName to displayed name of (get file of theCurrentProcess)
set theWindows to theWindows & "Window “" & windowName & "” of application “" & applicationName & "”" & return & return
end if
end repeat
end tell

tell me to activate
display dialog theWindows buttons {"OK"} default button 1 giving up after 15
--END OF SCRIPT

From there, it should be easy to get the name of the frontmost application, the first one in the list if the script is run from the Script Menu, the second one if the script is run from the AppleScript Editor window.

Hope it can help

Oct 27, 2009 6:49 AM in response to Noaks

I thought that you wanted the +frontmost window+ overall, not just the application behind the AppleScript Editor. Suppose for example that TextEdit is the application behind the AppleScript Editor, but that all of its windows have been closed. In such a case, the last application is not the one whose window is frontmost. Unless I missed the point completely?

Oct 27, 2009 8:21 AM in response to Pierre L.

You're probably right. I think the question inadvertently morphed into getting the application when in fact I had asked to get the name of the window -- when I was unsuccessful at getting the window, it appeared that I would have to first get the application.

Your solution does both, and it certainly can be modified to provide the same result as John Maisey's. Sorry for the confusion.

Message was edited by: Noaks

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.

frontmost window, overall

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