Jump to content


Photo

WinTrigger: Watch [de]activation/[un]existance of windows...


  • Please log in to reply
21 replies to this topic

#1 MasterFocus

MasterFocus
  • Moderators
  • 4132 posts

Posted 15 October 2010 - 01:22 AM

My template got very popular, so it
definitely deservers its own thread.

This template can be used to watch for windows or
programs and act upon their [de]activation or [un]existance.

----------------------------------------------------------------------------------------------
License: GNU AGPL (v3.0) Posted Image
Please check: MasterFocus's Home Topic
This topic's latest update: 04/september/2012
Latest code modification: 15/march/2010
----------------------------------------------------------------------------------------------

TEMPLATE (download)
;========================================================================
; 
; Template:     WinTrigger (former OnOpen/OnClose)
; Description:  Act upon (de)activation/(un)existance of programs/windows
; Online Ref.:  http://www.autohotkey.com/forum/viewtopic.php?t=63673
;
; Last Update:  15/Mar/2010 17:30
;
; Created by:   MasterFocus
;               http://www.autohotkey.net/~MasterFocus/AHK/
;
; Thanks to:    Lexikos, for improving it significantly
;               http://www.autohotkey.com/forum/topic43826.html#267338
;
;========================================================================
;
; This template contains two examples by default. You may remove them.
;
; * HOW TO ADD A PROGRAM to be checked upon (de)activation/(un)existance:
;
; 1. Add a variable named ProgWinTitle# (Configuration Section)
; containing the desired title/ahk_class/ahk_id/ahk_group
;
; 2. Add a variable named WinTrigger# (Configuration Section)
; containing the desired trigger ("Exist" or "Active")
;
; 3. Add labels named LabelTriggerOn# and/or LabelTriggerOff#
; (Custom Labels Section) containing the desired actions
;
; 4. You may also change CheckPeriod value if desired
;
;========================================================================

#Persistent

; ------ ------ CONFIGURATION SECTION ------ ------

; Program Titles
ProgWinTitle1 = ahk_class Notepad
WinTrigger1 = Exist
ProgWinTitle2 = Calculator
WinTrigger2 = Active

; SetTimer Period
CheckPeriod = 200

; ------ END OF CONFIGURATION SECTION ------ ------

SetTimer, LabelCheckTrigger, %CheckPeriod%
Return

; ------ ------ ------

LabelCheckTrigger:
  While ( ProgWinTitle%A_Index% != "" && WinTrigger := WinTrigger%A_Index% )
    if ( !ProgRunning%A_Index% != !Win%WinTrigger%( ProgWinTitle := ProgWinTitle%A_Index% ) )
      GoSubSafe( "LabelTriggerO" ( (ProgRunning%A_Index% := !ProgRunning%A_Index%) ? "n" : "ff" ) A_Index )
Return

; ------ ------ ------

GoSubSafe(mySub)
{
  if IsLabel(mySub)
    GoSub %mySub%
}

; ------ ------ CUSTOM LABEL SECTION ------ ------

LabelTriggerOn1:
LabelTriggerOff1:
LabelTriggerOn2:
  MsgBox % "A_ThisLabel:`t" A_ThisLabel "`nProgWinTitle:`t" ProgWinTitle "`nWinTrigger:`t" WinTrigger
Return

; ------ END OF CUSTOM LABEL SECTION ------ ------
Related scripts:
WinWait framework by berban - <!-- l --><a class="postlink-local" href="http://www.autohotkey.com/community/viewtopic.php?f=2&t=92017">viewtopic.php?f=2&t=92017</a><!-- l -->

#2 Chicken Pie 4 Tea

Chicken Pie 4 Tea
  • Members
  • 377 posts

Posted 15 October 2010 - 07:52 AM

Thanks, yes it does deserve its own thread as I had not noticed it before.

#3 dave_100

dave_100
  • Members
  • 94 posts

Posted 18 October 2010 - 08:37 PM

an amazing script to auto run scripts when certain events occur, thank you!

#4 RossGoodman

RossGoodman
  • Members
  • 10 posts

Posted 17 October 2012 - 12:56 PM

OK, I hope I'm doing something completely stupid but I can't get this to work.
I'm running Windows7 (64bit).
Other AHK scripts work fine.
I copied the code as is from the script window above.

