Quick tutorial for setting up VSCode for AHK

Scripting and setups with Visual Studio Code (vscode) and AutoHotkey.
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Quick tutorial for setting up VSCode for AHK

Post by boiler » 15 Apr 2022, 09:59

I was asked for quick instructions on how to set up VSCode for AHK use, and it was suggested I post it here as it might benefit others. It's pretty easy, but it might not be clear if you're new to VSCode:

1. Download and run the Visual Studio Code installer.

2. Install an AHK extension: Open VSCode and select Extensions by either clicking the icon on the left bar (looks like four squares with one separated from the others) or by pressing Ctrl+Shift+X. Search for AutoHotkey and select your desired extension. You can also browse extensions at the VSCode Extension Marketplace. I currently use AutoHotkey Plus Plus after also using its predecessors that it was forked from.

3. (Optional) Set the default language for AHK so the AHK extension will work before even saving the file with a .ahk file extension (i.e., AHK syntax highlighting and everything else is applied to new windows that aren't saved yet): Go to the File menu, then Preferences, then Settings, or press Ctrl+,. With the Settings open, press Ctrl+F and search for "default language" and put in ahk.

Once you have set up one instance with your settings and extensions, it's easy to sync settings across multiple machines, which I find extremely useful as I have these and many other changes such as theme settings that are instantly applied to my other machines (virtual or otherwise) when I choose.

lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: Quick tutorial for setting up VSCode for AHK

Post by lexikos » 22 Apr 2022, 01:56

If you're using v2, I would recommend AutoHotkey2 Language Support by thqby.

It is possible to use multiple language extensions, such as for v1 and v2. The current language is shown at the bottom-right of the screen. Clicking it brings up a menu to switch the language for the current file. This menu also includes an option "Configure File Association for '.ahk'" (or presumably whatever extension the current file has) to change the default language for files of that type.

OpalMonkey
Posts: 18
Joined: 23 Jan 2014, 03:02

Re: Quick tutorial for setting up VSCode for AHK

Post by OpalMonkey » 24 Apr 2022, 08:07

It might be worth noting that if using thqby's AutoHotkey2 Language Support, I believe debugging requires it's own extension. I use vscode-autohotkey-debug. Whereas it looks like AutoHotkey Plus Plus can handle debugging on its own, but I could be mistaken since I haven't used it much.

User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Quick tutorial for setting up VSCode for AHK

Post by boiler » 01 Sep 2022, 08:01

It's probably worth adding here how to set up VSCode to be the default editor for .ahk files. Use the Registry Editor to point to the VSCode executable path here:
ahk editor regedit.png
ahk editor regedit.png (16.73 KiB) Viewed 29067 times

Your VSCode executable may be located in AppData > Local > Programs instead of your Program Files folder.

lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: Quick tutorial for setting up VSCode for AHK

Post by lexikos » 01 Sep 2022, 16:41

If you install AutoHotkey v2, it will ask you which editor to use the first time you attempt to edit a script. VS Code should be detected automatically, so you can select it from the list. This list can also be accessed from the Dash at any time, to change the default editor.

User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Quick tutorial for setting up VSCode for AHK

Post by boiler » 01 Sep 2022, 18:54

That's a nice feature. Thanks for adding it.

mmmax
Posts: 88
Joined: 25 Jun 2018, 09:01

Re: Quick tutorial for setting up VSCode for AHK

Post by mmmax » 30 Jan 2023, 23:06

On Windows 10, the above didn't work for me when using the "Edit This Script" option in the right-click/context menu of the AHK tray icon. This, however, solved the problem.

www.autohotkey.com/board/topic/23889-how-to-edit-this-script-in-any-editor-other-than/?p=543108

For future-proofing, here's the text from the linked post:
Below is a .reg script that changes editor to use when picking an option "Edit this script" from tray icon menu.

Code: Select all

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Applications\AutoHotkey.exe\shell\edit]

[HKEY_CLASSES_ROOT\Applications\AutoHotkey.exe\shell\edit\command]
@="\"c:\\Program Files (x86)\\Notepad++\\notepad++.exe\" \"%1\""

I prefer Sublime Text, so I modified the .reg file as follows.

Code: Select all

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Applications\AutoHotkey.exe\shell\edit]

[HKEY_CLASSES_ROOT\Applications\AutoHotkey.exe\shell\edit\command]
@="\"C:\\Program Files\\Sublime Text\\sublime_text.exe\" \"%1\""


