0

I want to open a folder in Visual Studio Code when I press a button. Can I bind this with AutoHotkey v1?

2
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Commented Nov 28, 2023 at 22:13
  • What button are you pressing? Commented Dec 7, 2023 at 15:43

2 Answers 2

0

You can do this with the following hotkey code, replace C:\Deforum\ with the path you would like to open in VSCode, be sure to use the trailing slash:

vscodepath := "C:\Users\" . A_UserName . "\AppData\Local\Programs\Microsoft VS Code\Code.exe C:\Deforum\"

^!1::
Run % vscodepath
return
Sign up to request clarification or add additional context in comments.

Comments

0

I wanted to open the Windows File Explorer's current folder in Visual Studio Code by pressing F9, and this code works for me very well:

#HotIf WinActive("ahk_class CabinetWClass")
~F9::
{ 
    vscodepath := "C:\Program Files\Microsoft VS Code\Code.exe" 
    if !FileExist(vscodepath)
    { 
        vscodepath := "C:\Users\" . A_UserName . "\AppData\Local\Programs\Microsoft VS Code\Code.exe"        
    }

    Send("!d")
    Sleep(100)
    Send("^c")
    Sleep(100)
    command := vscodepath . " " . A_Clipboard    
    A_Clipboard := ""
    Run(command)
    Return
}
#HotIf
Return
  • Some explanation: On one machine my VS was in "Program Files", in another machine it was under the user/appdata, so the if handles that situation. The code basically does this: sends Alt+D => to put the focus on the File Explorer address bar, then sends Ctrl+C to copy the path, then combines the VS Code exe path and the current folder to run the command. The top #HotIf WinActive("ahk_class CabinetWClass") makes sure this runs only if the active window is Windows File Explorer, and based on personal experience, if I don't add the empty if at the end, the code execution won't flow to the rest of the AHK file.
  • Why F9? Because I'm using F12 for opening the current folder in Windows Terminal, and F11 and F10 are already in use by Windows Explorer.
  • The code compiles in AHK v2, and I have not tested it in v1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.