April 23, 2021

Opening AutoHotkey Help from any editor

I have this week found out a better way to open the AutoHotkey help based on current selection/ context. (thanks to RaptorX and Joe the-Automator.) This is based on AHK Help Launcher.

Usage

Select a word you want to look for the AHK help for e.g. double-click or leave the cursor on it and run the function.

It will open the AutoHotkey.com/docs page (online version not local .chm which is less detailed) in a stripped browser window (without the toolbar etc) and with the keyword input in the search bar and the search is run.

For VS Code you have to select the word (double click) since a normal Ctrl+C always copy the full line even if nothing is selected.

Implementation

I have integrated it in the NWS PowerTool and map the function to Alt+H only if the Window Title contains .ahk (e.g. for all Editors)

I have split it a bit in two functions
Lib/AHK.ahk -> AHK_Help()

AHK_Help(kw){
; Based on AHK Help Launcher. Credit RaptorX https://www.the-automator.com/autohotkey-webinar-notepad-a-solid-well-loved-but-dated-autohotkey-editor/
pwb := WinExist("ahk_id " pwbHandle) ? pwb : ComObjCreate("InternetExplorer.Application"), pwbHandle := pwb.hwnd
pwb.navigate("https://www.autohotkey.com/docs/")
pwb.addressbar:=false
pwb.ToolBar:=false
pwb.Statusbar:=false
pwb.visible := true

WinActivate, % "Ahk_id " pwb.hwnd

while (pwb.busy || pwb.ReadyState != 4)             ;Wait for page to load
    sleep 50

while !(pwb.document.querySelectorAll("#left")[0])  ; make sure the element exists before performing any more actions
    Sleep, 50

pwb.document.querySelectorAll("#left > div.search > div.input > input[type=search]")[0].value := kw
pwb.document.querySelectorAll("button[aria-label='Search tab']")[0].click()

ControlSend, Internet Explorer_Server1, {enter 2}, % "Ahk_id " pwb.hwnd
}

and
Lib/Clip.ahk -> Clip_GrabWord()

Clip_GrabWord(){
; Based on AHK Help Launcher. Credit RaptorX https://www.the-automator.com/autohotkey-webinar-notepad-a-solid-well-loved-but-dated-autohotkey-editor/
oldClip := clipboardAll
;If WinActive("ahk_exe Code.exe") or WinActive("ahk_exe Code - Insiders.exe") ; needs to select word with double click else copy whole line
    
clipboard =
sendinput ^c
Clip_Wait()
if (clipboard == "") ; no word selected -> use caret cursor location
{
    ; get char to the left of the cursor
    SendInput +{Left}^c{Right}
    clipwait 0
    LeftChar := regexmatch(clipboard, "\w")

    clipboard = 
    SendInput % (leftChar ? "^{left}" : "") "^+{right}^c"
    clipwait 0
    regexmatch(clipboard, "\w+", grab)
    return grab
    
    ; Ctrl+Right and then Ctrl+Shift+Left to select word
    SendInput ^{Right}
    SendInput ^+{Left}
    SendInput ^c
    clipwait 0
    grab := clipboard

}
else
    grab := trim(clipboard)

clipboard := oldClip ; restore clipboard
return grab
}

The hotkey assignment in NWS PowerTool (NWS.ahk) looks like this:

#If WinActive(".ahk")
!h:: ; Alt+h AHK Open Help command
kw := Clip_GrabWord()
AHK_Help(kw)
Return

References

AHK Help Launcher

Context Sensitive Help (Script Showcase) | AHK Forum

4 comments: