tonyarnold/Xcode-4-Uncrustify-Automator-Services — pull request #8 by idpaterson (2012-08-23 15:18:59+00:00)
Title: Handling multiple Xcode installs

The path of the frontmost application is now used to distinguish multiple Xcode installs, which should fix #6.

Additionally, I developed a robust technique to find the current active document in Xcode 4 since the approach in the first commit was not working in assistant editors. While this was trivial in Xcode 3, there were no good solutions out there for Xcode 4 that did not break down when pitted against multiple windows, tabs, or assistant editors.

There are a few ways that we can interact with the active document in AppleScript without actually having a reference to it, for example emitting keystrokes or changing the text selection. The approach used in this pull request is to:

1. Use the Xcode menu to select all text in the active document
2. Iterate all open `text document` instances and record their current `selection` value
3. Emit an up arrow keystroke to deselect the active document
4. Iterate all open `text document` instances and find the one with a different `selection` value
5. Return this as the active `text document` and operate on it

This approach should work well until Xcode's AppleScript bindings are improved. Since the `menu_click` routine could be subject to system localization issues, I made a small "config" section at the top for convenience.