Thank you, Dmitry. Your ugly method works (with a few tweaks, see below).
I was worried that it would be excruciatingly slow to iterate through all of Xcode's "text documents". However, very strangely, 'text documents' always contains exactly 2 documents – the current frontmost text document that I want, and one other which is often, but not always, the prior frontmost text document. Very strange! If it is true that there are always 2 'text documents', then instead of being excruciatingly slow, my script might be only painfully slow.
(What I want to do is to be able to insert text like I could with the wonderful User Scripts feature, which apparently has been eliminated from Xcode 4. User Scripts could insert text in milliseconds with a shortcut key.)
I'm just getting over the fact that Apple has apparently eliminated the wonderful User Scripts feature that they worked so hard on spiffing up in Xcode 3, and now I can't believe that it takes 27 lines of AppleScript code to get the current text document in Xcode 4.
I've also shown code to accomplish the same task in TextEdit, by comparison. Does anyone know a better way to do this?
TextEdit (Requires 1 line of code)
tell application "TextEdit" to set my_document to document 1
Xcode 4 (Requires 27 lines of code)
tell application id "com.apple.dt.Xcode" activate set my_work_space to active workspace document set my_projects to projects of my_work_space set my_project to item 1 of my_projects set my_windows to windows set my_window to item 1 of my_windows set window_name to name of my_window -- window_name is typically "ProjectName - FileName.m" -- The following is very fragile and ugly… tell me set my_names to split(window_name, " - ") end tell set target_file_name to item 2 of my_names set my_document to missing value repeat with a_document in text documents if ((name of a_document as string) is (target_file_name as string)) then set my_document to a_document exit repeat end if end repeat end tell
to split(aString, delimiter) set oldastid to AppleScript's text item delimiters set AppleScript's text item delimiters to delimiter set pieces to aString's text items set AppleScript's text item delimiters to oldastid return pieces end split
|