Jump to content

Application timer and killer


jefhal
 Share

Recommended Posts

Latest incarnation:

Allows command line parameters:

DadSoft.exe "name of app" "hours allowed"

This code (based on an idea similar to Jesse Griffin's Killspace) was written to prevent my son from using AIM for more than X hours per day. When you start it, it asks for the name of the application to track (pull down list or type in partial name), and how many hours per day you want it allowed. At the end of those hours there is a warning and the app closes. The next day, X more hours are allocated and so on. If you enter a fractional hour (e.g. .1) the monitored app will run for only .1 hour or 6 minutes. If you un-remark the tooltip lines you can see what is going on with the variables (debugging)...

#include <date.au3>
#include <GUIConstants.au3>
#Include <GuiCombo.au3>

$g_szVersion = "Dadsoft"
$day = _DateToDayValue ( @Year, @Mon, @MDAY )
If WinExists($g_szVersion) Then Exit 
AutoItWinSetTitle($g_szVersion)
Opt("TrayIconHide", 1)
Opt("TrayIconDebug",1)
HotKeySet("!^x", "ExitKill") ;Sets the hot key combination to Ctrl+Alt+X
Dim $on = 1 ;Variable says the program is running
Global $strTitle = "Untitled"
Dim $strApplication = "Application"
Dim $timeAllowed = 480000 ; enter milliseconds (e.g. 30 seconds = 30000 milliseconds)
Dim $timeElapsed = 0
Dim $timeStarted = 0
Dim $state = "Not running"
Global $timeAllowedHours = $timeallowed/3600000
Dim $var, $run, $AppList, $GetTimeAllowed
MsgBox(1,"Command line parameters?",$cmdline[0])
If $cmdline[0] = 2 Then
        ;MsgBox(1,"Command line parameters?",$cmdline[1])
        ;MsgBox(1,"Command line parameters?",$cmdline[2])
    $strTitle = execute($cmdline[1])
    $TimeAllowedHours = $cmdline[2]
Else
    $w = 575
    $h = 110
    GUICreate("DadSoft",$w,$h,@DesktopWidth/2 - $w/2,@DesktopHeight/2 - $h/2)
    $var = WinList()
    GUISetFont(12,500)
    $AppList = GUICtrlCreateCombo ("Choose the application to block (pull down list)", 10,10,550,50,BitOr($CBS_DROPDOWN, $CBS_AUTOHSCROLL, $WS_VSCROLL)) ; create first item
    For $i = 1 to $var[0][0]
    ; Only display visble windows that have a title
    If $var[$i][0] <> "" AND IsVisible($var[$i][1]) And $var[$i][0] <> "Program Manager" Then
        _GUICtrlComboAddString($AppList,$var[$i][0])
        ;MsgBox(0, "Details", "Title=" & $var[$i][0] & @LF & "Handle=" & $var[$i][1])
    EndIf
    Next
    GUISetFont(24,1)
    $lblHours = GUICtrlCreateLabel("Hours allowed:",10,60,250,30)
    $GetTimeAllowed = GUICtrlCreateInput("1",260,60,75,40)
    $updown = GUICtrlCreateUpdown($GetTimeAllowed)
    $Run = GUICtrlCreateButton("Run",480,60,80,40)
    
    GUISetState(@SW_SHOW)
EndIf

While 1
    $msg = GUIGetMsg()
    Opt("WinTitleMatchMode",2)
    Select
        Case $msg = $GUI_EVENT_CLOSE 
            Exit
        Case $msg = $Run Or $cmdline[0] <> 0
            GUISetState(@SW_HIDE)
            If $cmdline[0] = "" Then 
                $strTitle = GUICtrlRead($AppList)
                $timeAllowedHours = GUICtrlRead($GetTimeAllowed)
            Else
                $strTitle = $cmdline[1]
                $TimeAllowedHours = $cmdline[2]
            EndIf
                ;MsgBox(1,"$AppList",GUICtrlRead($AppList))
                ;MsgBox(1,"$GetTimeAllowed",GUICtrlRead($GetTimeAllowed))
            AdlibEnable("_resetStartTime",250)

            Do  
    
    ;;;;;;;;;;;;;;;; get started
            Sleep(1000)
            If WinExists(execute($strTitle)) And $day = _DateToDayValue ( @Year, @Mon, @MDAY ) And $timeElapsed = 0 Then
                    ;MsgBox(1,"Got here","2",1)
                $timeStarted = @hour + (@min/60)
                    ;MsgBox(1,"$timestarted",@HOUR + (@MIN/60),1)
                $timeElapsed = ((@hour + (@min/60)) + .0001) - $timeStarted
                $restart = 0
            EndIf

            Sleep(1000)
    ;;;;;;;;;;;; what to do while time hasn't run out
            If WinExists(execute($strTitle)) And $day = _DateToDayValue ( @Year, @Mon, @MDAY ) And $timeElapsed < $timeAllowedHours Then
                    ;MsgBox(1,"Got here","3",1)
                $timeElapsed = ((@hour + (@min/60)) + .0001) - $timeStarted
            EndIf

            Sleep(1000)
    ;;;;;;;;;;;;;;; what to do when time runs out
                ;MsgBox(1,"Does WinTitle exist?",WinExists(execute($strTitle)) & "   :   " & $strTitle) 
            If WinExists(execute($strTitle)) And $day = _DateToDayValue ( @Year, @Mon, @MDAY ) And $timeElapsed > $timeAllowedHours Then
                    ;MsgBox(1,"Got here","5",1)
                    ;ToolTip($strApplication & ": " & $state & @CRLF & "Current time: " & @HOUR + (@MIN/60) & @CRLF & "Time started: " & $timestarted & @CRLF & "Minutes elapsed: " & $timeElapsed*60 &  @CRLF & "Minutes allowed: " & $timeAllowedHours*60 & @CRLF & "Minutes left: " & ($timeAllowedHours - $timeElapsed)*60 & @CRLF & "Day: " & $day,750,100,"DadSoft Warning")
                MsgBox(1,"Warning","Your " & $timeAllowedHours & " hours for using " & $strApplication & " has run out...",10)
                Sleep(3000)
                MsgBox(1,"End",$strApplication & " will now close.",5)
                WinClose($strTitle)
            EndIf

            Sleep(1000)
                ;MsgBox(1,"Got here","7",2)
            If WinExists(execute($strTitle)) Then 
                $state = "Running"
            Else
                $state = "Not running"
            EndIf
    
            Sleep(1000)
                ;ToolTip($strApplication & ": " & $state & @CRLF & "Current time: " & @HOUR + (@MIN/60) & @CRLF & "Time started: " & $timestarted & @CRLF & "Minutes elapsed: " & $timeElapsed*60 &  @CRLF & "Minutes allowed: " & $timeAllowedHours*60 & @CRLF & "Minutes left: " & ($timeAllowedHours - $timeElapsed)*60 & @CRLF & "Day: " & $day,750,100,"DadSoft Warning")
            Sleep(4000)
    
            If $day < _DateToDayValue ( @Year, @Mon, @MDAY ) Then
                $day = _DateToDayValue ( @Year, @Mon, @MDAY )
                MsgBox(1,"_dateToDayValue(YMD)",_DateToDayValue(@YEAR,@MON,@MDAY))
                $timeElapsed = 0
            EndIf
    
            Until $on = 0 ;Repeat this process until the program is turned off

        ToolTip("")
    EndSelect
WEnd

Func ExitKill() ;When the hot key combination is pressed
    $on = 0 ;Turn the program off
EndFunc

Func _resetStartTime()
    If WinExists($strTitle) = 0 And $timeElapsed = 0 Then
            ;ToolTip($strTitle & ": " & $state & @CRLF & "Current time: " & @HOUR + (@MIN/60) & @CRLF & "Time started: " & $timestarted & @CRLF & "Minutes elapsed: " & $timeElapsed*60 &  @CRLF & "Minutes allowed: " & $timeAllowedHours*60 & @CRLF & "Minutes left: " & ($timeAllowedHours - $timeElapsed)*60 & @CRLF & "Day: " & $day,750,100,"DadSoft Warning")
            ;MsgBox(1,"Does WinTitle exist?",WinExists(execute($strTitle)) & "   :   " & $strTitle) 
        WinWait($strTitle)
        $timeStarted = (@HOUR + (@MIN/60))
        $timeElapsed = .0001
    ElseIf WinExists($strTitle) = 0 And $timeElapsed <> 0 Then
            ;ToolTip($strTitle & ": " & $state & @CRLF & "Current time: " & @HOUR + (@MIN/60) & @CRLF & "Time started: " & $timestarted & @CRLF & "Minutes elapsed: " & $timeElapsed*60 &  @CRLF & "Minutes allowed: " & $timeAllowedHours*60 & @CRLF & "Minutes left: " & ($timeAllowedHours - $timeElapsed)*60 & @CRLF & "Day: " & $day,750,100,"DadSoft Warning")
        WinWait($strTitle)
        $timeStarted = (@HOUR + (@MIN/60)) - $timeElapsed
    EndIf
EndFunc

Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 2 ) Then 
    Return 1
  Else
    Return 0
  EndIf
