title: AppleScript: Get list of windows on all desktops
body: <p>I need to get a count of all windows per application.  Every way I've tried to do this, I only get a count of windows that are assigned to the current (Mission Control) Desktop.  (I'm currently running Mac OS X 10.7, so post-Spaces.)  Is there any way to get a per-application count of all windows across all Desktops?</p>
  
  <p>The crux of what I've tried:</p>
  
  <pre><code>tell application "System Events"
    repeat with _app in (every process whose visible is true)
      tell _app
        log (name as string) &amp; ": " &amp; (count of every window)
      end tell
    end repeat
  end tell
  </code></pre>
  
  <p>Note that the <code>whose visible is true</code> clause isn't the problem.  It finds all of the appropriate processes, but once I ask the processes for windows, they only count the ones in the active Desktop.</p>
  
  <p>I've tried pulling the <code>log</code> line out of the <code>tell</code> and using <code>name of _app</code> and <code>count of every window of _app</code>, but there's no difference.  I've tried grabbing things other than <code>process</code>es from <code>System Events</code>, but anything useful ends up effectively being just a different way to get the same object.  I've tried iterating over <code>UI elements</code>, but no windows show up that aren't on the current Desktop, though I do get a menubar for each application.</p>
  
  <p>I'm fine with iterating across all Desktops (though not actually switching to all of them), but I can't even find a way to get a list of Desktops.  <a href="https://stackoverflow.com/a/18707251/147202">This answer</a> claims to describe how to do that, but I only ever get a single element inside <code>every desktop</code>.  Not that there's an obvious way to get windows once you have that Desktop object anyway.</p>
  
  <p>It's also worth pointing out that desktops are controlled by the Dock, and not by Mission Control.  I'm not aware of any way for AppleScript to talk to the Dock, so if you know of something, then an answer or comment about that might help point me in the right direction.</p>
  
  <p>Am I trying to do something impossible?</p>