Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

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


  • Please log in to reply
27 replies to this topic
MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
####################################################################################
This old forum will be archived ( see https://autohotkey.c...s-decommission/ ).
For my up-to-date code and information, check the following links:
http://git.io/master | https://github.com/MasterFocus | http://masterfocus.ahk4.net/ | https://sites.google...masterfocusahk/
####################################################################################


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.


Template:
    WinTrigger

Description:
    Act upon (de)activation/(un)existance of programs/windows

Links:
    GitHub (check the AutoHotkey repository)

Related scripts:
• WinWait framework by berban - http://www.autohotke...php?f=2&t=92017

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


Chicken Pie 4 Tea
  • Members
  • 379 posts
  • Last active: Dec 12 2014 06:46 PM
  • Joined: 18 Aug 2009
Thanks, yes it does deserve its own thread as I had not noticed it before.
"Choose your parents wisely"

dave_100
  • Members
  • 103 posts
  • Last active: Mar 05 2015 10:03 PM
  • Joined: 11 Jul 2010
an amazing script to auto run scripts when certain events occur, thank you!

RossGoodman
  • Members
  • 10 posts
  • Last active: Mar 06 2012 11:02 AM
  • Joined: 18 Jan 2011
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?

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
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.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


RossGoodman
  • Members
  • 10 posts
  • Last active: Mar 06 2012 11:02 AM
  • Joined: 18 Jan 2011
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

RossGoodman
  • Members
  • 10 posts
  • Last active: Mar 06 2012 11:02 AM
  • Joined: 18 Jan 2011
OK, it's soooo close to doing what I want:
Code removed from this post.
Please see: github.com/MasterFocus
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

ewerybody
  • Members
  • 70 posts
  • Last active: Feb 01 2017 01:55 PM
  • Joined: 18 Sep 2006
He peeps! Wasn't there another window check framework that didn't depend on periodical checking?... *think*crawl*....

RossGoodman
  • Members
  • 10 posts
  • Last active: Mar 06 2012 11:02 AM
  • Joined: 18 Jan 2011
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!

RossGoodman
  • Members
  • 10 posts
  • Last active: Mar 06 2012 11:02 AM
  • Joined: 18 Jan 2011
Just blogged about your script : <!-- m -->http://www.rossgoodm... ... f-outlook/<!-- m -->

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
@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:

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


redking974
  • Members
  • 205 posts
  • Last active: Feb 11 2015 08:31 PM
  • Joined: 06 Apr 2013

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?



MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009

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.


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


redking974
  • Members
  • 205 posts
  • Last active: Feb 11 2015 08:31 PM
  • Joined: 06 Apr 2013

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.



Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

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.