EndFunc
Edited by jefhal
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Nice script! Works like a charm. Now I see that I was going about the timer on Killspace all wrong. Now you've inspired me to work on Killspace again!

Go for it! Also, after I added the AdLibEnable part, I'm not sure I even need some of the other code in my script. Try to eliminate some code if you can... :D
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Go for it! Also, after I added the AdLibEnable part, I'm not sure I even need some of the other code in my script. Try to eliminate some code if you can... :D

I'll try. I will probably work on that before I begin Killspace again...just to get a better feel for how you did things.

Edited by Woobs
Link to comment
Share on other sites

CODE

#include <date.au3>

#include <GUIConstants.au3>

#Include <GuiCombo.au3>

$g_szVersion = "Dadsoft"

$day = _DateToDayValue ( @Year, @Mon, @MDAY )

If WinExists($g_szVersion) Then Exit

AutoItWinSetTitle($g_szVersion)

AutoItSetOption("TrayIconHide", 1)

Opt("TrayIconDebug",1)

Opt("WinTitleMatchMode",2)

HotKeySet("!^x", "ExitKill") ;Sets the hot key combination to Ctrl+Alt+X

Dim $on = 1 ;Variable says the program is running

Dim $strTitle = "Untitled"

Dim $strApplication = "Application"

Dim $timeAllowed = 480000 ; enter milliseconds (e.g. 30 seconds = 30000 milliseconds)

