Jump to content

Script to close Window after 30 minutes of use?


Recommended Posts

I've been beating my head against the wall trying to get a script to do the following...

When a particular process is activated, start a timer event that will count. When the timer reaches 30 minutes, call the winclose() function to close it.

A few key things here are:

1. If the process is again started in the same day, the winclose() function should immediately close the process (gracefully).

2. If the process is initially launched and closed after 10 minutes of use, when the process is re-launched on the same day, the winclose() function should be called after 20 minutes of use as this is the remainder of time left for the day.

I am somewhat new to AutoIt. I would appreciate any assistance. I have searched the forums but haven't seen anything like this. There have been a few similar items with closing windows but I just can't get it to the level I need.

Thanks in advance

Link to comment
Share on other sites

; Manadars comment:
;   This only allows one instance of the defined process, and closes it after the allowed time.
;   It does not yet include the function that when the process is closed, the script pauses.. So, I'll might get on that later, let's just see how you like it now. :]

;#NoTrayIcon  ; to remove the tray icon. Disabled for testing purposes.

;;;; SETTINGS ;;;;
Global Const $Process = "Dumbsleep.exe"
Global Const $Minutes = 30 ;minutes allowed

;;;; PROGRAM ;;;;
Dim Const $MilliSeconds = $Minutes * 60 * 1000
Dim $Start = 0
Dim $Counter

While 1
    ProcessCheck()
    Sleep(100) ; to prevent CPU overload
WEnd


Func ProcessCheck()
    Local $List = ProcessList($Process)
    For $x = 1 To $List[0][0]
        If $x = 1 Then ; this process is still alowed. It is the only instance allowed.
            If $Start = 0 Then ; the counter has not yet started.
                $Counter = TimerInit()
                $Start = 1
            Else ; counter is already running.. check if the counter is not yet over the allowed time.
                If TimerDiff($Counter) > $MilliSeconds Then
                    ProcessClose($List[$x][1])
                EndIf
            EndIf
        Else ; these are the other instances of the process. Close these!
            ProcessClose($List[$x][1])
        EndIf
    Next
EndFunc

I had my oktoberfest yesterday, what a headache...

Edited by Manadar
Link to comment
Share on other sites

I'll just write this entire stuff for you.. I'll just start out very basic though, just to get it right the first time.

So basically, this limits your usage to 30 minutes a day.. ok :whistle: I'll get on it just now..

The response is unbelievable! I appreciate the script. I'm looking at it and will try to determine how to add in the pausing ability. This and the reset each day are key.

Thanks again

Link to comment
Share on other sites

roar.. he beat me to it... and its a heck of alot more simpler then mine.. lol

I made this one to limit my sister awhile back from using the internet:

$Time = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Limit","Time")
While 1
    If ProcessExists("iexplore.exe") Then
        ToolTip("Internet Opened",0,0)
        Do
            If RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Limit","Time") = 30 Then
                If RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Limit","Day") = @YDAY Then
                    ProcessClose("iexplore.exe")
                    MsgBox(0,"Limit Exceeded!", "You've used your daily limit of 30 minutes!")
                Else
                    RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Limit","Time")
                    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Limit","Time", "REG_SZ",0)
                    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Limit","Day", "REG_SZ",@YDAY)
                    $Time = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Limit","Time")
                    ToolTip("You have used: " & $Time & " out of 30 minutes",0,0)
                EndIf
            Else
                Sleep(1000)
                $Time = $Time + 1
                RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Limit","Time","REG_SZ",$Time)
                ToolTip("You have used: " & $Time & " out of 30 minutes",0,0)
            EndIf
        Until Not ProcessExists("iexplore.exe")
    Else
        ToolTip("Internet Not Opened",0,0)
    EndIf
WEnd

roar... nice manadar

Edited by MethodZero

[center]"When you look at old, classic games like Snake, you often put it off because it's such a simple game, but it's only when you actually try and create your own unique game from scratch, do you finally appreciate those games."[/center][center]Don't ask for answers if you haven't TRIED yet![/center][center]Most answers can be answered in the help file! Use it![/center]

Link to comment
Share on other sites

This may do the trick...

; #NoTrayIcon ; for testing 

Dim $Minutes = 30 
Dim $Title = "My Window Title" 

; settings
Dim $Show_Clock = 1 ; 0 = no show
Dim $Clock_Title = $Minutes & "  Minute Time Machine"

If WinExists($Clock_Title) Then Exit
AutoItWinSetTitle($Clock_Title)

; ***** for testing only ******
HotKeySet("{F9}", "Runner")
Func Runner()
    Run("notepad.exe")
EndFunc   ;==>Runner
$Minutes = 3 ; for testing
$Title = "Untitled" ; for testing
; *****************************

While 1
    If WinExists($Title) Then Clockit()
    Sleep(100)
WEnd

Func Clockit()
    Local $log = @WindowsDir & "\temp\"
    Local $log_file = $log & @YDAY & ".pak"
    If Not FileExists($log_file) Then 
        FileDelete($log & "*.pak")
        FileWriteLine($log_file, $Minutes)
    EndIf
    Local $M_Minutes = FileReadLine($log_file, 1)
    Local $begin = TimerInit(), $60Count = 0
    If $M_Minutes <= 0 Then
        WinClose($Title)
        MsgBox(64, $Clock_Title,  "Time-Up!! ...Your daily time usage has passed.    ", 5)
        Return
    EndIf
    While $M_Minutes > 0 And WinExists($Title)
        $dif = TimerDiff($begin)
        $Count = Int($dif / 1000)
        If $Count >= 60 Then
            $60Count += 1
            $M_Minutes -= 1
            $begin = TimerInit()
        EndIf
        If $Show_Clock Then ToolTip("Minutes Remaining = " & $M_Minutes & @CRLF & "Minutes Past = " & $60Count & @CRLF & "Seconds Count = " & $Count, 20, 20, $Clock_Title, 1)
        Sleep(100)
    WEnd
    ToolTip("")
    FileDelete($log_file)
    Sleep(300)
    If $Count >= 20 And $M_Minutes > 0 Then $M_Minutes -= 1
    FileWriteLine($log_file, $M_Minutes)    
EndFunc   ;==>Clockit

8)

NEWHeader1.png

Link to comment
Share on other sites

Now the question is how to keep the child, monkey, husband, wife, etc from realizing that the script that's blocking them from being on their favorite program can be closed from windows task manager. :P

I don't have time to script it right now, but you could have a program that when launched, will open up another program. When one is closed, the other reopens it. Problem would be having them keep their variables when they're closed... Hmmm... Interesting indeed. Maybe if they were constantly reading/writing from an .ini file, or did so every few seconds, they'd be able to keep their values... And if they need to be closed, a hotkey could pull up a password input to close them both...

GRRRR now I want to go script it.... Homework first... BBL :whistle:

Link to comment
Share on other sites

Firstly I want to thank everyone who provided code examples and comments. I've taken ideas from pretty much all of them and I'm getting very close to finishing up the project. I will be sure to post the code here for future reference.

Thanks again.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...