Code that uses OS X accessibility API to list and move/resize windows
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Summary

Please provide source code for a program that can list all windows, and code that can move an individual window.

Details

It's a common Stack Overflow question: how can you programmatically list and move windows in OS X? The usual answer is that you can use accessibility APIs. This is the best example I've found: http://stackoverflow.com/questions/614185/window-move-and-resize-apis-in-os-x

There are two pieces of functionality, and it is okay for them to be provided either as library functions, command line tools, or both. The first would be to list windows with application name, title, position, and dimensions. If window ID or PID or some other identifier that can be used to connect the two is available, that would be best.

As an example, imagine the deliverable is two command line tools. The first lists tab-separated output window ID, application name, title, position (separate X and Y), and dimensions (separate width and height):

$ lswin
123 iTerm   Default 695 150 745 458
456 firefox Bountify | Post a bounty    120 22  1224    874

The second moves (and optionally resizes) a window by ID:

$ movewin 123 700 155       # move iTerm 5 pixels down and to right
$ movewin 456 0 22 300 300  # move Firefox to top left, make it tiny

You can imagine similar C functions that would do the same thing. It would be fine to provide library functions, and just a very thin main() wrapper to demonstrate the functionality. If I can compile and run the code on a couple OS X boxes and list and move windows, then I will consider this task well fulfilled.

Implementation Notes

  • Simple Cocoa code that can be compiled from the command line is preferred. (I'm posting this because I know C well, but am a total Cocoa newbie.)
    • But, any language which compiles down to a native program (no AppleScript, please; I have working AppleScript code, but using osascript is quite slow) is fine.
    • I promise a $20 tip if you also write a Ruby gem wrapper!
  • Source code rights should either be released to me or the public domain, or MIT/BSD licensed in your name, at your choice, so it's okay to be republished with modifications.
    • I'll plan to clean up and publish it (giving credit if you'd like) so that future Google searchers will be satisfied.
  • Should work on both Mountain Lion and Mavericks (deprecation warnings okay). If it only works on one of those, Mavericks is preferred.
  • Should not need undocumented API calls, and just using the accessibility API.
By the way, in case it is useful for hints, here are some equivalent AppleScript solutions: https://github.com/andrewgho/applescript/blob/master/lswin and https://github.com/andrewgho/applescript/blob/master/movewin
AGH over 6 years ago
And here is another relevant Stack Overflow post that covers pretty much this exact scenario (it has code snippets that are instructive, but does not have actual working code that can be compiled): http://stackoverflow.com/questions/6178860/getting-window-number-through-osx-accessibility-api
AGH over 6 years ago
awarded to Be The Match

Crowdsource coding tasks.

0 Solutions