Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm developing an commercial application that does exactly what you describe and I've been messing with different ways of doing this for over a year now. I'm a newbie developer, so I'm totally open to the idea that there may be a much better way of doing it than my way, but it seems to be stable and work.</p> <p><strong>I use Apple Script</strong></p> <p>I get the path of the active document every 3 seconds, but if the active application is the Finder, I get the path of the active window. I do this using Apple Script as described by the accepted answer.</p> <p><strong>Getting Window List using Carbon</strong></p> <p>To get the window list of all processes to get the window ID (something Apple Script can't do), I use CGWindowListCopyWindowInfo as detailed in this question:</p> <p><a href="https://stackoverflow.com/questions/311956/getting-a-unique-id-for-a-window-of-another-application">Getting a unique ID for a window of another application</a></p> <p>This presents me an array with all the windows of all processes ordered by which is frontmost. So all I need to do is pluck the first entry from the array. This can also be used to get a screengrab of the front window, if that's helpful to you, as shown in the Son of Grab sample application, which has been invaluable to me as a working example.</p> <p><strong>Sending Apple Script from Cocoa</strong></p> <p>When it comes to Apple Script, I've experimented with all 3 that Jim suggests and my conclusion is that each has it's issues, both in terms of stability and flexibility.</p> <ol> <li><p>Apple Event Manager relies on you sending raw Apple Events. For me, this was too much like hard work and too low level.</p></li> <li><p>Using NSAppleScript I found to be slow and error prone. It's also pretty clumsy when you want to send a whole variety of Apple Script messages as I do.</p></li> <li><p>When reading about Scripting Bridge, I got very excited, only to try it out and be very disappointed. For the attributes of the windows I was trying to grab, it didn't even recognise them as existing. It seemed to me to be buggy and weird. I expect if the commands you're sending are supported by SB, it would be the best option.</p></li> </ol> <p><strong>A Fourth Option</strong></p> <p>I now rely on a fabulous Objective C wrapper around Apple Script called <a href="http://appscript.sourceforge.net" rel="nofollow noreferrer">AppScript</a>. It's been around for many, many years, it's stable, the developer is great and most of all it just works. </p> <p>It has rock solid methods which allow you to send Apple Script and it'll even return errors for you neatly without a whole load of potentially buggy and messy code needing to check for them. </p> <p>I've been using it for a year now with no problems. If you have any other questions about how to do this, please don't hesitate to comment and I'll try my best to answer them.</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