Xcode is scriptable, and even has its own Script menu. Sadly, it uses an odd method to add items, and it’s hierarchical without access to shortcuts, so it’s a bit fiddly to use. But if you use one of the third-party utilities that lets you assign keyboard shortcuts, like FastScripts, you can make life a bit easier for yourself.
One of the things I keep having to type over and over is:
property NSWhatever : class "NSWhatever"
So I wrote a script whereby I just type the class name, hit a shortcut (control-P in my case), and the property line is inserted. It’s a bit rough and ready, but it’s already saved me a lot of typing. Enjoy.
tell application "Xcode"
tell text document 1
set {theFirst, theLast} to selected character range
if theLast < theFirst then -- assume insertion point is at end of class name, so select whole name
-- get previous 60 characters and get length of last word
set prevText to (get characters (theLast - 60) thru theLast) as text
set theOffset to length of word -1 of prevText
set selected character range to {(theLast - theOffset + 1), theLast}
else
-- assume class name is selected
end if
set className to contents of selection
-- strip off any white space
repeat while character -1 of className is in {return, linefeed, space, tab}
set className to text 1 thru -2 of className
end repeat
repeat while character 1 of className is in {space, tab}
set className to text 2 thru -1 of className
end repeat
if className is in {"NSURL"} then
-- NSURL is AS terminology, so it needs pipes
set contents of selection to "property |" & className & "| : class \"" & className & "\"" & return
else
set contents of selection to "property " & className & " : class \"" & className & "\"" & return
end if
-- replace selection and select following insertion point
set {theFirst, theLast} to selected character range
set selection to insertion point (theLast + 1)
end tell
end tell
Here is what I have been using. I added in the NSURL from your example.
Type in the class name, highlight it and execute the script.
If nothing is highlighted, nothing will happen. You need to check the “Replace Selection”
in the Output popup in the user scripts window.
Yeah, I can never remember the shortcut for highlighting the preceding word, and I wanted to be able to type the class name then hit control-P, without having to grab the mouse and select.
if className is in {"NSURL"} then
-- NSURL is AS terminology, so it needs pipes
set contents of selection to "property |" & className & "| : class \"" & className & "\"" & return
else
set contents of selection to "property " & className & " : class \"" & className & "\"" & return
end if
to
if className is in {"NSURL"} then
set contents of selection to "current application's class \"" & className & "\"" & return
else
set contents of selection to "current application's class \"" & className & "\"" & return
end if
Actually, you can assign shortcuts to the user scripts. You do that just to the right of the name of the script. There is a column with the command symbol as its label.
This is how you would run your applescript from Xcode’s script menu.
The script doesn’t need that top and tail – the menu supports straight AS. Some of the samples are AS, such as “Delete Line”. It’s the keyboard shortcuts I missed, thanks.
But puzzlingly it adds “(null)” after the inserted text when run from Xcode’s menu.
Yes. It’s very odd – if I run it from anywhere else, it’s fine, but in Xcode’s menu it insists on adding “(null)” whenever I set the contents of the selection.