Snippets in VSCode

Scripting and setups with Visual Studio Code (vscode) and AutoHotkey.
jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Snippets in VSCode

Post by jsong55 » 15 May 2021, 22:59

Just creating this topic for anyone that codes a lot in VSCode.

Any good snippet plugin and tutorials for the AHK language?

Perhaps we can all create some good snippets to share... Here's one I just made for a start

First hit Ctl+Shift+P in VSCode and Bring up Usersnippets json
Paste the following code inside

Creating a brand new Gui with options

Code: Select all

  "Gui new with name label and options": {
    "prefix": "Gui new with name label and options",
    "body": [
      "Gui, ${1:GuiName}: New",
      "Gui, ${1:GuiName}: +Label${1:GuiName} ${2|+Resize,-Resize,+Caption,-Caption,+AlwaysOnTop,-Border|} ${3|+Resize,-Resize,+Caption,-Caption,+AlwaysOnTop,-Border|} ${4|+Resize,-Resize,+Caption,-Caption,+AlwaysOnTop,-Border|}"
    ],
    "description": "Gui new with name label and options"
  }
Adding a new button, text, edit or combobox

Code: Select all

"Gui named button text or edit": {
    "prefix": "Gui named button text or edit",
    "body": [
      "Gui, ${TM_SELECTED_TEXT}: Add, ${1|Text,Edit,Button,ComboBox|}, % \"v${2:Var} g${3:Sub} ${4|xm ym,x+section,xm y+section|}\", % \"$5\""
    ],
    "description": "Gui named with button text or edit"
  },
In a new window where ahk is the coding language type out the name of a Gui (see example below) select it and type exactly "gnbt" without quotes

If you want to create a gui called

Code: Select all

Gui, calculator: New
Gui, calculator: +labelCalculator
then you can type out "calculator" and select it before typing "gnwbt"
Last edited by jsong55 on 16 May 2021, 00:26, edited 4 times in total.

User avatar
jasc2v8
Posts: 59
Joined: 10 Dec 2020, 12:24
Contact:

Re: Snippets in VSCode

Post by jasc2v8 » 15 May 2021, 23:24

There are several AHK Plugins for VSCode.
My fav is referenced here: https://www.autohotkey.com/boards/viewforum.php?f=89
The VSCode help files are very good to download, install, and use a plugin.

gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Snippets in VSCode

Post by gregster » 15 May 2021, 23:31

jasc2v8 wrote:
15 May 2021, 23:24
There are several AHK Plugins for VSCode.
My fav is referenced here: https://www.autohotkey.com/boards/viewforum.php?f=89
The VSCode help files are very good to download, install, and use a plugin.
I think the OP's question is specifically about managing code snippets, not generally about AHK plugins for VS Code.

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

Re: Snippets in VSCode

Post by jsong55 » 15 May 2021, 23:45

gregster wrote:
15 May 2021, 23:31
jasc2v8 wrote:
15 May 2021, 23:24
There are several AHK Plugins for VSCode.
My fav is referenced here: https://www.autohotkey.com/boards/viewforum.php?f=89
The VSCode help files are very good to download, install, and use a plugin.
I think the OP's question is specifically about managing code snippets, not generally about AHK plugins for VS Code.
Yea, I updated my original post with an example snippet

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

Re: Snippets in VSCode

Post by jsong55 » 16 May 2021, 23:32

jsong55 wrote:
15 May 2021, 22:59
Just creating this topic for anyone that codes a lot in VSCode.

Any good snippet plugin and tutorials for the AHK language?

Perhaps we can all create some good snippets to share... Here's one I just made for a start

First hit Ctl+Shift+P in VSCode and Bring up Usersnippets json
Paste the following code inside

Creating a brand new Gui with options

Code: Select all

  "Gui new with name label and options": {
    "prefix": "Gui new with name label and options",
    "body": [
      "Gui, ${1:GuiName}: New",
      "Gui, ${1:GuiName}: +Label${1:GuiName} ${2|+Resize,-Resize,+Caption,-Caption,+AlwaysOnTop,-Border|} ${3|+Resize,-Resize,+Caption,-Caption,+AlwaysOnTop,-Border|} ${4|+Resize,-Resize,+Caption,-Caption,+AlwaysOnTop,-Border|}"
    ],
    "description": "Gui new with name label and options"
  }
Adding a new button, text, edit or combobox

Code: Select all

"Gui named button text or edit": {
    "prefix": "Gui named button text or edit",
    "body": [
      "Gui, ${TM_SELECTED_TEXT}: Add, ${1|Text,Edit,Button,ComboBox|}, % \"v${2:Var} g${3:Sub} ${4|xm ym,x+section,xm y+section|}\", % \"$5\""
    ],
    "description": "Gui named with button text or edit"
  },
In a new window where ahk is the coding language type out the name of a Gui (see example below) select it and type exactly "gnbt" without quotes

If you want to create a gui called

Code: Select all

