问题
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.
- Start Automator.app.
- Select "Service".
- Drag-n-drop "Run AppleScript" to workflow.
- Set "service receives" to "no input", and application is Xcode.
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
Save workflow.
- In System Preferences → Keyboard → Keyboard Shortcuts → Services → General, assign a shortcut.
- 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:
- open "System Preferences" → "Keyboard" → "Keyboard Shortcuts" → "Application Shortcuts"
- click on the "+" plus sign
- select "XCode" as Application and type "Open with External Editor" as Menu Title
- 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.
- Choose the Menu: "Xcode" -> "Preferences..."
- Click the "Key Bindings" Tab on the Preferences Window
- In the "Filter" search box enter
External
- 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.
- Choose the Menu: "MacVim" -> "Preferences..."
- 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