Dim $timeElapsed = 0

Dim $timeStarted = 0

Dim $state = "Not running"

Dim $timeAllowedHours = $timeallowed/3600000

Dim $var

$w = 575

$h = 110

GUICreate("DadSoft",$w,$h,@DesktopWidth/2 - $w/2,@DesktopHeight/2 - $h/2)

$var = WinList()

GUISetFont(12,500)

$AppList = GUICtrlCreateCombo ("Choose the application to block (pull down list)", 10,10,550,50,BitOr($CBS_DROPDOWN, $CBS_AUTOHSCROLL, $WS_VSCROLL)) ; create first item

For $i = 1 to $var[0][0]

; Only display visble windows that have a title

If $var[$i][0] <> "" AND IsVisible($var[$i][1]) And $var[$i][0] <> "Program Manager" Then

_GUICtrlComboAddString($AppList,$var[$i][0])

EndIf

Next

GUISetFont(24,1)

$lblHours = GUICtrlCreateLabel("Hours allowed:",10,60,250,30)

$GetTimeAllowed = GUICtrlCreateInput("1",260,60,75,40)

$updown = GUICtrlCreateUpdown($GetTimeAllowed)

$Run = GUICtrlCreateButton("Run",480,60,80,40)

GUISetState(@SW_SHOW)

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

Exit

Case $msg = $Run

GUISetState(@SW_HIDE)

$strTitle = GUICtrlRead($AppList)

$timeAllowedHours = GUICtrlRead($GetTimeAllowed)

AdlibEnable("_resetStartTime",250)

Do

;;;;;;;;;;;;;;;; get started

Sleep(1000)

If WinExists($strTitle) And $day = _DateToDayValue ( @Year, @Mon, @MDAY ) And $timeElapsed = 0 Then

$timeStarted = @hour + (@min/60)

$timeElapsed = ((@hour + (@min/60)) + .0001) - $timeStarted

$restart = 0

EndIf

Sleep(1000)

;;;;;;;;;;;; what to do while time hasn't run out

If WinExists($strTitle) And $day = _DateToDayValue ( @Year, @Mon, @MDAY ) And $timeElapsed < $timeAllowedHours Then

$timeElapsed = ((@hour + (@min/60)) + .0001) - $timeStarted

EndIf

Sleep(1000)

;;;;;;;;;;;;;;; what to do when time runs out

If WinExists($strTitle) And $day = _DateToDayValue ( @Year, @Mon, @MDAY ) And $timeElapsed > $timeAllowedHours Then

MsgBox(1,"Warning","Your time for using " & $strApplication & " has run out...",10)

Sleep(3000)

MsgBox(1,"End",$strApplication & " will now close.",5)

WinClose($strTitle)

EndIf

Sleep(1000)

If WinExists($strTitle) Then

$state = "Running"

Else

$state = "Not running"

EndIf

Sleep(4000)

If $day < _DateToDayValue ( @Year, @Mon, @MDAY ) Then

$day = _DateToDayValue ( @Year, @Mon, @MDAY )

MsgBox(1,"_dateToDayValue(YMD)",_DateToDayValue(@YEAR,@MON,@MDAY))

$timeElapsed = 0

EndIf

Until $on = 0 ;Repeat this process until the program is turned off

