With automator you can add scripts to your right click menu in xcode. We can use this to create a script that helps us keep our #import directives in order. In this example the directives will be sorted in alphabetical order and removing duplicates. Lets start automator and begin.
- In automator go to File -> New choose Service as the type and click Choose
-
Drag a Run shell script action to the drag actions here… area. Select text in the services recieves selected dropdown. select any application in the in dropdown. Check the Output replaces selected text checkbox
-
This is where the magic happens. Type the following in your run shell script:
the automator window should now look like this:
-
Choose File -> Save… **and save the service as **Sort and remove duplicates
-
Restart Xcode, open a file with messy #import directives, select them and right click and choose Sort and remove duplicates.
Sort and remove remove duplicates result
These #import directives now looks like this: