Nat! bio photo

Nat!

Senior Mull

Twitter Github Twitch

Sorting and Beautifying Code in Xcode

Thanks to Automator it's very easy to create a Service using Applescript and unix commands. Here's an example how to sort lines in Xcode with the context menu.

In Automator create a new Service (Dienst)

Automator Open Dialog

Then add a shell script to the Service (red). Be sure to check the box (yellow) and change the command to sort (green), Finally save the script. The name you give to your script, will be the name that appears in the Services menu. I chose "Zeilen sortieren".

Add

Now in 10.8.1 everything works fine. But I remember that in former times, it may have been necessary to do some magic to get the service going (login/logut). But maybe I am confusing this with Colorsync or Finder plugins.

Anyway... In Xcode select some lines and run your "Service" on it. If you don't have many services, you might be lucky, and the services will appear directly in the top menu.

Doing the magic in Xcode

Now with these preliminaries out of the way, I have created a small unix tool, that aligns variables (and as a bonus also can align assignment statements).  So this:

   int    foo;
   static char  *x;
   auto volatile int  foo[ 126];

becomes

   int                 foo;
   static char         *x;
   auto volatile int   foo[ 126];

and

   x = foo
    // bla
      y[x] = bar

becomes

   x     = foo
    // bla
   y[x]  = bar

Post a comment

All comments are held for moderation; basic HTML formatting accepted.

Name:
E-mail: (not published)
Website: