Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save idleberg/fc0df222e1df258d525d704042d72582 to your computer and use it in GitHub Desktop.
Save idleberg/fc0df222e1df258d525d704042d72582 to your computer and use it in GitHub Desktop.
“Open in Sublime Text” in macOS context-menu

This list has been updated for Big Sur (and later). Since I don't use these versions, this guide might still need further improvements. For older macOS versions, please see this older revision.

Open in Sublime Text

  • Open Automator
  • Quick Action
  • Set “Service receives selected” to files or folders in any application
  • Add a Run Shell Script action
  • Set the script action to /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -n "$@"
  • Set “Pass input” to as arguments
  • Save as Open in Sublime Text

Add to Sublime Text

Let's repeat the steps above for a second entry, this time to add files or folders to an existing Sublime Text window

  • Open Automator
  • Create a new Service
  • Add a Run Shell Script action
  • Set “Workflow receives current” to files or folders in any application
  • Set the script action to /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -a "$@"
  • Set “Pass input” to as arguments
  • Save as Add to Sublime Text

Keyboard Shortcuts

You can assign a global shortcut to run the services we just created

  • Open “System Preferences”
  • Select “Keyboard” then the “Shortcuts” tab
  • In the left pane, click on “Services”
  • In the right pane, scroll to “Files and Folders”
  • Select “Open in Sublime Text” (or “Add to Sublime Text”) and click “add shortcut”
  • Select a shortcut

Edit Context Menu items

You might want to rename or edit the items we just created

  • Activate Finder
  • Click on “Finder” in the Apple menu, select “Services” then “Services Preferences”
  • In the right pane, scroll to “Files and Folders” and scroll to the item you want to edit
  • Right click the item and select “Open in Automator”
  • Edit and save

Alternatively, you can edit the workflow (e.g. ~/Library/Services/Open in Sublime Text.workflow) in your preferred text editor

@chaityacshah
Copy link

Awesome! Thanks.

Can someone please explain "$@" in /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -n "$@"

I understood that the first part finds the executable for Sublime Text, -n option is for a new window.

@partap
Copy link

partap commented Sep 4, 2018

"$@" is an automatic variable contains the input to the service.

...in this case, the filenames selected in Finder.

@johncthomas
Copy link

johncthomas commented Nov 14, 2018

Looks like Service option has been renamed to "Quick Action" in the current version of Automator.

@friedbrice
Copy link

friedbrice commented Jan 26, 2020

This doesn't seem to let me open directories. Is there a way to do that?
Screen Shot 2020-01-25 at 6 11 21 PM

I figured it out. I needed to set Shell: /bin/bash (which I had previously left blank) in my "Run Shell Script" action.

@ahmu83
Copy link

ahmu83 commented Jun 20, 2021

In macOS Big Sur the Automator option to "Create a new Service" is replaced with "Quick Action"
I had to look other places in automator for the "Create a new Service" option in confusion

@MajoroMask
Copy link

In macOS Big Sur the Automator option to "Create a new Service" is replaced with "Quick Action"
I had to look other places in automator for the "Create a new Service" option in confusion

And the Service receives selected have been changed into Workflow receives current, like this:

image

@idleberg
Copy link
Author

idleberg commented Jun 30, 2021

@ahmu83 @MajoroMask Thanks for the input, I've updated the gist accordingly

@Avila-Diego
Copy link

In macOS Big Sur the Automator option to "Create a new Service" is replaced with "Quick Action"
I had to look other places in automator for the "Create a new Service" option in confusion

And the Service receives selected have been changed into Workflow receives current, like this:

image

I tested it for macOS Bigsur, it works.

@rianmurnen
Copy link

Modified for use with Shortcuts.app instead of Automator.app

My Context

  • macOS Ventura v13.7 (22H123)
  • Shorcuts.app v6.0 (1505.3.1.3.4)
  • Sublime Text.app Build 4180

Shortcut Input

1-shortcut-actions
  1. Select “files” and “folders”. (Why it reads as “Files and 2 more” instead of "Files and Folders" is a mystery.)
  2. Set to "Stop and Respond" with a message of "Select a file or folder" in case the Quick Action is somehow activated without an input selected.

Run Shell Script

~/Applications/"Sublime Text.app"/Contents/SharedSupport/bin/subl -n "$@"
  1. Prepend the path with ~/. Why? I installed “Sublime Text” in my user’s “Applications” folder not the system’s “Application” folder.
  2. In the path, wrap “Sublime Text.app” in quotes ("Sublime Text.app") because it contains a space character. For whatever reason, escaping the space (\ ) doesn’t work in Shortcuts.app.
  3. Set "Shell" to "zsh".
  4. Set "Input" to "Shortcut Input".
  5. Set "Pass Input" to "as arguments".

Shortcut “Details”

2-shortcut-details-tab

In the “Details” tab:

  1. Select "Use as Quick Action".
  2. Select "Finder".
  3. Deselect “Service Menu".

Shortcut Privacy

3-shortcut-privacy-tab

In the “Privacy” tab:

  1. Deselected "Allow running when locked".

Note that Shortcuts.app will ask for permission to allow the “Run Shell Script” action to use “Finder" at some point in the this process so that it has access to the Shortcut Input. During my process, Shortcut.app “Settings…” dialog opened to the “Advanced” tab where I needed to select “Allow Running Scripts”.

4-shortcut-app-settings-dialog-advanced-tab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment