I want to open a folder in Visual Studio Code when I press a button. Can I bind this with AutoHotkey v1?
-
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.Community– Community Bot2023-11-28 22:13:06 +00:00Commented Nov 28, 2023 at 22:13
-
What button are you pressing?Mister Robato– Mister Robato2023-12-07 15:43:29 +00:00Commented Dec 7, 2023 at 15:43
Add a comment
|
2 Answers
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.