Monday, March 19, 2012

AutoIt countdown timer

So i've been playing around with AutoIt recently. Its a great little piece of software for writting automated programs. Its like a cross between C and python. But the best part about it is that as its comunity based anyone can write their own functions for it and add them to the site. Great!

Anyway, we've selected a few open access computers that are going to be used for electronic submission only. The higher manangement wanted a piece of software that would instruct the student logged on that they would have a maximum of 15 minutes on the PC, and then get logged off. After a bit of discussion I managed to get them to accept giving the students a warning that their time was up and then the counter would count backwards in big red writting.

So this is the code

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>

$ms = 900250 ;This is the time in milliseconds Adjust as needed
$timer = TimerInit()
traysetstate(2)
$labeltext = "This computer is primarily for electronic submission. You will be automatically logged off after 15 minutes"
GUICreate("Electronic Submission", 350, 330, -1, -1, $WS_DLGFRAME, $WS_EX_TOPMOST)
$Label = GUICtrlCreateLabel($labeltext, 10, 10, 330, 200, $SS_CENTER)
GUICtrlSetFont(-1, 18, 400, $labeltext)
$Input = GUICtrlCreateInput("", 125, 150, 90, 40, BitOr($SS_CENTER, $ES_READONLY, $WS_CLIPCHILDREN))
GUICtrlSetFont(-1, 18, 400)
GUISetState (@SW_SHOW)
WinSetTrans("Electronic Submission","", 127)
While (TimerDiff($timer) < $ms)
   $seconds = TimerDiff($timer)/1000
   $diff = $seconds - ($ms/1000);
   $minutes = Int($diff / 60)
   $secondsRem = $diff - ($minutes * 60)
   $minutes = $minutes * -1
   $secondsRem = $secondsRem * -1
   $time = StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem)
   GUICtrlSetData($Input, $time)
   WEnd
 
   Global $reply = MsgBox(1, "TIMES UP", &@UserName " you have had 15 minutes on this computer. If someone is waiting please let them use it. CLICK OK TO LOG OFF",30)
   If $reply = 1  then run("shutdown.exe -l")
  
   $Input = GUICtrlCreateInput("", 25, 125, 300, 100, BitOr($SS_CENTER, $ES_READONLY, $WS_CLIPCHILDREN))
   GUICtrlSetColor(-1, 0xff0000)
   GUICtrlSetFont(-1, 50, 400)
  
   While (TimerDiff($timer) > $ms)
   $seconds = TimerDiff($timer)/1000
   $diff = $seconds - ($ms/1000)
   $minutes = Int($diff / 60)
   $secondsRem = $diff - ($minutes * 60)
   $minutes = $minutes * -1
   $secondsRem = $secondsRem * -1
   $time = StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem)
   GUICtrlSetData($Input, $time)
   WEnd
   run("shutdown.exe -l")

Its not the best code, but im just learning it. feel free to adjust and use it for yourselves.

2 comments:

Jarmez said...

Hey man,

Awesome script. I was looking for something a little bit similar and then got distracted about reading about GUI's with no buttons (press OK only) and stumbled across this. I did a test subbing notepad and dropping time to 10 seconds and works great! love the transparency....super annoying haha, just about as annoying as those darn users on computers for longer than they are supposed to be ;-)

One little mistake. Theres a typo in your code and won't execute at all as it is. I actually have no idea how you've done it, cause how can someone type all that nice code and then slip up and make it not execute when it is done so well?
Wanna know where it is? Line 28 @UserName needs to go before &

Thanks foryour handy script. Might have something to trade for you one day ;-)

James

RichardS said...

Hey Jarmez,

Glad you like the script!

Actually I've had no trouble running it with the & before or after the @username. I'm running SciTE-Lite Version 2.28 with AutoIt version 3.3.8.1. Not sure if that make a difference or not but it works fine for me as is. At least it helps other people who might have the same problem.

Cheers buddy!