Jump to content

Recommended Posts

Posted

I have spent the past couple of days searching for the answer to this question, and I have not found one that really works.

What I am trying to do, is have the following code always run and when ever notepad is open and idle for more then 2 seconds close it. It works but only when their is one instance of Notepad, if there are more then one it does not close all of them at the same time.

Can someone help me, using this code, to have all windows with the title Notepad close?

#include <Timers.au3>

;Declare the Timer:
; Global $TIMER = TimerInit()

Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)


HotKeySet("#{F4}", "_exit") ; Ctrl-Shift-Alt-X to exit


While 1

    ;If this application is active then reset the timer:
    If WinExists("Notepad") And _Timer_GetIdleTime() >= 2 * 1000 Then
     MsgBox (0, "Time reached", "You have been idle for more than 2 seconds.")
WinClose("Notepad")


EndIf

    ;Sleep for 1 seconds before looping again:
;    Sleep(1000)
WEnd

Func _exit()
Exit
EndFunc

Thanks

Grimm

Thanks

Grimm

  • Moderators
Posted

grimmlock,

Use WinList to get the handles of any open NotePad GUIs and then loop through the returned array using WinClose on each one. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

Hiya Melba23,

I copied this from the WinList function (Autoit), I am a little confused as to how to incorporate it :). I do not see where I would put in "Notepad"?

Local $var = WinList()

For $i = 1 To $var[0][0]
    ; Only display visble windows that have a title
    If $var[$i][0] <> "" And IsVisible($var[$i][1]) Then
        MsgBox(0, "Details", "Title=" & $var[$i][0] & @LF & "Handle=" & $var[$i][1])
    EndIf
Next

Func IsVisible($handle)
    If BitAND(WinGetState($handle), 2) Then
        Return 1
    Else
        Return 0
    EndIf

EndFunc   ;==>IsVisible

Thanks

Grimm

  • Moderators
Posted

grimmlock,

You need to give a parameter to the WinListfunction so that it only lists Notepad GUIs. As you do not know the titles of all the GUIs, you need to look for the class - which you can get via the AutoIt Window Info Tool. ;)

WinList("[CLASS:Notepad]")

All clear? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

  • Moderators
Posted

grimmlock,

  Quote

$var=WinList(Notepad)

That will not work - you need to use the correct syntax which I gave you in my last post. As you do not know the titles of the various Notepad GUIs, you need to use the class. Look in the Help file under <Using AutoIt - Window Titles and Text (Advanced)>. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

  • Moderators
Posted

grimmlock,

Are all the Notepad instances titled the same? If so then by all means just use that. But why not use the class and make life simpler. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...