Search code examples
autohotkey

AHK: closing a window whenever it pops up


I wrote an AHK script designed to copy a text from Adobe Acrobat when I press F9. It then changes it according to a regex and shows the copied text in tooltip. Additionally, I added code to automatically close an annoying window that Acrobat sometimes shows when coping a text, an infamous There was an error while copying to Clipboard. An internal error occurred. When this window doesn't show up, the script keeps showing a tooltip, which is designed to close after a specified amount of time. I've been banging my head against the wall but I don't know how to correct this.

;#NoTrayIcon
#Persistent
#SingleInstance
F9::
#If WinActive("ahk_exe Acrobat.exe")
{
Clipboard:=""
send,^c
ClipWait, 1
Clipboard := RegExReplace(Clipboard, "\r\n", " ")
SetTimer,CheckForMsgBox,100
CheckForMsgBox:
    IfWinExist, Adobe Acrobat
    {
        Send {Enter}
        SetTimer,CheckForMsgBox,Off
    }
;Return
If (StrLen(Clipboard) < 120)
ToolTip % Clipboard
Else
ToolTip Copied
SetTimer, ToolTipOff, -1000
return
}
#If

ToolTipOff:
ToolTip
return

Solution

  • ;#NoTrayIcon
    ; #Persistent ; (1)
    #SingleInstance
    SetTimer,CheckForMsgBox,100 ; (2)
    return
    
    #If WinActive("ahk_exe Acrobat.exe") ; (3)
    
    F9::    
    clipboard:=""
    send,^c
    ClipWait, 1
    Clipboard := RegExReplace(Clipboard, "\r\n", " ")
    If (StrLen(Clipboard) < 120)
        ToolTip %Clipboard%
    Else
        ToolTip Copied
    SetTimer, ToolTipOff, -1000
    return
    
    #If  ; turn off context sensitivity
    
    ToolTipOff:
    ToolTip
    return
    
    CheckForMsgBox:
    ; ControlSend, Control, Keys, WinTitle, WinText, ExcludeTitle, ExcludeText
    ControlSend, , {Enter}, Adobe Acrobat  ; Close this unwanted window whenever it appears
    return
    

    (1) Scripts containing hotkeys, hotstrings, or any use of OnMessage() or Gui are automatically persistent.

    (2) SetTimer causes a subroutine (Label) to be launched automatically and repeatedly at a specified time interval.

    https://autohotkey.com/docs/commands/SetTimer.htm

    (3) Like the #IfWin directives, #If is positional: it affects all hotkeys and hotstrings physically beneath it in the script.

    https://autohotkey.com/docs/commands/_If.htm