Gui, calculator: New
Gui, calculator: +labelCalculator
then you can type out "calculator" and select it before typing "gnwbt"
Try this snippet to create a Msgbox like GUI with up to 5 button options. Each button can perform an action

After pasting the code in your Snippets JSON file, type "splashmg" to bring it up

Code: Select all

"splash msg gui": {
  "prefix": "splash msg gui",
  "body": [
    ";-------------------------------------- BEGIN ----------------- SPLASH MSG ---------------------------------------------------",
"Gui, splashmsg: New",
"Gui, splashmsg: +Labelsplashmsg -border",
"Gui, splashmsg:Font,% \"s14 c\" ${1|Orange:=\"fb8500\",Dark_Cornflower_Blue:=\"023e8a\",Linen:=\"ece4db\",Burnt_Sienna:=\"e76f51\"|}, Tahoma",
"Gui, splashmsg:color,% ${2|Antique_Brass:=\"cb997e\",Desert_Sand:=\"ddbea9\",Champagne_Pink:=\"ffe8d6\",Ash_Gray:=\"b7b7a4\",Artichoke:=\"a5a58d\",Ebony:=\"6b705c\"|}",
"Gui, splashmsg: Add, ${3|Text,Edit,ComboBox|}, % \"vtext1 xm ym w400 r$4\", % \"$5\"",
"Gui, splashmsg: Add, Button, % \"vsplashmsgbtn1 gsplashmsggoBTN xm y+10 ${8|w100,w150,w200,w250,w300,w350,w400,w450,w500|}\", % \"$6\"",
"Gui, splashmsg: Add, Button, % \"vsplashmsgbtn2 gsplashmsggoBTN x+10 ${8|w100,w150,w200,w250,w300,w350,w400,w450,w500|}\", % \"$7\"",
"; Gui, splashmsg: Add, Button, % \"vsplashmsgbtn3 gsplashmsggoBTN x+10 ${8|w100,w150,w200,w250,w300,w350,w400,w450,w500|}\", % \"Custom name\"",
"; Gui, splashmsg: Add, Button, % \"vsplashmsgbtn4 gsplashmsggoBTN x+10 ${8|w100,w150,w200,w250,w300,w350,w400,w450,w500|}\", % \"Custom name\"",
"; Gui, splashmsg: Add, Button, % \"vsplashmsgbtn5 gsplashmsggoBTN x+10 ${8|w100,w150,w200,w250,w300,w350,w400,w450,w500|}\", % \"Custom name\"",
"Gui, splashmsg: Show, Autosize",
"",
"splashmsg_guiwait:=TRUE",
"While (splashmsg_guiwait=TRUE) {",
"	sleep, 10",
"}",
"Gui, splashmsg: Destroy",
"",
";----------------------BUTTON ACTIONS--------------- SPLASH MSG ---------------------------------------------------",
"Switch splashmsggoBTN {",
"	Case \"splashmsgbtn1\":",
"		; do action",
"",
"	Case \"splashmsgbtn2\":",
"		; do action",
"",
"	Case \"splashmsgbtn3\":",
"		; do action",
"",
"	Case \"splashmsgbtn4\":",
"		; do action",
"",
"	Case \"splashmsgbtn5\":",
"		; do action",
"",
"}",
"; Empty all btn variables",
"",
"splashmsgbtn1:=\"\", splashmsgbtn2:=\"\", splashmsgbtn3:=\"\", splashmsgbtn4:=\"\", splashmsgbtn5:=\"\", "
"Return",
"",
";SplashmsgClose:",
";Gui, splashmsg:Destroy",
";Return",
"",
"splashmsggoBTN() {",
"global splashmsg_guiwait, SplashMsgGoBtn",
"SplashMsgGoBtn:=A_GuiControl",
"splashmsg_guiwait:=False",
"Gui, splashmsg:Destroy",
"}",
";-------------------------------------- END ----------------- SPLASH MSG ---------------------------------------------------"
  ],
  "description": "splash msg gui with WAIT"
},
"pixel color wait": {
  "prefix": "pixel color wait",
  "body": [
    "WinActivate, ahk_exe ${1:appname}.exe",
"CoordMode, pixel",
"CoordMode, Mouse",
"color:=\"\"",
"While !(instr(color,\"${2:9B9B9B}\")) {",
"	sleep, 500",
"	PixelGetColor, color, ${3:xcoord} , ${4:ycoord}",
"	IF (A_Index = ${5|20,30,40,50,60,80,100|}) {",
"		Msgbox,% Obj2String(\"Color not found`nLast Color:\" color) \"`n`n`nCode line #:\" A_LineNumber",
"		Return",
"	}",
"}",
"msgbox, Color found, press ok to click",
"Mouseclick,Left, ${3:xcoord}, ${4:ycoord},1,0",
"CoordMode,pixel,Window",
"CoordMode,Mouse,Window"
  ],
  "description": "Wait for pixel color to appear"
},

Post Reply

Return to “Visual Studio Code”