;--------------------------------------------------------------------------- ; ; A simple auto-search utility for the WinExplorer ; created by Buddy 03/2009 ; disclaimer: this version has undergone non-Buddy edits ; ;--------------------------------------------------------------------------- #Include COM.ahk #SingleInstance SetBatchLines,-1 Menu, tray, NoStandard Menu, tray, Add, Reload, Reload Menu, tray, Add, Exit, Exit Menu, Tray, Icon, shell32.dll, 23 ;--------------------------------------------------------------------------- ; Define SPACE as Windows Explorer auto-search hotkey ;--------------------------------------------------------------------------- GroupAdd, Explorer, ahk_class CabinetWClass GroupAdd, Explorer, ahk_class ExploreWClass ;GroupAdd, Explorer, ahk_class #32770 ;for future? browse 'Open File' windows, etc. #IfWinActive, ahk_group Explorer Space:: searchInExplorer() Return #IfWinActive ;--------------------------------------------------------------------------- ; Shift-ESC terminates the script ;--------------------------------------------------------------------------- #IfWinActive, myGui ~+Esc::ExitApp #IfWinActive Exit: ExitApp Return Reload: Reload Return ;--------------------------------------------------------------------------- ; Search function ;--------------------------------------------------------------------------- searchInExplorer() { ;--------------------------------------------------------------------------- ; Control variables must be global ;--------------------------------------------------------------------------- global Input, List ;--------------------------------------------------------------------------- ; Store current directory name [to clipboard - use global Dir instead] ;--------------------------------------------------------------------------- ControlGetText, Dir, Edit1, A ;--------------------------------------------------------------------------- ; Define and show minimal GUI with controls ;--------------------------------------------------------------------------- Gui, 3:Font, S10 CDefault Bold, Letter Gothic Gui, 3:Margin,0,0 Gui, 3:+Owner -Caption AlwaysOnTop Gui, 3:Add, Edit, x0 y0 h25 w600 vInput gInputEvent hwndhInput Gui, 3:Add, ListView, x0 y+0 w600 vList gListEvent hwndhList Hide -Hdr AltSubmit,%A_Space% Gui, 3:Show, Center, myGui WinGet, hGui, ID, myGui WaitForKey: ;--------------------------------------------------------------------------- ; Retrieve the control, which currently has the focus ; (according ControlGetFocus page in AHK manual) ;--------------------------------------------------------------------------- VarSetCapacity(hInputDec, 65, 0) DllCall("msvcrt_i64toa", Int64, hInput, Str, hInputDec, Int, 10) GuiThreadInfoSize = 48 VarSetCapacity(GuiThreadInfo, GuiThreadInfoSize) NumPut(GuiThreadInfoSize, GuiThreadInfo, 0) If DllCall("GetGUIThreadInfo", uint, 0, str, GuiThreadInfo) hFocused := NumGet(GuiThreadInfo, 12) Else hFocused = 0 ;--------------------------------------------------------------------------- ; Wait until user pressed ENTER, ESC or DOWN key ;--------------------------------------------------------------------------- Input, key,V, {Enter}{Esc}{Down} ;--------------------------------------------------------------------------- ; For DOWN the cursor jumps down into the search list ;--------------------------------------------------------------------------- If (ErrorLevel = "EndKey:Down") { If (hFocused = hInputDec) SendInput {Tab}{Down} ;--------------------------------------------------------------------------- ; Go wait for the next key if DOWN is pressed ;--------------------------------------------------------------------------- Goto,WaitForKey } ;--------------------------------------------------------------------------- ; ENTER & ESC closes the GUI ;--------------------------------------------------------------------------- Gui, 3:Destroy ;--------------------------------------------------------------------------- ; ENTER also shows the selected file in another explorer window ;--------------------------------------------------------------------------- If (ErrorLevel = "EndKey:Enter") AND SelectedFile { If StrLen(Dir) = 3 ; Extra "" from C:, D:,... StringTrimRight,Dir,Dir,1 If InStr( FileExist( Dir . "" . SelectedFile ), "D" ) ShellNavSelect3( Dir . "" . SelectedFile ) Else ShellNavSelect3( Dir, False, 0, SelectedFile ) } Return ;--------------------------------------------------------------------------- ; Routine executed for each keystroke in the input field ;--------------------------------------------------------------------------- InputEvent: ;--------------------------------------------------------------------------- ; Retrieve actual user input ;--------------------------------------------------------------------------- GuiControlGet, Input ;--------------------------------------------------------------------------- ; Show list control ;--------------------------------------------------------------------------- GuiControl, Show, List ;--------------------------------------------------------------------------- ; Search files based on specified prefix and list them ;--------------------------------------------------------------------------- LV_Delete() Loop, %Dir%%Input%*, 1 LV_Add("", A_LoopFileName) ;--------------------------------------------------------------------------- ; Resize list control height according to the number of found files ;--------------------------------------------------------------------------- LV_ModifyCol(1,"AutoHdr") h1 := LV_GetCount() * 25 h2 := h1 + 25 GuiControl, Move, List, h%h1% Gui, 3:Show, Center h%h2% Return ;--------------------------------------------------------------------------- ; Routine executed for each keystroke in the search list ;--------------------------------------------------------------------------- ListEvent: ;--------------------------------------------------------------------------- ; Retrieve file name for selected row ;--------------------------------------------------------------------------- LV_GetText(SelectedFile, LV_GetNext(0)) Return } /* Modified to accept file path and select files Added sMode (selection mode): 0 Deselect the item. 1 Select the item. 3 Put the item in edit mode. 4 Deselect all (--but the specified item). 8 Ensure the item is displayed in the view. 16 Give the item the focus. */ ;based on ShellNavigateShellFolder by Sean ShellNavSelect3(sPath, bExplore=False, hWnd=0, sSelect="") { COM_Init() psh:= COM_CreateObject("Shell.Application") hWnd:=WinExist("ahk_group Explorer") psw := COM_Invoke(psh, "Windows") Loop, % COM_Invoke(psw, "Count") If COM_Invoke(pwb:=COM_Invoke(psw, "Item", A_Index-1), "hWnd") hWnd COM_Release(pwb) Else Break If sSelect { pfv := COM_Invoke(pwb,"Document") COM_Invoke(pfv,"SelectItem","+" pfi:=COM_Invoke(pfv,"Folder.ParseName",sSelect),16) ;focus COM_Invoke(pfv,"SelectItem","+" pfi,8) ;scroll to file COM_Invoke(pfv,"SelectItem","+" pfi,4) ;deselect others COM_Invoke(pfv,"SelectItem","+" pfi,1) ;select file COM_Release(pfi) COM_Release(pfv) } Else { COM_Invoke(pwb, "Navigate2", sPath) COM_Release(pwb) } COM_Release(psw) COM_Release(psh) COM_Term() }