ToolTip("")

EndSelect

WEnd

Func ExitKill()

$on = 0

EndFunc ;==>ExitKill

Func _resetStartTime()

If WinExists($strTitle) = 0 And $timeElapsed = 0 Then

WinWait($strTitle)

$timeStarted = (@HOUR + (@MIN/60))

$timeElapsed = .0001

ElseIf WinExists($strTitle) = 0 And $timeElapsed <> 0 Then

WinWait($strTitle)

$timeStarted = (@HOUR + (@MIN/60)) - $timeElapsed

EndIf

EndFunc ;==>_resetStartTime

Func IsVisible($handle)

If BitAnd( WinGetState($handle), 2 ) Then

Return 1

Else

Return 0

EndIf

EndFunc ;==>IsVisible

Made a fast look at it and shortened it a bit :D

Hope it does not make any problems now :D

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Link to comment
Share on other sites

This is nice! I can get my brother off faster! (He is sooo annoying!)

http://www.autoitking.co.nr Site is DOWN | My deviantART | No Topic Topic - Don't do it!-------------------- UDF's/Scripts:AutoIt: [BenEditor 3.6] [_ShutDown()]PHP: [CommentScript]Web Based AutoIt: [MemStats] [HTML to AU3] [User LogIn and SignUp script]
Link to comment
Share on other sites

when i read all this i feel happy that i dont have a sister or a brother ^^

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Link to comment
Share on other sites

They're fine, just don't let them near the computer :D

Exactly, my brother is really bad like that. He just comes up to me and starts yelling "get off it's my turn you idiot!" over and over again, then he starts with the name calling when I don't let him on and then he wonders why, so I tell him "Say PLEASE". Then he yells "Mom, tell ben to get of the computer, it's my turn" then my mom yells back "ben, let your brother on". So I finally get off. And he never said please. I doubt he even knows the word please exists! lol (he's 12)
http://www.autoitking.co.nr Site is DOWN | My deviantART | No Topic Topic - Don't do it!-------------------- UDF's/Scripts:AutoIt: [BenEditor 3.6] [_ShutDown()]PHP: [CommentScript]Web Based AutoIt: [MemStats] [HTML to AU3] [User LogIn and SignUp script]
Link to comment
Share on other sites

He just comes up to me and starts yelling "get off it's my turn you idiot!"

I have two kids and 16 computers in the house, and they still insist on using the one computer in the family room that's next to the big tv. So.... DadSoft to the rescue. I guess the next version will track which one of their screen names is active and give them each a time limit. Seem fair?
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Made a fast look at it and shortened it a bit :D

Hope it does not make any problems now :D

Seems to still run fine. I put the tooltips back in since they are useful for debugging and they take a lot of time to type out (lazy)... Thanks for the help...
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Fair :D

But i think when you got 16 computers, why cant you give both of them one into their room and then tell them that they are able to use those and no else in your house.

I think they feel better too if they have their own computer.

I used the computer of my dad to before i've been 10, then he gave me his old computer and i felt better because i got my own that i could destroy^^.

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Link to comment
Share on other sites

Fair :D

But i think when you got 16 computers, why cant you give both of them one into their room and then tell them that they are able to use those and no else in your house.

I think they feel better too if they have their own computer.

I used the computer of my dad to before i've been 10, then he gave me his old computer and i felt better because i got my own that i could destroy^^.

Well, my kids like TV as much as they like IM. Therefore, they both prefer to be in the room with the TV and the computer. My son also plays PC games, and his bedroom computer (933-PIII) does not really excel at those, while the TV room computer does....

As someone who does not even like electronic games (reaction times too slow), I look at the whole game/IM/TV thing as a huge waste of time, except if you plan to join the Air Force or Army. I guess if you know how to fly a predator, you may not have to carry a gun on the front lines!

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Seems very fair. When they reach their time limit, will you log them off the computer or close every application and keep them from opening others?

Just log them off IM, since without IM, what's the point of having the hard disk spinning?... :D
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

You mean it can spin without AIM running? I guess I just like the look on peoples faces as the application they are on closes due to my programming "prowess."

Just don't try it with Word or Powerpoint where they lose 4 hours of hard work and never have a chance to save! DOH! :D
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Just don't try it with Word or Powerpoint where they lose 4 hours of hard work and never have a chance to save! DOH! :D

:D Well, schools out for Summer, so MS Word is collecting dust. I wouldn't think of that; I might forget to turn off the program and lose my own work... :wacko:

Link to comment
Share on other sites

Check out the new code which allows command line parameters for running from HKLM-Software-Microsoft-Windows-Run or from batch file...

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
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...