User avatar
Curdflappers
Posts: 11
Joined: 13 Nov 2020, 03:18
Contact:

Re: Quick tutorial for setting up VSCode for AHK

Post by Curdflappers » 07 Feb 2023, 23:02

OpalMonkey wrote:
24 Apr 2022, 08:07
It might be worth noting that if using thqby's AutoHotkey2 Language Support, I believe debugging requires it's own extension. I use vscode-autohotkey-debug. Whereas it looks like AutoHotkey Plus Plus can handle debugging on its own, but I could be mistaken since I haven't used it much.
Yep, AHK has basic debugging support, but if you need something fancy then vscode-autohotkey-debug is great. And we're working on v2 support now so that folks don't have to install multiple extensions just to write code in one language!

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

Re: Quick tutorial for setting up VSCode for AHK

Post by jsong55 » 08 Apr 2023, 05:45

Got a question regarding running scripts off VSCode. It seems that when I run another script VSCode command line exits the currently running v2 script. How do we run multiple?

User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Quick tutorial for setting up VSCode for AHK

Post by boiler » 08 Apr 2023, 06:52

Do you mean you want to run multiple scripts from VSCode? It may depend on the plug-in, but in general, you shouldn’t be using VSCode as a launcher. Using an editor to launch code should be for checking a script while developing it, not to be used as your general approach for launching scripts.

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

Re: Quick tutorial for setting up VSCode for AHK

Post by jsong55 » 08 Apr 2023, 07:13

I like to use VScode to launch them because I keep editing and making improvements.

Other than double clicking the ahk file how else should we run them ?

User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Quick tutorial for setting up VSCode for AHK

Post by boiler » 08 Apr 2023, 08:01

jsong55 wrote:
08 Apr 2023, 07:13
Other than double clicking the ahk file how else should we run them ?
That's the way I suggest. Not sure why you're looking for another way. You could also put a shortcut to them in the Windows Startup folder and other approaches that are used to launch apps when appropriate.

TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: Quick tutorial for setting up VSCode for AHK

Post by TXShooter » 17 Jul 2023, 18:17

Reviving.

How do we pass command line arguments in VSCode, either for testing or especially for debugging? Are instructions available for modifying the launch.json or creating a new one?

User avatar
thqby
Posts: 560
Joined: 16 Apr 2021, 11:18
Contact:

Re: Quick tutorial for setting up VSCode for AHK

Post by thqby » 19 Jul 2023, 10:42

for v2, install thqby.vscode-autohotkey2-lsp and zero-plusplus.vscode-autohotkey-debug
Open the ahk file, and right-click menu select Debug Script with Params.

nhan
Posts: 12
Joined: 18 Mar 2023, 13:22

Re: Quick tutorial for setting up VSCode for AHK

Post by nhan » 08 Sep 2023, 22:27

Thanks boiler for the quick guide and others for suggesting useful plugins.

Is there a way to make a table of contents for an AHK file in VSCode? I'm approaching 600 lines in my AHK file and it's a bit wieldy scrolling up and down. It'd be nice if I can specify an outline/table of contents that can be folded/unfolded. The default Outline pane in VSCode isn't very helpful because it just lists all the keys.

CPager
Posts: 10
Joined: 13 Apr 2022, 15:45

Re: Quick tutorial for setting up VSCode for AHK

Post by CPager » 13 Nov 2023, 10:09

jsong55 wrote:
08 Apr 2023, 07:13
I like to use VScode to launch them because I keep editing and making improvements.

Other than double clicking the ahk file how else should we run them ?
To quickly and easily launch my scripts, I wrote a script that I named: V2_AHK_Script_Launcher.ahk
This script works in AHK V2 version. This is a GUI that launches automatically when Windows opens.
This GUI allows me to launch any of my scripts with a "Double Click", regardless of whether it is written in V1 or V2.
As my scripts are divided into two folders, "Desktop or Documents", I added the two corresponding tabs.
I also added a third tab to also access my "Python" scripts.
The GUI minimizes itself in the taskbar, after launching a script, and thus remains accessible at all times.

I am currently working on adding a "ContextMenu" which would allow you to choose between "Launch" or "Edit" the targeted script.
Ideally, this context menu would offer 4 choices: "Open - Edit in Scite4AHK - Edit in VSCode - Copy Full Path".
I built this script from example #3 at the bottom of the "ListView" page in the AutoHotkey.com V2 Documentation
Here is the link to this page: https://www.autohotkey.com/docs/v2/lib/ListView.htm#Examples Example #3

