Formatting Code in Xcode with Astyle
If you’ve ever wanted automatic code formatting in xcode like that in eclipse, it is entirely possible with an automator workflow and astyle.
first install astyle, if using macports:
Then in our home directory we need to create a .astylerc file. This file defines the style for the code formatting. An example is provided below, for more info refer to the documentation.
Example .astylerc :
Then open up automator and create a new service. The default should be “Service recieves selected (text) in (any application)” with “Output replaces selected text” unchecked.
Check the “Output replaces selected text” checkbox. We want to replace the original text with formatted text that astyle generates. Then drag the “Run Shell Script” into the workflow area.
In the “Run Shell Script” box replace any text with:
Save your service and open up the keyboard settings in System preferences. In the “Keyboard Shortcuts” tab find your service and set it’s shortcut.
To use the service, select any text in xcode and use the shortcut you previously set. This shortcut will work in other editors too!