How to check for the presence of a hidden window?

Hi,

I am trying to put together a macro that would allow me to have a hotkey combination perform a different action depending on whether or not a specific window is in the foreground and has focus.

The problem is that the window i am checking for, Optimal Layout, which is a window manager doesnt appear in the menu bar when it has focus. ie It seems to be a hidden window.

Is there any way for me to check whether or not that apps “window” is at the fg and currently has focus? I ran into a similar issue when trying to detect whether or not the Quicksilver window is currently active.

Any ideas?

Bump…

Other than Find Image on screen (presuming the window changes somehow when it is at the foreground), or perhaps some sort of Applescript solution, I don’t know.

Hey Guys,

System Events can see some hidden windows and palettes and suchlike, but it won't register things like LaunchBar's window as part of the front application space.

Okay. I just figured out how to detect LaunchBar's window:

If LaunchBar's window is OPEN System Events sees it when asked to look – if the window is NOT open System Events will NOT see it.

# AppleScript to determine if LaunchBar's window is open.
tell application "System Events"
  tell process "LaunchBar"
    if (count of (get its windows)) > 0 then
      return true
    else
      return false
    end if
  end tell
end tell

This is less than ideal, but if it works for Optimal Layout and/or Quicksilver you can do some things.

I have the guys at Objective Development (LaunchBar's devs) a pretty hard time when they moved to this phantom window methodology, because many, many macros I had could no longer detect LaunchBar's application context and became useless. Decade-old workflows died. But that's the way the bits crumble in the computer game, so I had to lump it and patch-around those short circuits.

-Chris

1 Like

@peternlewis Whoa, that’s a nifty little feature, thx!!

Thx @ccstone, I tried your script but it doesn’t work for me with Quicksilver or Optimal Layout. The script seems to always return true even if the window isn’t active/has focus. It seems that it’s detecting a window even when it isn’t active. If I quit the app then it works.

I basically just changed the process name.

# AppleScript to determine if Optimal Layout's window is open.
tell application "System Events"
  tell process "Optimal Layout"
    if (count of (get its windows)) > 0 then
      return true
    else
      return false
    end if
  end tell
end tell

Unfortunately this isn't shocking. Some apps maintain an invisible window and flip its visibility.

Try this though before you give up.

tell application "System Events"
  tell process "Optimal Layout"
    return count of (get its windows)
  end tell
end tell

Use an Execute an AppleScript action and save the result to a variable.
Display the variable in a Display Text action.

Run your macro with your apps' windows visible and NOT visible and see if there is a difference in the count.

-Chris

@ccstone Thx for the quick response, that set me on the right track!! Seems like OL has 2 windows visible when it’s in active and 0 when it isn’t. QS on the other hand has 2 always visible and 3 when active.

Any thoughts on which option (Find Image vs AppleScript) would be more reliable/performant?

AppleScript.

The basic sequence of preference should be:

  • Native Keyboard Maestro actions
  • UI actions such as selecting menus, clicking buttons, etc
  • AppleScript/JXA/Shell Script (if you can perform the AppleScript as a higher level command, then this is preferable to UI actions).
  • Click at locations, including clicking relative to an image.

Although, of course, the preferable course is whichever you can get working reliably most easily/quickly, and then only "improving" the macro if it proves necessary.

1 Like

Awesome, thx for all the help!!

1 Like