“Sort Lines in Selection” for Xcode 4

被刻印的时光 ゝ 提交于 2019-12-02 19:15:19

You can do this with Automator in Lion.

  • Start Automator and select "Service"
  • Find and drag "Run Shell Script" into the workflow panel
  • Select "Output replaces selected text"
  • Type sort -f into the "Run Shell Script" textfield
  • Save

Now you can sort lines in any textfield. Select some text and right-click or Control click and select the service you just created.

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.

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!