Tuesday, March 15, 2011

XCode: Corrected user scripts - Delete Line, Copy/Move line Up/Down

[Note: It is a shame that XCode 4 does not have the Scripts menu]

XCode has the following scripts:
  • Delete Line
  • Move Line Up
  • Move Line Down
I added two more (as described in this post):
  • Copy Line Up
  • Copy Line Down
However there was a problem. The script always worked on the first text document. That meant that when you had multiple documents open, the commands sometimes modified the document which did not have focus (baaad!).

It turns out the window 1 is always the window which has the focus. I corrected the scripts to make use of this fact. Here is the general strategy behind the modifications I made to the scripts:
:
:
using terms from application "Xcode"
 set selectedDocument to missing value
 repeat with index from 1 to (count of text documents)
  if (associated file name of window 1 as string) is equal to (path of text document index) then
   -- found the document
   set selectedDocument to (text document index)
   exit repeat
  end if
 end repeat
 if (selectedDocument is not missing value) then
  tell selectedDocument
   -- do the modifications to document
  end tell
 else
  beep 1
 end if
end using terms from

Download the scripts here. Just unzip and drop the scripts in /Developer/Library/Xcode/User Scripts/ .

I have filed a bug at http://radar.apple.com for this. The problem Id is 9138643. I have attached the fixed scripts to the problem.

4 comments:

Anonymous said...

This is wonderful blog. I love it.

Anonymous said...

How do you use them? I dropped into the folder mentioned above but can't get them work in Xcode 4 :(

Sandip Chitale said...
This comment has been removed by the author.
Sandip Chitale said...

As noted at the top of the blog entry, Apple removed the Scripts support in Xcode 4. I am investigating other approaches to support it.