I run the script and nothing happens when I create or activate a Notepad or Calculator window.

The only change I made was to the custom label section:
; ------ ------ CUSTOM LABEL SECTION ------ ------

LabelTriggerOn1:
	MSgBox "One On"
LabelTriggerOff1:
	MSgBox "One Off"
LabelTriggerOn2:
  MsgBox % "A_ThisLabel:`t" A_ThisLabel "`nProgWinTitle:`t" ProgWinTitle "`nWinTrigger:`t" WinTrigger
Return
to make sure that Notepad did "something".

Thoughts?

#5 MasterFocus

MasterFocus
  • Moderators
  • 4132 posts

Posted 17 October 2012 - 06:43 PM

Your adaption would probably give unwanted results because you lack a Return for each separate label.
There was no need to change anything: all 3 labels would execute the same detailed MsgBox, but with a different message each.
I've just copied my exact code and run it. Works as expected here (AHK Basic + Win7 x64).
I don't see any reason for it not to show the MsgBox properly.
The Calculator example would not work if the window title is different (another language, perhaps). However, the Notepad test should work just fine.
Try using SetTitleMatchMode, 2 on the top of the script and don't use ahk_class. Let me know if it works.

#6 RossGoodman

RossGoodman
  • Members
  • 10 posts

Posted 17 October 2012 - 10:42 PM

OK, looks like it was a version problem.
I was running 1.0.47.06
I downloaded and installed the latest version and the script works perfectly now.
Thanks for your patience!

Thanks
Ross

#7 RossGoodman

RossGoodman
  • Members
  • 10 posts

Posted 18 October 2012 - 09:26 AM

OK, it's soooo close to doing what I want:
; Note: From now on whenever you run AutoHotkey directly, this script
; will be loaded.  So feel free to customize it to suit your needs.

; Please read the QUICK-START TUTORIAL near the top of the help file.
; It explains how to perform common automation tasks such as sending
; keystrokes and mouse clicks.  It also explains more about hotkeys.


;========================================================================
;
; Template:     WinTrigger (former OnOpen/OnClose)
; Description:  Act upon (de)activation/(un)existance of programs/windows
; Online Ref.:  http://www.autohotkey.com/forum/viewtopic.php?t=63673
;
; Last Update:  15/Mar/2010 17:30
;
; Created by:   MasterFocus
;               http://www.autohotkey.net/~MasterFocus/AHK/
;
; Thanks to:    Lexikos, for improving it significantly
;               http://www.autohotkey.com/forum/topic43826.html#267338
;
;========================================================================
;
; This template contains two examples by default. You may remove them.
;
; * HOW TO ADD A PROGRAM to be checked upon (de)activation/(un)existance:
;
; 1. Add a variable named ProgWinTitle# (Configuration Section)
; containing the desired title/ahk_class/ahk_id/ahk_group
;
; 2. Add a variable named WinTrigger# (Configuration Section)
; containing the desired trigger ("Exist" or "Active")
;
; 3. Add labels named LabelTriggerOn# and/or LabelTriggerOff#
; (Custom Labels Section) containing the desired actions
;
; 4. You may also change CheckPeriod value if desired
;
;========================================================================

#Persistent

; ------ ------ CONFIGURATION SECTION ------ ------
SetTitleMatchMode 2
; Program Titles
;ProgWinTitle1 = ahk_class rctrl_renwnd32
ProgWinTitle1 = Microsoft Outlook
WinTrigger1 = Active

; SetTimer Period
CheckPeriod = 200

; ------ END OF CONFIGURATION SECTION ------ ------

SetTimer, LabelCheckTrigger, %CheckPeriod%
Return

; ------ ------ ------

LabelCheckTrigger:
  While ( ProgWinTitle%A_Index% != "" && WinTrigger := WinTrigger%A_Index% )
    if ( !ProgRunning%A_Index% != !Win%WinTrigger%( ProgWinTitle := ProgWinTitle%A_Index% ) )
      GoSubSafe( "LabelTriggerO" ( (ProgRunning%A_Index% := !ProgRunning%A_Index%) ? "n" : "ff" ) A_Index )
Return

; ------ ------ ------

GoSubSafe(mySub)
{
  if IsLabel(mySub)
    GoSub %mySub%
}

