233

On my Windows PC, I would just right-click a folder and select "Open with Code" to open it in VS Code, which saved me time when looking through lots of code online. On a Mac, however, I do not have this option. I have to first open VS Code, then click "Open" to open a new folder.

How do I directly open a folder in VS Code from Finder on macOS?

1
  • 2
    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. Commented Oct 4, 2022 at 13:21

12 Answers 12

455

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.

  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


If you want BOTH the code . from the command-line AND an Automator Action, use this as your Automator action.

/usr/local/bin/code -n "$*" 

enter image description here

Sign up to request clarification or add additional context in comments.

20 Comments

There is. That's the "Image" icon in the second screenshot. Select from a list or add your own.
vs code is opening but its blank not opening the corresponding folder
i changed to /usr/local/bin/code -n "$1" works fine
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.
If your VS Code is opening blank make sure in your Run Shell Script section the option Pass input is set as arguments.
|
248

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

  4. In the Open Finder Items section choose open with select Other.. then from the popup list select Visual Studio Code. Please see the screenshot below.

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

12 Comments

I would consider this as cleaner solution ❤️
only solution that worked with latest macOs
For me Visual Studio Code is blurred out :/
@ZeeshanAhmadKhalil just had the same problem. Didn't find any info on it other than the comments here. But I tried something kinda stupid and it just worked. When you are in the selection screen for the "Other..." app, just drag VS Code (or any other app) from the selection pane onto the selection menu in Automator XD.
@ZeeshanAhmadKhalil You need to register VS Code as an app by dragging it's package file (it's probably in Downloads) to Applications. After that, you'll see it in app list
|
45

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.

6 Comments

Thanks @emmaakachukwu and krummens
I mean, yes it works but that is not what OP is asking.
@distante because op needs to understand mac is different than windows. and should learn how to use mac rather making mac working as windows.
@JiteshDhamaniya Mac also has open with. It is not a windows-only feature.
Downgrading because it doesn't answer the OP request, and requires going to a terminal window in order to open a folder that is currently visible in Finder. It doesn't make sense to go from a UI (Finder) to a command prompt (Terminal) to then launch a UI application (VS Code).
|
29

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

2 Comments

make sure to also enable it in context menu items: quick actions -> customize -> tick
Not a request of the OP, but I added a Keyboard Shortcut for this using Preferences > Keyboard > Shortcuts.
25

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

5 Comments

here is updated cloned repo to allow opening of both files/folders in VSCode via finder shortcut: github.com/Pr0to7yp3/OpenInVSCode
Thanks, "dragging the folder onto the app icon" is probably the fastest way to open a folder in VS Code and pretty easy.
This is a very underrated answer, as this works particularly very easily in this usecase.
This one is perfect!
Nice, the OpenFolderInVSCode workflow has the benefit of creating a new window in VSCode – a lot of workflow solutions open the folder in the existing window.
20

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

enter image description here

6 Comments

Not sure why this is not upvoted, it is ✅✅✅THE BEST✅✅✅ solution!
I guess it's because of my name, @Tom. ;) Thanks though :)
Maybe because it receives no updates: github.com/RoadToDream/SzContext/releases? Upvoted because this really is the most useful solution, which does not require an additional click as the Automator approaches do.
I agree with the other commenters here. This is a very good solution!
I think this utility is a better solution: OpenInTerminal
|
14

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

2 Comments

this is exactly what i was looking for, to get an icon in the finder toolbar, works perfectly, thanks!
This is also a very good solution. It open the folder you are in, so I combined this with Satan's solution above, to also open specific files.
13

Drag and drop the folder on the VSCode icon in the Dock.

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

Comments

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!

3 Comments

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
I don't have this 'Any' in step four. I just upgraded to Sonoma and didn't ever use this feature on previous versions. Could you add images?
@hippietrail - seems they have changed the shortcuts app to show Receive {Images and 18 more} input from {Quick Actions} - by clicking on {Images and 18 more} - you can select just files and folders for which this quick option shows up for you. Hope that helps.
2

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 . 

2 Comments

This is much better solution
This is not an answer to the question. Yes, it's an alternative, but doesn't help the OP.
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.

Comments

1

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

enter image description here

Then edit:

enter image description here

Finally you could open a file or folder like:

enter image description here

Or downloading from my sharing link(Updated):

https://www.icloud.com/shortcuts/53452a90b10346a5b2abcdeba6920fb3

5 Comments

Link is dead...
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.
This seems to be lacking important steps/details for people who have never used this before. When I go into Quick Actions I don't see Open With Visual Studio Code. I'm assuming but not sure this is the end result in which case it should be the last picture? I can create all the Receive part, but I can't see what to do next to create the Open part.
Well I fumbled around and recreated this exactly as shown. But I don't see how to use it. I see no new options when I right-click on a file or folder in Finder etc.
It should be the sub-level, when you right click a file or folder, 1. you should see Quick Actions, moving mouse on it. 2. then name like Open With VS Code will show up.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.