Here is also a copy of this script, without the "ContextMenu", since it is currently under construction.
To personalize it, simply modify the path of the 3 “Loop Files”, Lines 18, 29 and 39.
NB. ( \*.ahk ) and ( \*.py ) are used to filter the results, in order to display only files containing the extension (.ahk or .py).

Code: Select all

#Requires Autohotkey v2.0
#SingleInstance Force
	; ^ =Ctrl - # =Win - ! =Alt - + =Shift

Gui1 := Gui()
RetrievedTitle := Gui1.Title
Gui1.SetFont("s12")  ; Set a font size (12-point).
Gui1.Add("Text",, "Pick a file to launch from the list below.")
Tab  := Gui1.AddTab3(, ["   AHK Desktop Folder   ","   AHK Documents Folder   ","   Python Folder   "])

; LV1
Tab.UseTab(1)
LV1 := Gui1.Add("ListView", "r20 w700", ["Name","In Folder","Size (KB)","Type"])
		LV1.ModifyCol(1, 280)  ; Auto-size each column to fit its contents.
        LV1.ModifyCol(2, 270)  ; Auto-size each column to fit its contents.
        LV1.ModifyCol(3, 70)  ; Make the Size column at little wider to reveal its header.
        LV1.ModifyCol(4, 50,)  ; Make the Size column at little wider to reveal its header.
			Loop Files, "C:\Users\Claude\Desktop\AutoHotkey\*.ahk"  ; Change UserName
        LV1.Add(, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, A_LoopFileExt)


; LV2
Tab.UseTab(2)
LV2 := Gui1.Add("ListView", "r20 w700", ["Name","In Folder","Size (KB)","Type"])
		LV2.ModifyCol(1, 280)  ; Auto-size each column to fit its contents.
        LV2.ModifyCol(2, 270)  ; Auto-size each column to fit its contents.
        LV2.ModifyCol(3, 70)  ; Make the Size column at little wider to reveal its header.
        LV2.ModifyCol(4, 50,)  ; Make the Size column at little wider to reveal its header.
			Loop Files, "C:\Users\Claude\Documents\AutoHotkey\Lib\*.ahk"  ; Change UserName
        LV2.Add(, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, A_LoopFileExt)

; LV3
Tab.UseTab(3)
LV3 := Gui1.Add("ListView", "r20 w700", ["Name","In Folder","Size (KB)","Type"])
		LV3.ModifyCol(1, 280)  ; Auto-size each column to fit its contents.
        LV3.ModifyCol(2, 270)  ; Auto-size each column to fit its contents.
        LV3.ModifyCol(3, 70)  ; Make the Size column at little wider to reveal its header.
        LV3.ModifyCol(4, 50,)  ; Make the Size column at little wider to reveal its header.
			Loop Files, "C:\Users\Claude\Desktop\Script Python\*.py"  ; Change UserName
        LV3.Add(, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, A_LoopFileExt)

gui1.Show

; Events
LV1.OnEvent('DoubleClick', RunFile)
LV2.OnEvent('DoubleClick', RunFile)
LV3.OnEvent('DoubleClick', RunFile)

RunFile(LV, RowNumber)
{
    FileName := LV.GetText(RowNumber, 1) ; Get the text of the first field.
    FileDir := LV.GetText(RowNumber, 2)  ; Get the text of the second field.
    try
        Run(FileDir "\" FileName)
    catch
        MsgBox("Could not open " FileDir "\" FileName ".")
    WinMinimize()
}


User avatar
michaelbeijer
Posts: 83
Joined: 04 Oct 2014, 08:01
Location: Hastings, UK.
Contact:

Re: Quick tutorial for setting up VSCode for AHK

Post by michaelbeijer » 10 Jan 2024, 12:00

lexikos wrote:
01 Sep 2022, 16:41
If you install AutoHotkey v2, it will ask you which editor to use the first time you attempt to edit a script. VS Code should be detected automatically, so you can select it from the list. This list can also be accessed from the Dash at any time, to change the default editor.
Thanks, it was very easy to change it from my previous SciTEAutoHotkey to VS Code in the Dash.
Michael Beijer
Dutch-English technical translator
Hastings, United Kingdom
Email: michael[at]beijer.uk
Website: Beijer.uk
Terminology: Beijerterm.com
AHK-learning project: Beijer.bot
Proz profile: proz.com/profile/652138
LinkedIn: linkedin.com/in/michael-beijer

Post Reply

Return to “Visual Studio Code”