Debugging AutoHotKey scripts for SciTE users

Helpful script writing tricks and HowTo's
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Debugging AutoHotKey scripts for SciTE users

02 Jan 2014, 21:21

I read a few posts on the forum about SciTE's built in debugging features however it wasn't straight-forward to me so I made a short video that helps show how to use it. Fincs has an awesome customized version for AutoHotKey which you can get here: http://fincs.ahk4.net/scite4ahk/

You can watch the ~6 minute video here and my script (which was built merely to show some of the tools) is below.



I should have prefaced my post with the warning that I am NOT a programmer. I might use incorrect terms here and there and I'm sure there are more elegant ways to demonstrate this but it should suffice for the basics.

Code: Select all

#SingleInstance, Force
#NoEnv
Var:=0

OutputDebug Line  %A_LineNumber%: A_Index is: %A_Index% and Var is: %Var%
GoSub Looper
MsgBox end of program
return

;***********************First loop********************************.
Looper:
loop, 15
{
var:=Var+A_Index
OutputDebug Line %A_LineNumber%: A_Index is: %A_Index% and Var is: %Var%
var:=Var+10
GoSub SubLooper
}
return

;***********************Sub loop********************************.
SubLooper:
Loop, 3
{
SubLooperVar:=A_Index   
OutputDebug Line %A_LineNumber%: A_Index is: %A_Index% and Var is: %Var% and Sublooper index is: %SubLooperVar%
}
return

Last edited by Joe Glines on 18 Jul 2016, 07:11, edited 1 time in total.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
vasili111
Posts: 747
Joined: 21 Jan 2014, 02:04
Location: Georgia

Re: Debugging AutoHotKey scripts for SciTE users

29 Jan 2014, 04:43

Very nice video. Thank you!
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Re: Debugging AutoHotKey scripts for SciTE users

31 Jan 2014, 09:29

Thanks for doing the video, the variable list means no more needing to use ListVars use for me!!!
User avatar
fincs
Posts: 527
Joined: 30 Sep 2013, 14:17
Location: Seville, Spain
Contact:

Re: Debugging AutoHotKey scripts for SciTE users

14 Feb 2014, 15:25

Really nice tutorial :)
fincs
Windows 11 Pro (Version 22H2) | AMD Ryzen 7 3700X with 32 GB of RAM | AutoHotkey v2.0.0 + v1.1.36.02
Get SciTE4AutoHotkey v3.1.0 - [My project list]
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Debugging AutoHotKey scripts for SciTE users

16 Dec 2015, 22:07

I learned a few more things about using SciTE to debug and thought I'd document it in the below video. I also give short review of using the dmp function for displaying what text is in an object/array. (Newbies that are not used to playing with objects or arrays will find this very helpful)


I also realized that the SciTE output pane can be used as a command prompt. I'm sure there are more uses for it, but I didn't realize I could easily submit commands from it. :)
Last edited by Joe Glines on 18 Jul 2016, 07:38, edited 1 time in total.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: Debugging AutoHotKey scripts for SciTE users

17 Dec 2015, 14:58

Joe, there is an even better (imho) way of doing this.
Download DebugView from the microsoft site.
This is an app that traps debug messages from other apps - anything you output with OutputDebug will appear in debugview.
If you want to clear the debug view window, do OutputDebug DBGVIEWCLEAR (case for the debug message is important).
Then in scite, do Options->Open User properties, and add ahk.debugger.capture.streams=0 at the end to turn off the stream viewer (it gets annoying once you have debugview).
With debugview, you may see messages from other apps, which might get annoying . so in this case you can write a logging func to prefix a string to each message:

Code: Select all

Log(str){
   OutputDebug % "MyAppName: " str
}
Then in debugview, add a filter: MyFunc: * and you only see debug info from your app.
Usually it isnt an issue tho, you dont have to bother.
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Debugging AutoHotKey scripts for SciTE users

17 Dec 2015, 19:41

Sounds both cool and a bit complex. I like my current method as it is simple and easy but I'm looking forward to trying your suggestion (especially when I have complicated debug issues) Thanks evilC for taking the time to write it out!
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: Debugging AutoHotKey scripts for SciTE users

18 Dec 2015, 03:57

It's not complex, at it's simplest level, just run debugview and close the stream viewer. I find the stream viewer really annoying as it pops up in front of your code and you invariably have to move it around.
With debugview, you just arrange your scite window and debugview window how you want and then forget about it. Personally, I normally put use Gui, Show, x0 y0 in my gui scripts when writing them, so the gui always appears top left, with the scite window to the right of that and the debugview window at the bottom.
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Debugging AutoHotKey scripts for SciTE users

18 Dec 2015, 07:13

@evilC- I made the changes and tested it. Yes, I definitely prefer the DebugView window over the StreamViewer. I too would get annoyed with where StremViewer would end up. Having this on it's own (besides being able to track alll debug commands) is pretty awesome! Thanks for sharing!
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: Debugging AutoHotKey scripts for SciTE users

21 Dec 2015, 09:03

I am using Lexikos ListGlobalVars() with Outputdebug (if i don't have too much vars in a script).
https://autohotkey.com/board/topic/2092 ... ntry156570
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Debugging AutoHotKey scripts for SciTE users

21 Dec 2015, 10:00

@haichen , this sounds interesting however, in my example, I keep having to step through Lexicos' function during debug (which I don't think makes sense). Can you provide a working example how you use it?

Here is what I did

Code: Select all

var3:=2
var:="hi"

Loop {
    tick := A_TickCount
ind:=A_Index
OutputDebug,   % ListGlobalVars()
    Sleep, 2000
}
return

RAlt::
Browser_Forward::Reload

