Search code examples
xcodexcode4

Xcode — quickly open current file in external editor


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.)


Solution

  • 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.