Xcode: wrap code in comment tags

While AppleScript’s Script Editor has long had a built-in function for wrapping or unwrapping code with comment tags, for some reason this seems to be missing in Xcode.

Not to worry, nothing a bit of AppleScript and Automator can’t sort out. This will work in most text editors as well as in Xcode. Here’s what it does:

Install the Service by double-clicking on the downloaded .workflow file and clicking through the dialog boxes. When it’s installed, you can assign it whatever hotkey you want in System Preferences | Keyboard | Shortcuts.

Download Toggle Comments for Selection workflow.zip

Screen Shot 2014-11-29 at 12.28.59

If you want to use it for languages that have different comment tags you can adjust the code in Automator. Likewise, it would be fairly simple to have the script detect a number of different tags and respond appropriately, but here I’ve just stuck with the /* …. */ tags.

A note on usage: when uncommenting, it’s best to ensure that the selection begins at the leading forward slash and ends at the trailing forward slash (in other words that there’s no whitespace at either end of the selection). I have built in some attempt to strip leading and trailing whitespace, but an accurate selection will always be the most reliable.

Download Toggle Comments for Selection workflow.zip


About philastokes

Freelance Writer, Developer and Technical Communicator. Explaining the unexplainable with images, video and text. Scripting anything imaginable in Applescript, Bash, C, Objective C, Cocoa, Python and Xcode.

Posted on November 29, 2014, in AppleScript, Automator, Developer, Xcode 6 and tagged , , , , , , , , . Bookmark the permalink. Comments Off on Xcode: wrap code in comment tags.

Comments are closed.

%d bloggers like this: