'Open a folder in vscode through Finder in macOS?

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?



Solution 1:[1]

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

    • 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)
  5. Configure the Workflow

    • 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

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

Solution 2:[2]

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. Add Open Finder Items, Choose open with as Other.. select Visual Studio Code and save your Quick Action as Open in VSCode optionally choose an Image for your Quick Action job done.

Choose VS Code as the app to open

Solution 3:[3]

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.

Solution 4:[4]

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

Solution 5:[5]

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

Solution 6:[6]

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

Solution 7:[7]

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!

Solution 8:[8]

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.

Solution 9:[9]

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 .

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ahmad Awais
Solution 2
Solution 3 Jitesh Dhamaniya
Solution 4
Solution 5 jknlsn
Solution 6 puio
Solution 7 Yashash Gaurav
Solution 8
Solution 9 AamirR