Open a txt file in VSCode using Run

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jsong55
Posts: 218
Joined: 30 Mar 2021, 22:02

Open a txt file in VSCode using Run

Post by jsong55 » 28 Mar 2023, 21:17

When I hit Win+R and type

code "C:\Users\abc\Downloads\mytextfile.txt"

it works perfectly, unfortunately not this code line in ahk

Code: Select all

run,% "C:\Users\abc\AppData\Local\Programs\Microsoft VS Code\Code.exe " "C:\Users\abc\Downloads\mytextfile.txt"

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Open a txt file in VSCode using Run

Post by mikeyww » 28 Mar 2023, 21:28

Hello,

Your path to the text file is unquoted. Add the quotation marks to the string itself.

An alternative is below.

Code: Select all

#Requires AutoHotkey v1.1.33
EnvGet localAppData, LOCALAPPDATA
downloads := downloadsDir()
filePath  := downloads "\test.txt"
Run % localAppData "\Programs\Microsoft VS Code\Code.exe """ filePath """"

downloadsDir() {
 ; Adapted from teadrinker: https://www.autohotkey.com/boards/viewtopic.php?p=284248#p284248
 Static FOLDERID_Downloads := "{374DE290-123F-4565-9164-39C4925E467B}"
 RegRead v, HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders, % FOLDERID_Downloads
 VarSetCapacity(downloads, (261 + !A_IsUnicode) << !!A_IsUnicode)
 DllCall("ExpandEnvironmentStrings", "Str", v, "Str", downloads, "UInt", 260)
 Return downloads
}
Run:
If the program/document name or a parameter contains spaces, it is safest to enclose it in double quotes (even though it may work without them in some cases). For example, Run, "My Program.exe" "param with spaces".

jsong55
Posts: 218
Joined: 30 Mar 2021, 22:02

Re: Open a txt file in VSCode using Run

Post by jsong55 » 28 Mar 2023, 21:58

Thanks Mikey it does work, but only when VSCode is closed. If VSCode is open in a workspace, nothing happens. Hmm this is likely a issue with VSCode itself, know the right flags to make it open in a new tab in current workspace?

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Open a txt file in VSCode using Run

Post by mikeyww » 28 Mar 2023, 22:06

It worked here, open or closed. I changed no installation settings; just using defaults.

jsong55
Posts: 218
Joined: 30 Mar 2021, 22:02

Re: Open a txt file in VSCode using Run

Post by jsong55 » 29 Mar 2023, 01:49

I'm wondering if there is a way to get it triggered the way the run menu natively from windows works?

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Open a txt file in VSCode using Run

Post by mikeyww » 29 Mar 2023, 05:31

It's working the same way in my test, so I am not reproducing your issue. Others here may have more information for you.

RussF
Posts: 1229
Joined: 05 Aug 2021, 06:36

Re: Open a txt file in VSCode using Run

Post by RussF » 29 Mar 2023, 05:56

I can't say for sure if it makes a difference, but here's something to try.

At the Run prompt, or in a Command window, if you type code myfile.txt, the system searches through the paths (in order) that are present in the PATH environment variable. When I type path in a Command window, the search path for VSCode is C:\Users\myuser\AppData\Local\Programs\Microsoft VS Code\bin. Note that the file CODE.EXE is located in the Microsoft VS Code folder, but what is actually executed when you type code myfile.txt is CODE.CMD which is located in Microsoft VS Code\bin. This CMD file adds some additional arguments to your command line before actually calling CODE.EXE. Perhaps try calling CODE.CMD in the \bin folder instead? Can't say for sure - your mileage may vary.

Russ

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Open a txt file in VSCode using Run

Post by mikeyww » 29 Mar 2023, 06:03

Good point, Russ.

I think that the replication of the run line is more straightforward, too, as ComSpec should handle the command the same way.

Code: Select all

#Requires AutoHotkey v1.1.33
EnvGet localAppData, LOCALAPPDATA
downloads := downloadsDir()
filePath  := downloads "\test.txt"
SoundBeep 1500
RunWait % ComSpec " /c code """ filePath """",, Hide

downloadsDir() {
 ; Adapted from teadrinker: https://www.autohotkey.com/boards/viewtopic.php?p=284248#p284248
 Static FOLDERID_Downloads := "{374DE290-123F-4565-9164-39C4925E467B}"
 RegRead v, HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders, % FOLDERID_Downloads
 VarSetCapacity(downloads, (261 + !A_IsUnicode) << !!A_IsUnicode)
 DllCall("ExpandEnvironmentStrings", "Str", v, "Str", downloads, "UInt", 260)
 Return downloads
}

jsong55
Posts: 218
Joined: 30 Mar 2021, 22:02

Re: Open a txt file in VSCode using Run

Post by jsong55 » 30 Mar 2023, 04:09

mikeyww wrote:
29 Mar 2023, 06:03
Good point, Russ.

I think that the replication of the run line is more straightforward, too, as ComSpec should handle the command the same way.

Code: Select all

#Requires AutoHotkey v1.1.33
EnvGet localAppData, LOCALAPPDATA
downloads := downloadsDir()
filePath  := downloads "\test.txt"
SoundBeep 1500
RunWait % ComSpec " /c code """ filePath """",, Hide

downloadsDir() {
 ; Adapted from teadrinker: https://www.autohotkey.com/boards/viewtopic.php?p=284248#p284248
 Static FOLDERID_Downloads := "{374DE290-123F-4565-9164-39C4925E467B}"
 RegRead v, HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders, % FOLDERID_Downloads
 VarSetCapacity(downloads, (261 + !A_IsUnicode) << !!A_IsUnicode)
 DllCall("ExpandEnvironmentStrings", "Str", v, "Str", downloads, "UInt", 260)
 Return downloads
}
Million thanks @mikeyww WORKS like a charm

Post Reply

Return to “Ask for Help (v1)”