#SingleInstance Force
#NoTrayIcon                     ;If you want the tray icon to be visible; comment this line by adding a semicolon ; in front of the #. Example: ;#NoTrayIcon

; ALTERNATIVE METHOD: Try the built-in Everything option "hotkey_explorer_path_search"
; ^^^INFO: https://www.voidtools.com/forum/viewtopic.php?p=17390#p17390

EverythingPath := "C:\Program Files\Everything\Everything.exe"    ;Set this to your everything.exe path. Keep the quotes.

;---Optional setup for special folders---
MyRecycleBin := "Recycle Bin"   ;If your OS is not English, go to your explorer's "Recycle Bin" (🚮) folder and change this to the title of that window. Keep the quotes.
MyThisPC := "This PC"           ;If your OS is not English, go to your explorer's "This PC" (💻) folder and change this to the title of that window. Keep the quotes.
MyHome := "Home"                ;(Windows 11) If your OS is not English, go to your explorer's "Home" (🏠) folder and change this to the title of that window. Keep the quotes.

MyFullName := ""
try {
    ;Get full user name (for file picking dialog)
    MyFullName := GetUserFullName()
} catch e {
    MyFullName := ""
}
EnvGet, UserProfile, UserProfile

;Default hotkey is ^F (Ctrl+F). Replace each ^F with F3 if you want to use that hotkey instead.
;You may delete each block below if you don't need its features.

;BLOCK 1 - DESKTOPS
#If, WinActive("ahk_class WorkerW") or WinActive("ahk_class Progman")
^F::
    RunPath = -p "%UserProfile%\Desktop\"
    Run, %EverythingPath% %RunPath%
    WinWait, ahk_exe everything.exe,, 2
    if (!ErrorLevel) {
        Sleep, 125
        WinActivate, ahk_exe everything.exe
    }
Return


;BLOCK 2 - TASKBARS
#If, WinActive("ahk_class Shell_TrayWnd") or WinActive("ahk_class Shell_SecondaryTrayWnd")
^F::
    Run, %EverythingPath%
    WinWait, ahk_exe everything.exe,, 2
    if (!ErrorLevel) {
        Sleep, 125
        WinActivate, ahk_exe everything.exe
    }
Return


;BLOCK 3 - EXPLORER WINDOWS and FILE PICKING DIALOGS (only for the full file picking dialog)
#If, WinActive("ahk_class CabinetWClass") or WinActive("ahk_class #32770")
^F::
    ;Try using the GetExplorerPath method (doesn't work on the file picker dialog)
    ;Doesn't work with Recycle Bin, This PC, Home... (probably more)
    RunPath := GetActiveExplorerPath()
    if (InStr(RunPath, "::{")) {
        ;If the function returns a GUID path, retry using the manual method below.
        RunPath := ""
    } else if (RunPath != "") {
        ;Finalize commandline
        RunPath = -p "%RunPath%"
    }

    if (RunPath == "") {
        ;Function method failed OR this is a file picking dialog

        ;To find the correct control path use an inspector application (UIAViewer, System Informer, Winspector etc..)
        ;Find Breadcrumb Parent1 -> ToolbarWindow321
        ControlGet, crumbHWND, Hwnd,, Breadcrumb Parent1, A
        ControlGetText, RunPath, ToolbarWindow321, ahk_id %crumbHWND%
        ;Cleanup the string (Location: C:\Dir1\Dir2)
        ;Windows paths can't include the ":" character so we don't need to combine result[2]...result[last]
        try {
            RunPath := StrSplit(RunPath, ": ")[2]
        }
        if (RunPath == "") {
            ;If we are still unable to find breadcrumb text, try again using the window title. This can help us detect some special named folders.
            WinGetTitle, windowTitle, A
            RunPath := windowTitle
        }
        if (FileExist(RunPath)) { ;Check if its a regular path
            ;Finalize commandline
            RunPath = -p "%RunPath%"
        } else if (FileExist(UserProfile . "\" . RunPath)) { ;Check if its a user folder
            RunPath = -p "%UserProfile%\%RunPath%\"

        ;Check special named paths
        } else if (RegExMatch(RunPath, "^" . MyThisPC) || RegExMatch(RunPath, "^" . MyHome)) { ;This PC or Home 
            RunPath := ""
        } else if (RegExMatch(RunPath, "^" . MyRecycleBin)) { ;Recycle Bin
            RunPath = -s "\$RECYCLE.BIN "
        } else if (MyFullName && RegExMatch(RunPath, "^" . MyFullName)) { ;UserProfile folder (using MyFullName)
            RunPath = -p "%UserProfile%"
        } else {
            ;Abort
            RunPath = ""
        }

    }

    ;Uncomment below for debugging
    ; MsgBox, %EverythingPath% %RunPath%
    Run, %EverythingPath% %RunPath%
    WinWait, ahk_exe everything.exe,, 2
    if (!ErrorLevel) {
        Sleep, 125
        WinActivate, ahk_exe everything.exe
    }
Return

#If

;FUNCTIONS
GetUserFullName() {
    username := A_UserName
    ; Connect to the WMI service
    objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!" . "\\.\root\cimv2")
    ; Query the Win32_UserAccount class for the current username
    colItems := objWMIService.ExecQuery("SELECT * FROM Win32_UserAccount WHERE Name='" . username . "'")
    ; Loop through the results to get the FullName
    for objItem in colItems {
        return objItem.FullName
    }
}

;GetActiveExplorerPath by ntepa https://www.autohotkey.com/boards/viewtopic.php?p=507423#p507423
;Works correctly in all situations except for special ::{GUID} folders (Recycle Bin, Home, Gallery)
GetActiveExplorerPath() {
    hwnd := WinActive("ahk_class CabinetWClass")
    activeTab := 0
    try ControlGet, activeTab, Hwnd,, % "ShellTabWindowClass1", % "ahk_id" hwnd
    for w in ComObjCreate("Shell.Application").Windows {
        if (w.hwnd != hwnd)
            continue
        if activeTab {
            static IID_IShellBrowser := "{000214E2-0000-0000-C000-000000000046}"
            shellBrowser := ComObjQuery(w, IID_IShellBrowser, IID_IShellBrowser)
            DllCall(NumGet(numGet(shellBrowser+0)+3*A_PtrSize), "Ptr", shellBrowser, "UInt*", thisTab)
            if (thisTab != activeTab)
                continue
            ObjRelease(shellBrowser)
        }
        return w.Document.Folder.Self.Path
    }
}