Skip to content

Instantly share code, notes, and snippets.

@idleberg
Last active July 13, 2024 18:11
Show Gist options
  • Save idleberg/bc65021a736e9139e3e31f7f2c761d5d to your computer and use it in GitHub Desktop.
Save idleberg/bc65021a736e9139e3e31f7f2c761d5d to your computer and use it in GitHub Desktop.
“Open in Visual Studio Code” in macOS context-menu

Open in Visual Studio Code

  • Open Automator

  • Create a new document

  • Select Quick Action

  • Set “Service receives selected” to files or folders in any application

  • Add a Run Shell Script action

    • your default shell should already be selected, otherwise use /bin/zsh for macOS 10.15 (”Catalina”) or later
    • older versions of macOS use /bin/bash
    • if you're using something else, you probably know what to do 😉
  • Set the script action to the following

    for f in "$@"; do
      open -a 'Visual Studio Code' "$@"
    done
    
  • Set “Pass input” to as arguments

  • Save as Open in Visual Studio Code

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 Visual Studio Code” 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 Visual Studio Code”
  • Edit and save

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

@jkeston
Copy link

jkeston commented May 23, 2023

Thanks! This worked to create a context menu item, but I can't seem to get a keyboard shortcut working for it. I've tried a bunch of different shortcuts making sure they weren't already in use and restarting after setting the shortcut. Any other suggestions?

@DaiZack
Copy link

DaiZack commented Jun 29, 2023

This would be easier and worked for me:

Open Automator

Create a new document

Select Quick Action

image

@me-tusharchandra
Copy link

Is there any way we can make it a shortcut and use it from the touchbar ??

@pushkarsingh32
Copy link

Thank you it even works on new version.

@adrixvoid
Copy link

@DaiZack solution worked for me

@elloop
Copy link

elloop commented Sep 2, 2023

This would be easier and worked for me:

Open Automator

Create a new document

Select Quick Action

image

good and simple solution. worked for me too. thanks @DaiZack

@bkaan99
Copy link

bkaan99 commented Sep 5, 2023

thanks for easy tutorial 👍🏻

@juji
Copy link

juji commented Sep 18, 2023

This would be easier and worked for me:

Open Automator

Create a new document

Select Quick Action

image

this works best, IMHO

@VirusEcks
Copy link

This would be easier and worked for me:

Open Automator

Create a new document

Select Quick Action

image

this is way easier method, Thanks 😄

@anandbibek
Copy link

If you've code installed in your path, the script could be as simple as

code "$@"

This also prevents the problem of vs code opening multiple windows when multiple folders are selected.

Screenshot 2023-10-21 at 1 27 48 PM

@lony
Copy link

lony commented Nov 2, 2023

@anandbibek Did you update to Sonoma already? I cannot find the "Receive Files and 2 more inputs from Quick Actions" even the icon is not available, and I would love to find out if it's an update problem or if I did something wrong?

@anandbibek
Copy link

@lony No. I'm still on Ventura.

@accidental-feature
Copy link

This would be easier and worked for me:

Open Automator

Create a new document

Select Quick Action

image

Best solution! Thanks a million!

@lony
Copy link

lony commented Nov 6, 2023

Screenshot from macOS Shortcut

I got help from a coworker and solved it. You have to select the quick action field.

@misaelabanto
Copy link

This would be easier and worked for me:

Open Automator

Create a new document

Select Quick Action

image

It worked, thank you!

@x-tropy
Copy link

x-tropy commented Feb 21, 2024

although there is probably very little practical impact, it would be better to replace, IMHO:

for f in "$@"; do
  open -a 'Visual Studio Code' "$@"
done

with

for f in "$@"; do
  open -a 'Visual Studio Code' "$f"
done

personally prefer $f without quotes, to open each folder as a seperate vscode window (rather than group them together as a workspace).

Those who prefer this way may try it out 😉

@icecreamsandwich
Copy link

This would be easier and worked for me:

Open Automator

Create a new document

Select Quick Action

image

thanks it worked 👍

@Minarcissist
Copy link

Lovely, thank you! <3

@SuvanCheng
Copy link

感谢您!!!
thx a lot!!!
@DaiZack

@yudopr
Copy link

yudopr commented May 31, 2024

Is there any way we can make it a shortcut and use it from the touchbar ??

you can customize the touch bar to include a quick action button image
once you tap it, the Open in Visual Studio Code option should be there.

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