Roman Malieiev
Roman Malieiev

Reputation: 305

Autohotkey window appear event

I'm using WorkRave rest reminder and want to turn off my screen when the rest window appears. I know how to turn it off.

How create an event when specified window (#IfWinActive ahk_class ...) appears?

Also, can i bind % symbol? {%} doesn't work, instead of other ones.

Upvotes: 10

Views: 8070

Answers (3)

Mister Robato
Mister Robato

Reputation: 176

  1. Open WorkRave
  2. Right Click > Preferences > User Interface > Block mode: No blocking > Close
  3. Save code below as WorkRave Close Rest Reminder Window.ahk
    #NoEnv
    SetWorkingDir %A_ScriptDir%
    #Warn
    CoordMode, Mouse, Window
    SendMode Input
    #SingleInstance Force
    SetTitleMatchMode 2
    SetTitleMatchMode Fast
    DetectHiddenWindows Off
    DetectHiddenText On
    #WinActivateForce
    #NoTrayIcon
    SetControlDelay 1
    SetWinDelay 0
    SetKeyDelay -1
    SetMouseDelay -1
    SetBatchLines -1
    #Persistent
    #MaxThreadsPerHotkey 2

    WorkRaveCloseRestReminderWindow:
    Loop
    {
        WinWait, Rest break ahk_class gdkWindowToplevel ahk_exe Workrave.exe
        WinClose, Rest break ahk_class gdkWindowToplevel ahk_exe Workrave.exe
        Run, TurnOffScreen
    }
    Return

Upvotes: 0

infogulch
infogulch

Reputation: 1291

To have an instant notification of a window appearing, use a Shell Hook. This is sometimes so fast that autohotkey can react before you even see the window yourself.

A shell hook is demonstrated on the AutoHotkey Forum.

An example with your usage (almost copied verbatim from the forum post):

#Persistent
SetBatchLines, -1
Process, Priority,, High

Gui +LastFound
hWnd := WinExist()

DllCall( "RegisterShellHookWindow", UInt,hWnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return

ShellMessage( wParam,lParam )
{
    If ( wParam = 1 ) ;  HSHELL_WINDOWCREATED := 1
    {
        WinGetTitle, Title, ahk_id %lParam%
        If  ( Title = "WorkRest" )
            WinClose, ahk_id %lParam% ; close it immideately
    }
}

If you want to use a literal % symbol in a command, escape it with AutoHotkey's escape character, the backtick ` (on the same key as ~ on a US keyboard) like so:

MsgBox You are 200`% awesome!

Upvotes: 15

Robert Ilbrink
Robert Ilbrink

Reputation: 7953

Romale,

You can try this, but since I don't use WorkRave, I can't test it.

; This next line needs to be added at the top of the AHK file, so it will be started as soon as AHK starts.
; Every 120000 ms, it will launch the "WorkRave:" script to check if a window with WorkRave exists.
SetTimer, WorkRave,120000 ; Run WorkRaveTester every 2 minutes = 120000


; Somewhere else in the AHK file.....
WorkRave: ; This is the label for the WorkRave script
SetTitleMatchMode, 2 ; 2 = Matches the string WorkRave anywhere in the window title of IfWinExist
IfWinExist, WorkRave ; When WorkRave window exists
{
  TrayTip, WorkRave, Started ,1 ; Or whatever you want to do here....
}
Return

Upvotes: 0

Related Questions