Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One way of doing this is indeed to use the accessibility APIs.</p> <p>An application I'm developing is doing just this to get the front window, the document path of the front window and many other attributes.</p> <p>The way I'm doing this is through AppleScript. It can be clumsy at times, but it seems to be fairly reliable. I use <a href="http://appscript.sourceforge.net/" rel="noreferrer">AppScript</a> to send AppleScript from within my Cocoa app. It's thread safe and more stable than the alternatives - either Scripting Bridge or NSAppleScript.</p> <p>The difficult bit will be identifying a window using it's window ID in AppleScript - AppleScript doesn't seem to have a window ID property that matches up to CGWindowID. However, you can get any window you want using AppleScript.</p> <ol> <li><p>Move frontmost window to 100, 100</p> <pre><code>tell application "System Events" set theprocess to the first process whose frontmost is true set thewindow to the value of attribute "AXFocusedWindow" of theprocess set position of thewindow to {100, 100} end tell </code></pre></li> <li><p>Resize frontmost window to 200, 300</p> <pre><code>tell application "System Events" set theprocess to the first process whose frontmost is true set thewindow to the value of attribute "AXFocusedWindow" of theprocess set size of thewindow to {200, 300} end tell </code></pre></li> <li><p>Change current window of the frontmost application</p> <pre><code>tell application "System Events" set theprocess to the first process whose frontmost is true set thewindow to the value of attribute "AXFocusedWindow" of theprocess set size of thewindow to {200, 300} windows of theprocess -- Code to get ID of window you want to activate tell window 2 of theprocess -- assuming it's window 2 perform action "AXRaise" end tell end tell </code></pre></li> <li><p>Window that's active</p> <pre><code>tell application "System Events" set theprocess to the first process whose frontmost is true set thewindow to the value of attribute "AXFocusedWindow" of theprocess end tell </code></pre></li> </ol> <p>There's an application available for AppScript called <a href="http://sourceforge.net/projects/appscript/files/ASTranslate/" rel="noreferrer">ASTranslate</a> that will turn this AppleScript into the Objective C code that calls the relevant commands in AppScript.</p> <p>For more information on how to get the size and bounds of windows (these are read only as far as I'm aware) see the <a href="http://developer.apple.com/mac/library/samplecode/SonOfGrab/index.html" rel="noreferrer">Son of Grab</a> sample application.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload