惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Recent Announcements
Recent Announcements
D
Docker
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
腾讯CDC
B
Blog
博客园_首页
罗磊的独立博客
D
DataBreaches.Net
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence

Kenneth Friedman

Alan Kay Answers MIT Scheme on Apple Silicon (M1 Mac) Introducing, Toski Marginal Notes MIT: Second Year Masters Oh The Places I've Been Introducing, Scribr The 2034 Website Real and Spectacular
Open With Sublime
Kenneth Friedman · 2018-12-26 · via Kenneth Friedman

A quick way to open any file in Sublime, on a Mac.

Sublime Text — especially on the Mac — is known to be a super simple, easy text editor. It loads fast, and it's fairly minimal. Whenever I want to quickly open a text file or a small program Sublime is my go to choice.

The Problem

To quickly inspect files with Sublime Text, I need to be able to open any given file in Sublime from the Mac Finder.

Finder does have the notion of a "Default Application", but there are two problems. First, it works at a per file-extension level. You can assign a particular file type to open with a default application, but you can't assign all files to have one default application. Second, there can be only be one default application, which is problematic when you have a files that need multiple apps. For example, with an HTML file, it is best to open with a text editor to modify the file, but it's best to open in a browser to actually view the file.Why do websites need a separate editor and viewer? Don't get me started. Why isn't the authoring always on?

A Flaw in the Obvious Solution

Finder's "Open With" menu.

Luckily, Finder has a context menu (right clicking on a file), that includes an "Open With..." submenu. This menu lists some of the possible applications that can open the given file type. At first, I tried adding a keyboard shortcut to this Finder menu option. Adding keyboard shortcuts is very easy on the Mac... you can provide the exact name of the menu item, and then attach a key command to it. Because the default application uses "Cmd+O", it makes sense that the secondary application should be "Shift+Cmd+O."

Finder trying to find applications that can open this file.

Unfortunately, the list of possible "Open With..." items is generated on the fly, when the user clicks or hovers on the menu item. You can see this very briefly, when the list is empty and the menu says "Fetching..." When the standard keyboard shortcut is invoked, it looks for the exact menu item "Open With...Sublime", but since the list isn't populated, it can't find it, and the shortcut doesn't run.

The Real Solution

The secret lies with Services. Services is another great Mac feature that allows you script applications. I created a Service using Automator. The Service is always available, preloaded, in the Finder menu. (Under Finder->Services).

The Service (now called Workflow in Automator) is very simple. As shown below, it simply takes the currently selected files or folders in Finder, and opens them in Sublime. Easy!

Then, the Service can be "installed" by saving the file to ~/Library/Services/

Finally, the installation makes it available in Finder's Service menu. Therefore it is easy — once again — to attach a keyboard shortcut to the Service, which opens the currently selected file or folder in Sublime.

Now — using this Automator Service and Keyboard Shortcut combination — I can open any text file in Sublime with a simple Shift+Command+O keyboard shortcut.