;~ https://autohotkey.com/board/topic/20925-listvars/#entry156570
ListGlobalVars()
{
    static hwndEdit, pSFW, pSW, bkpSFW, bkpSW
    
    if !hwndEdit
    {
        dhw := A_DetectHiddenWindows
        DetectHiddenWindows, On
        Process, Exist
        ControlGet, hwndEdit, Hwnd,, Edit1, ahk_class AutoHotkey ahk_pid %ErrorLevel%
        DetectHiddenWindows, %dhw%
        
        astr := A_IsUnicode ? "astr":"str"
        ptr := A_PtrSize=8 ? "ptr":"uint"
        hmod := DllCall("GetModuleHandle", "str", "user32.dll", ptr)
        pSFW := DllCall("GetProcAddress", ptr, hmod, astr, "SetForegroundWindow", ptr)
        pSW := DllCall("GetProcAddress", ptr, hmod, astr, "ShowWindow", ptr)
        DllCall("VirtualProtect", ptr, pSFW, ptr, 8, "uint", 0x40, "uint*", 0)
        DllCall("VirtualProtect", ptr, pSW, ptr, 8, "uint", 0x40, "uint*", 0)
        bkpSFW := NumGet(pSFW+0, 0, "int64")
        bkpSW := NumGet(pSW+0, 0, "int64")
    }

    if (A_PtrSize=8) {
        NumPut(0x0000C300000001B8, pSFW+0, 0, "int64")  ; return TRUE
        NumPut(0x0000C300000001B8, pSW+0, 0, "int64")   ; return TRUE
    } else {
        NumPut(0x0004C200000001B8, pSFW+0, 0, "int64")  ; return TRUE
        NumPut(0x0008C200000001B8, pSW+0, 0, "int64")   ; return TRUE
    }
    
    ListVars
    
    NumPut(bkpSFW, pSFW+0, 0, "int64")
    NumPut(bkpSW, pSW+0, 0, "int64")
    
    ControlGetText, text,, ahk_id %hwndEdit%

    RegExMatch(text, "sm)(?<=^Global Variables \(alphabetical\)`r`n-{50}`r`n).*", text)
    return text
}
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: Debugging AutoHotKey scripts for SciTE users

21 Dec 2015, 10:07

Joe, when you are debugging, you have a number of options:

1) "Step In" (The Right arrow or F10)
This steps "into" functions etc. If you "step in" on a line that is a call to another func (eg lex's ListGlobalVars), you will debug that func.
Also, if you have SetTimers psuedo-threads running, step in can jump to those as well.

2) "Step Over" (The down arrow or F11)
This steps "over" functions etc. It will run until the next line.

3) "Step Out" (Up and left arrow, or Shift+F11)
Runs until you exit from the current function.
Useful if you accidentally "Step In" - will keep going until that function call ends.

4) Run (Play icon or F5)
Play until next breakpoint.

So TL;DR - Use Step Over (F11) to avoid debugging stuff you do not want to, and use Step Out if you Step In by mistake.
User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: Debugging AutoHotKey scripts for SciTE users

21 Dec 2015, 11:15

Hi Joe, if you put OutputDebug DBGVIEWCLEAR into the loop you can see it a little better. Lexikos function does the same as the listvars command. So maybe a big loop is not the best example, but if you want to have a look at all vars at a special codeline this may be an easy solution. Its like a better Msgbox, % var1 var2 var3 .. (without stopping the script and changing the active window)
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Debugging AutoHotKey scripts for SciTE users

21 Dec 2015, 15:41

@EvilC- thanks for point out the 3rd option. I new it did behaved this way on labels but I didn't realize it would behave this way on functions too.

@haichen- I thought you meant you were using the function in the same line as Outputdebug. Thank you for the clarification.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
tbolto00

Re: Debugging AutoHotKey scripts for SciTE users

26 Jan 2016, 11:53

When debugging this script using F11 (step by step); it activates notepad, but then it makes Scite the active window. Thus, when I debug the next line, it sends a 1 in Scite rather than Notepad. Is there a way to prevent Scite from constantly making itself the active window?

WinActivate, ahk_class Notepad
Send 1
Send 2
Send 3
Send 4
Send 5
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: Debugging AutoHotKey scripts for SciTE users

26 Jan 2016, 14:31

Yeah, that's annoying. FWIW it's no better in C with Visual Studio in my experience, so I don't think it's inherently an AHK / SciTe problem.
However, it may well be fixable in SciTe4AHK as a lot of the code is actually AHK code, so if the part that focuses SciTe is AHK code, and is not fundamentally required, it could be deactivated.
Another solution would be to run an AHK script that redirects F11 to the SciTe window (eg by using ControlSend). Potentially what you could have the script do when you hit F11 would be to store the active window, send F11 to SciTe (Which would grab focus), then re-focus the window you had focused.
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: Debugging AutoHotKey scripts for SciTE users

27 Jan 2016, 01:17

You could just remove the IfWinNotActive/WinActivate lines from SciTE_RedrawLine and SciTE_EnsureFileIsOpen in SciTEDebug.ahk...
pmobin
Posts: 17
Joined: 14 Apr 2016, 12:50

Re: Debugging AutoHotKey scripts for SciTE users

19 Aug 2020, 22:38

Anyone know where I can get a copy of dmp.ahk? The .de site is no longer.
Thanks
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: Debugging AutoHotKey scripts for SciTE users

19 Aug 2020, 22:43

pmobin wrote:
19 Aug 2020, 22:38
Anyone know where I can get a copy of dmp.ahk? The .de site is no longer.
Thanks
There is a link in Joe's post above: https://www.autohotkey.com/boards/viewtopic.php?f=7&t=1254#p62711
Link: https://www.the-automator.com/download/dmp.ahk

Return to “Tutorials (v1)”

Who is online

Users browsing this forum: No registered users and 34 guests