Xcode — quickly open current file in external editor

烂漫一生 提交于 2020-01-13 16:25:14

问题


Is it possible to set a keyboard shortcut (or maybe add some menu item somewhere) to open currently edited file in external editor?

(Obviously I may do [ (right click in file tree → Show in Finder) / (right click in window title → select containing directory) ] → right click on file → Open With → "the app" — but it's too much steps.)


回答1:


Huh, I've found it.

  1. Start Automator.app.
  2. Select "Service".
  3. Drag-n-drop "Run AppleScript" to workflow.
  4. Set "service receives" to "no input", and application is Xcode.
  5. Paste this code (replace MacVim with your editor)

    tell application "Xcode"
        set current_document to last source document
        set current_document_path to path of current_document
    end tell
    
    tell application "MacVim"
        activate
        open current_document_path
    end tell
    
  6. Save workflow.

  7. In System Preferences → Keyboard → Keyboard Shortcuts → Services → General, assign a shortcut.
  8. You're done!

I've borrowed AppleScript code from this project: Uncrustify Automator Services for Xcode 4.




回答2:


A better solution is to add a keyboard shortcut directly in the keyboard preferences pane:

  1. open "System Preferences" → "Keyboard" → "Keyboard Shortcuts" → "Application Shortcuts"
  2. click on the "+" plus sign
  3. select "XCode" as Application and type "Open with External Editor" as Menu Title
  4. set your shourcut (I'm using ⇧⌃E)



回答3:


Because I use tabs, I was eager for a solution that wouldn't open a document from the first tab. I ended up with following:

Note: If Assistant editor is opened, the file from the Standard Editor (on the left side) is opened regardless which editor is active, but for me it's better than the first tab.

on run {input, parameters}

    set current_document_path to ""

    tell application "Xcode"
        set last_word_in_main_window to (word -1 of (get name of window 1))
        if (last_word_in_main_window is "Edited") then
            display notification "Please save the current document and try again"
            -- eventually we could automatically save the document when this becomes annoying
        else
            set current_document to document 1 whose name ends with last_word_in_main_window
            set current_document_path to path of current_document
        end if
    end tell

    tell application "MacVim"
        if (current_document_path is not "") then
            activate
            open current_document_path
        end if
    end tell

    return input
end run



回答4:


I really liked the solution that Fjölnir made, so to expand on how to do that.

  1. Choose the Menu: "Xcode" -> "Preferences..."
  2. Click the "Key Bindings" Tab on the Preferences Window
  3. In the "Filter" search box enter External
  4. Double click on the "Key" column next to the "Open with External Editor (File Menu)" command.

If you are using MacVim for this, you can improve the load time for the external editor by configuring it to stay running after the last window closes.

  1. Choose the Menu: "MacVim" -> "Preferences..."
  2. In the "General" tab set "After last window closes" to "Hide MacVim"

Now when I press ⌥E the window pops open, I make my change, :q, and focus returns to Xcode.



来源:https://stackoverflow.com/questions/12300982/xcode-quickly-open-current-file-in-external-editor

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