162

On my windows pc, I would just double click a folder and click "Open in VS Code" to a open a folder, which saved time for me when looking through lots of code online. With Mac, however, I do not have this option. I have to directly open vscode and click "Open" to open a new folder. How do I directly open a file in vscode through finder in macOS?

anshul
  • 2,431
  • 2
  • 10
  • 22
  • It's worth noting that being able to directly open a file in vscode through finder in macOS already exists - what's missing is the ability to open a folder in vscode from finder, and that's what the answers are addressing. – Shiraz Oct 04 '22 at 13:21

12 Answers12

327

2023 Update - Use Native Method

  1. Open the Command Palette (Cmd+Shift+P) and type shell command to find the Shell Command: Install 'code' command in PATH command.

Command Palette

  1. Restart all terminal sessions for the new $PATH value to take effect.

You'll be able to type code . in any folder to start editing files in that folder.


If you want to do it in Finder, you can write an Automator script to do it (it's easier than it sounds, AND shows you the power of the OS).

  1. Launch Automator

  2. Create New Document

  3. Create a new Quick Action Select "Quick Action"

  4. Add the Action... (New Method - 10.13+)

    • Workflow receives current files or folders from Finder.
    • Add a new "Open Finder Items" action to the workflow. (drag the "Open Finder Items" object, highlighted in the screenshot, to the empty window on the right)
    • Select "Visual Studio Code" from the list.

Add "Open Finder Items"


  1. Add the Action... (Old Method)

    • Workflow receives current files or folders from Finder.
    • Add a new Run Shell Script action to the workflow. (drag the "Run Shell Script" object, highlighted in the screenshot, to the empty window on the right)
  2. Configure the Workflow (Old Method)

    • Set the Pass Input to be as arguments
    • Paste the following in the input box:
    open -n -b "com.microsoft.VSCode" --args "$*" 
    

    Screenshot of Workflow

  3. Save the action using a name like Open in Visual Studio Code.

You may now right-click on the folder and find your newly created task under Quick Actions.

enter image description here

jnovack
  • 7,629
  • 2
  • 26
  • 40
  • Is there any way you can change the icon for that quick action? – anshul Sep 25 '20 at 16:51
  • 2
    There is. That's the "Image" icon in the second screenshot. Select from a list or add your own. – jnovack Sep 25 '20 at 19:46
  • 2
    And for VSCodium it's: `open -n -b "com.visualstudio.code.oss" --args "$*"` – cyberwombat Nov 11 '20 at 14:36
  • 2
    For VS Code Insiders use the Application Bundle Identifier `com.microsoft.VSCodeInsiders` (full command: `open -n -b "com.microsoft.VSCodeInsiders" --args "$*"`) – Emil Devantie Brockdorff Aug 11 '21 at 11:38
  • 2
    Thanks a lot for the solution and introducing to one of the most underrated feature on mac, Automator. – Jatin Garg Sep 10 '21 at 06:17
  • I followed these steps, but when I click "Open in Visual .." it throws an error: The action “Run Shell Script” encountered an error: “The application /Users/saadzafar/Downloads/Visual Studio Code.app cannot be opened, error=Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"” – Saad Zafar Oct 13 '21 at 13:09
  • 15
    vs code is opening but its blank not opening the corresponding folder – PRAJIN PRAKASH Oct 29 '21 at 08:28
  • 3
    i changed to /usr/local/bin/code -n "$1" works fine – PRAJIN PRAKASH Oct 29 '21 at 09:22
  • 6
    The accepted answer doesn't work on Mac OS Monterey. You can force it to work by locating the workflow in ~/Library/Services (note ~/Library may be hidden), opening it in the new Shortcuts app and giving it permission to run scripts. Otherwise, you can create it from scratch in the Shortcuts app as described by @gluedpixel. – John Henry Nov 27 '21 at 00:29
  • Great answer! Pro tip: if you've changed your default terminal away from zsh you will want to set it back to zsh for this to work. I'm sure there's a way to get it to run with any terminal, but probably not worth exploring when zsh works just fine. – Raydot Jan 08 '22 at 21:54
  • 1
    Under "image" for the workflow, you can even select "own" (or "custom" - not sure, since I have no english language), and navigate to "Visual Studio Code.app" and choose it - that way you even have the VS Code App Icon as an Image for the quick action (at least on macOS Monterey)! – Steffen Funke Jan 16 '22 at 17:29
  • 8
    If your VS Code is opening blank make sure in your **Run Shell Script** section the option `Pass input` is set `as arguments`. – Gaurav Kispotta Apr 01 '22 at 08:50
  • 2
    Hearing some complaints down here about this method not working on new OSX versions. If that's your case, try the method suggested by @AD_Progress down below. Works well for me on Monteray and is definitely a cleaner solution. – Apekshik Panigrahi Apr 30 '22 at 22:23
  • I also got "Operation not permitted" and solved it following [this page](https://brianli.com/how-to-fix-automator-operation-not-permitted-error-in-macos-catalina/), which is to manually grant full disk access to Finder. Go to System Preferences > Security & Privacy > Privacy. Basically, scroll down on the left column and click on Full Disk Access. Click on the + button and add Finder. – Nadjib Mami Aug 06 '22 at 19:07
  • Opened an empty vs code window, but missed setting "Pass input" as "as arguments", seems it defaults as stdin – Devishankar Jan 26 '23 at 22:35
  • @GauravKispotta comments worked for me – footose Feb 01 '23 at 16:21
  • 1
    you saved me a lot of time. – Layhout Feb 21 '23 at 04:04
  • The second method is working fine with ventura – webchun Aug 27 '23 at 08:38
157

The simplest solution is to create a Quick Action with Open Finder Items: This way you don't need a shell script that might break with an OS update or VS Code update

  1. Launch Automator and select Quick Action or File > New > Quick Action If Automator is already open.

Quick Action

  1. Set Quick Action receives selected to files or folders and in to Finder. Files and Folders

  2. Choose an Image (icon) select Choose.. and double click Visual Studio Code in the window that pops up, this will set your quick action icon to the same as VS Code

  3. On the left-hand side where you see all the available items to add to your automation go into the search bar and type open, next choose Open Finder Items (you can either double click it or drag it to your workflow), Choose open with as Other.. select Visual Studio Code.

Choose VS Code as the app to open

  1. Finally, just hit CMD+S to save and name it Open in VSCode

Here is your final result: final-result

AD Progress
  • 4,190
  • 1
  • 14
  • 33
46

Well you need to understand. macOS has different ways to do things than windows and initially it might be a challenge. For starters you start to make use of Terminal. its beautiful.

Now answering your question.

Open your VS code and then, press CMD + SHIFT + P, type shell command and select Install code command in path. Afterwards, navigate to any project from the terminal and type code . from the directory to launch the project using VS Code.

Jitesh Dhamaniya
  • 1,326
  • 2
  • 8
  • 17
28

Using the new shortcuts app.

  1. Download:https://github.com/gluedpixel/shortcuts

  2. Or create it by yourself

  • Open Shortcuts app > Quick Actions
  • Change from receive any to Files and Folders
  • Add run shell script action
  • Paste the code open -n -b "com.microsoft.VSCode" --args "$*"
  • Change input to Shortcut input
  • Click on Shortcut input and set type to "Folder" & Get to File Path Shortcut overview image
gluedpixel
  • 292
  • 3
  • 6
  • 3
    make sure to also enable it in context menu items: quick actions -> customize -> tick – GorvGoyl Jan 30 '22 at 13:24
  • Not a request of the OP, but I added a Keyboard Shortcut for this using Preferences > Keyboard > Shortcuts. – RonH Apr 29 '22 at 14:26
21

There are some ways suggested here in the VS Code GitHub Issues tracker, but I would go with the first option of dragging the folder onto the app icon if you have VS Code icon in your dock!

If you really want to be able to do so from a right click, then this repository has a workflow solution. https://github.com/Sankra/OpenFolderInVSCode

jknlsn
  • 373
  • 1
  • 6
12

Drag and drop the folder from Finder onto an empty VSCode window.

puio
  • 1,208
  • 6
  • 19
7

I've actually managed to have it as an icon you can just add to the Finder window as such:

VSCode icon as a shortcut in the Finder window

The script will allow you to open the current folder path in VSCode.
The steps to do so are:

  1. Create a new Application with Automator
  2. Select the "Run shell script" from the menu and drag it to the editor
  3. Paste the following small script to the text area:
finderPath=`osascript -e 'tell application "Finder" to get the POSIX path of (target of front window as alias)'`
open -n -b "com.microsoft.VSCode" --args "$finderPath"
  1. Save the Application to the Applications folder
  2. Right click the app and drag and drop your desired VSCode icon to the blank area to the left of the name and close the Get Info window
  3. Press and hold the Command key and drag the Application to an empty space in the Finder title row like in the picture
  4. Test that it works by pressing on the icon. VSCode should now open with the content of the current folder in the navigation bar
raed115
  • 81
  • 1
  • 2
4

Found a way to achieve a 'Quick Action' that met my needs using the following steps using MacOS' Shortcuts app (No Automator needed):

if you are looking for screenshots as well, I wrote a small Notion page

  1. Open the ‘Shortcuts’ app on your Mac
  2. On the Left Pane, navigate to ‘Quick Actions’
  3. On the top bar, press the ➕ button to add New Shortcut.
  4. Click on ‘Any’ and change to ‘Files and Folders’ by selecting only those from the dropdown that appears on clicking the ‘Any’ field.
  5. Click on ‘Action Library’ on the right pane and search for ‘shell’. Drag and drop the ‘Run Shell Script’ onto the main pane. This will add the action to the Shortcut’s flow.
  6. In the shortcut details (on the right pane), make sure Use as Quick Action is ticked and so are ‘Finder’ and ‘Service Menu’.
  7. In the newly added action called ‘Run Shell Script’ write “code “ and right-click to get the below pop-up, go ahead ‘Insert variable’ and then ‘Shortcut Input’.
  8. Click on the ‘Shortcut Input’ tag that appears. A pop must appear, choose ‘File Path’ and just click anywhere else. This will change the tag to ‘File Path’.
  9. Set ‘Pass Input:’ option to ‘as arguments’.
  10. Be sure to set a cool name for your new Shortcut! I named mine a boring “Open in VSCode”.
  11. Open a new ‘Finder’ Instance, right-click on a folder, go to ‘Quick Actions’ > ‘Customize...’
  12. Make sure your Shortcut’s Name is selected. Exit the dialogue and Test!
Yashash Gaurav
  • 581
  • 5
  • 9
  • 1
    Now we just need someone to turn this into some sort of install script and get it included inside VSCode for Mac as an install option – MattG Aug 26 '22 at 04:05
3

There is a Terminal command named code, I would stick with it and you should get used to it as well if you are regularly using VS.

Follow the one-time setup with-in VS Code

  1. Open your VS code and press ⌘ CMD + SHIFT + P

  2. Type shell, and select Shell Command: Install 'code' command in PATH from the list

Open Terminal and hit any of the commands below:

code <path-to-folder>

OR

cd <path-to-folder>
code .

OR

cd <path-to-folder> && code .
AamirR
  • 11,672
  • 4
  • 59
  • 73
2

I find this to be the best solution out there: https://github.com/RoadToDream/SzContext

enter image description here

Satan
  • 133
  • 6
1

Here is an alternative derived from the accepted answer. Indeed the accepted solution by @jnovack just opens VS Code for me, but not the desired folder. If VS Code is already running it just switches the focus to a running instance of VS Code.

Solution

Provided that you have the code CLI command installed (if not, go to VS Code and do CMD+SHIT+P>"Shell Command: install 'code' in PATH")

Then open a terminal and run which code. You should get the code executable path looking somethin like /usr/local/bin/code

Then, as per @jnovack 's answer,

  1. Open Automator
  2. Create a new Document (CMD+N)
  3. Create a new Quick Action
  4. Workflow receives current files and folders from Finder.
  5. Add a new Run Shell Script action to the workflow. (drag and drop the "Run Shell Script" object)
  6. Pass input as arguments
  7. Paste this code in the shell command field /usr/local/bin/code -n "$*" or replace with your path to the code executable if it differs from this one.
  8. Save the action

Tbh. I would have expected to be able to use just code -n "$*", but for some reason, the PATH used by the automator's shell seems to be different from that used in the terminal. I'd welcome some input on that matter.

Mazata
  • 73
  • 6
0

It's the simplest by creating a Shortcut under Qucik Actions:

enter image description here

Then edit:

enter image description here

Or downloading from my sharing link:

https://www.icloud.com/shortcuts/a34bf6a4e0ec463a9fb59343b5167dff

liviaerxin
  • 579
  • 6
  • 13
  • Link is dead... – itsji10dra Aug 02 '23 at 08:29
  • You have to explore "open file" action with two parameters where the first parameter would be "shortcut input" and its type should be "folder". make sure "show open in menu" is unchecked as it invalidates the "default App" parameter if you checked it. – Nishain de Silva Aug 02 '23 at 13:30