Wednesday, March 09, 2011

XCode: Copy/Move Up/Down line


[Note: Please see this post for corrected scripts]

XCode already has the following scripts under Script Menu > Text
  • Delete Line
  • Move Line Up
  • Move Line Down
I added the following two scripts:
  • Copy Line Up
  • Copy Line Down
Basically I started by creating the following Applescript files:

/Developer/Library/Xcode/User Scripts/Copy Line Up.scpt with content:

--
(*
To edit this script, choose Save As... and save it in your home directory, then re-add it to the User Scripts list.
*)
using terms from application "Xcode"
tell first text document
set {startLine, endLine} to selected paragraph range
if startLine > 1 then
set theText to (paragraphs startLine through endLine)
set theText to (theText as string)
make new paragraph at beginning of paragraph (startLine) with data theText
set selected paragraph range to {startLine, endLine}
else
beep 1
end if
end tell
end using terms from
--

/Developer/Library/Xcode/User Scripts/Copy Line Down.scpt with content:

--
(*
To edit this script, choose Save As... and save it in your home directory, then re-add it to the User Scripts list.
*)
using terms from application "Xcode"
tell first text document
set {startLine, endLine} to selected paragraph range
if endLine < (count paragraphs) then
set theText to (paragraphs startLine through endLine)
set theText to (theText as string)
make new paragraph at beginning of paragraph (startLine + 1) with data theText
set selected paragraph range to {endLine + 1, endLine + 1}
else
beep 1
end if
end tell
end using terms from
--

and then added those scripts using the Script Menu > Edit User Scripts... dialog.


While I was at it I added the following keybindings:

Delete Line Command+D
Move Line Up Alt+UP
Copy Line Up Control+Alt+UP
Move Line Down Alt+Down
Copy Line Down Control+Alt+Down

Note: The Command+D keybinding for Delete Line conflicts with the default Add Bookmark keybinding. I changed that keybinding to something else in Preference > Key Bindings > Menu Key Bindings > Edit section.

Here is the screenshot:


Now I have my LineTools NetBeans plugin like functionality in XCode. Yay!

No comments: