Search code examples
searchfull-text-searchautohotkey

autoHotkey search across all windows


I am trying to create a script that searches a highlighted in Word, Pdf, Excel and Chrome tab if they are open at the time that I am running the script.

The problem is that I am trying to do all this with the press of a single key. The script looks like this, but it does not work properly:

Send, ^c
WinGetClass, class, A
MsgBox, The active window's class is "%class%"
;SetKeyDelay, 0
if WinExist("ahk_class OpusApp")
{
    WinActivate
    Send, ^f
    gosub getplain
    SetKeyDelay, 0
    Send {Raw}%clipboardt%
    ;clipboardt := ""
    ;return
}
;SetKeyDelay, 0

if WinExist("ahk_class AcrobatSDIWindow")
{
    WinActivate
    Send, ^f
    gosub getplain
    SetKeyDelay, 0
    Send {Raw}%clipboardt%
    ;clipboardt =
    ;return
}
;SetKeyDelay, 0

if WinExist("ahk_class XLMAIN")
{
    WinActivate
    Send, ^f
    gosub getplain
    SetKeyDelay, 0
    Send {Raw}%clipboardt%
    ;clipboardt =
    ;return
}
;SetKeyDelay, 0
if WinExist("ahk_class Chrome_WidgetWin_1")
{
    WinActivate
    Send, ^f
    gosub getplain
    SetKeyDelay, 0
    Send {Raw}%clipboardt%
    ;clipboardt =
}
;SetKeyDelay, 0
if WinExist("ahk_exe firefox.exe")
{
    WinActivate
    Send, ^f
    gosub getplain
    SetKeyDelay, 0
    Send {Raw}%clipboardt%
    ;clipboardt =
}


getplain:
StringReplace, clipboardt, clipboard, `r`n, %A_Space%, All
clipboardt := RegExReplace(clipboardt, "` {2,}", "` ")
StringLeft, 1st, clipboardt, 1
IfInString, 1st, %A_Space%
StringTrimLeft, clipboardt, clipboardt, 1
StringRIght, last, clipboardt, 1
IfInString, last, %A_Space%
StringTrimRight, clipboardt, clipboardt, 1



return

}

Do you have any advice? Is it too much script for one key?

I do not know what does not work :/


Solution

  • Try this:

    ; Create an array of those windows:
    my_windows := ["OpusApp", "AcrobatSDIWindow", "XLMAIN", "Chrome_WidgetWin_1"," MozillaWindowClass"]
    
    #If WinExist("ahk_class OpusApp") || WinExist("ahk_class AcrobatSDIWindow") || WinExist("ahk_class XLMAIN") || WinExist("ahk_class Chrome_WidgetWin_1") || WinExist("ahk_class MozillaWindowClass") ; || mens OR
    
        F1::
            clipboard := ""  ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
            Send, ^c         ; copy the selected text
            ClipWait 1       ; wait max. 1 sec for the clipboard to contain data
            If (ErrorLevel)
            {
                MsgBox, No text copied
                return
            }
            ; otherwise:            
            clipboard := Trim(clipboard, ", `t`r`n") ; remove leading and trailing commas, spaces, tabs, CRs and LFs 
            For each, window in my_windows           ; retrieve each window from the array, one at a time
            {
                If WinExist("ahk_class" window)
                {
                    WinActivate
                    WinWaitActive
                    Send, ^f
                    Sleep, 200
                    Send, ^v
                    Sleep, 200
                    Send, {Enter}
                }
            }
        return
    
    #If