“Sort Lines in Selection” for Xcode 4

前端 未结 5 1576
孤街浪徒
孤街浪徒 2021-02-01 20:09

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

相关标签:
5条回答
  • 2021-02-01 20:44

    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.

    0 讨论(0)
  • 2021-02-01 20:46

    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.

    0 讨论(0)
  • 2021-02-01 20:47

    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.

    0 讨论(0)
  • You can do this with Automator:

    • Start Automator and select either:
      "Service" for macOS 10.7 or
      "New Document" followed by "Quick Action" for macOS 11
    • 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.

    0 讨论(0)
  • 2021-02-01 21:01

    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.

    0 讨论(0)
提交回复
热议问题