; ------ ------ CUSTOM LABEL SECTION ------ ------

LabelTriggerOn1:
;	winget	isMax, MinMax, %ProgWinTitle1%
;	msgbox	%isMax%
	msgbox 4, Stay Focussed, Are you sure you want to read your mails?, 5
	ifmsgbox	Yes
		winmaximize	%ProgWinTitle1%
	else
		winminimize	%ProgWinTitle1%
	return
LabelTriggerOff1:
;  MsgBox % "A_ThisLabel:`t" A_ThisLabel "`nProgWinTitle:`t" ProgWinTitle "`nWinTrigger:`t" WinTrigger
Return

; ------ END OF CUSTOM LABEL SECTION ------ ------
I want to confirm when I initially bring focus to Outlook.
This works fine.
BUT, if I send an email and the focus returns to Outlook it is prompting again - any thoughts on how I can identify the difference?

Thanks In Advance

Ross

#8 ewerybody

ewerybody
  • Members
  • 65 posts

Posted 18 October 2012 - 10:13 AM

He peeps! Wasn't there another window check framework that didn't depend on periodical checking?... *think*crawl*....

#9 RossGoodman

RossGoodman
  • Members
  • 10 posts

Posted 18 October 2012 - 12:33 PM

This is probably the wrong thread for my question - as this script is written - it periodically checks to see if the window is active - as such the script is working perfectly. For the moment I'm happy to be over-zealously prompted to stay out of Outlook - I'm in in the mood for processing my mails, I can pause the script!

#10 RossGoodman

RossGoodman
  • Members
  • 10 posts

Posted 18 October 2012 - 01:49 PM

Just blogged about your script : <!-- m -->http://www.rossgoodm... ... f-outlook/<!-- m -->

#11 MasterFocus

MasterFocus
  • Moderators
  • 4132 posts

Posted 18 October 2012 - 04:18 PM

@ewerybody: A quick search for "shell hook" reveals the other method you're probably referring to.
It is indeed a different approach. As RossGoodman noted, WinTrigger offers a timer as a simple solution.
More sofisticated and/or time-critical requests may have better luck with other methods. :)

@RossGoodman: Indeed, adding a hotkey like Pause::Pause at the bottom of the script should be enough in your case.
I'm glad you liked my template so much you even blogged about it! :D
I think no one will use it for commercial purposes anyway, but I guess it's better to state that the code is licensed under AGPL v3.0, just in case. :wink:
And here's a quick guide for those (like me) who think that reading licenses is boring. :mrgreen:

#12 redking974

redking974
  • Members
  • 112 posts

Posted 25 April 2013 - 03:32 PM

Is it possible to script a thing like

LabelTriggerOn1:
SetTimer, LabelCheckTrigger, Off
...
LabelTriggerOff1:
SetTimer, LabelCheckTrigger, On
...

To desactive the check of the window when it's opened
and reactivate the timer at the close of the window?



#13 MasterFocus

MasterFocus
  • Moderators
  • 4132 posts

Posted 25 April 2013 - 03:48 PM

The "only" way to know if you opened or closed a window is with a timed subroutine.

 

In that case, it's the purpose of LabelCheckTrigger.

That's actually the only timed subroutine being constantly executed by my script.

If you disable that, there's no way to tell what's going on.

 

But why would you do that? It doesn't consume a significant amount of CPU or memory at all.



#14 redking974

redking974
  • Members
  • 112 posts

Posted 25 April 2013 - 04:38 PM

The "only" way to know if you opened or closed a window is with a timed subroutine.

 

In that case, it's the purpose of LabelCheckTrigger.

That's actually the only timed subroutine being constantly executed by my script.

If you disable that, there's no way to tell what's going on.

 

But why would you do that? It doesn't consume a significant amount of CPU or memory at all.

 

In fact, I haven't realized that the closing was linked to the timer. But yeah, it was a bad idea.



#15 Guest10

Guest10
  • Members
  • 492 posts

Posted 28 April 2013 - 05:09 PM

in my case, msgbox appears in both cases when Notepad exists or NOT exists. however, in the case of Calculator, msgbox appears only when Calculator is Active. when NOT active, unlike Notepad, msgbox does NOT appear. I also tried with SetTitleMatchMode, 2.