I like Textmate\'s \"Sort Lines in Selection\" feature a lot. Is there something similar for Xcode 4? If not, what would be the best way to integrate such a feature (a plugin, A
For macOS 10.14+ (Mojave) users who are looking at epatel's answer, Automator has renamed Services
to Quick Action
. I was lost for a moment until I figured it out.
How
In TextMate, open the bundle and see how they have implemented it ;)
Specifically, they have used sort -f
for that command.
In Xcode
Xc4 doesn't offer external commands, but Xc3 did... what version are you using?
Here's an overview for Xc3's script system: http://www.mactech.com/articles/mactech/Vol.23/23.01/2301XCode/index.html
Xc4 allows you to run an external script via Behaviours, but you cannot pass or return text/selection.
AppleScript
You may be able to do it with AppleScript... every time I have tried to do anything nontrivial with AS + Xcode, it didn't work out very reliably (if at all). But that was with Xc3 - locating what you need may be easier with Xc4 (unified UI and all).
I just use TextMate for this.
After 4 years Xcode still doesn't have this feature builtin, but now it supports extensions. So here you go: "xcsort" is an extension to sort text in Xcode 8. It adds a command to sort lines in selection.
You can do this with Automator:
sort -f
into the "Run Shell Script" textfieldNow you can sort lines in any textfield. Select some text and right-click or Control click and select the service you just created.
There's a bug on 10.7 and 10.8 where the shortcuts for Automator services don't always work until the services menu has been shown once from the menu bar. You can still select them from the context menu, but another option would be to assign a shortcut to a script like this:
try
set old to the clipboard as record
end try
try
tell application "System Events" to keystroke "c" using command down
do shell script "export LC_CTYPE=UTF-8; pbpaste | sort -fn | pbcopy"
tell application "System Events" to keystroke "v" using command down
delay 0.05
end try
try
set the clipboard to old
end try
Trying to get the clipboard when it's empty results in an error. pbpaste and pbcopy don't support Unicode by default in the environment used by do shell script.