WinBatch® Technical Support Forum

All Things WinBatch => WebBatch => Topic started by: jfrasier on August 14, 2013, 10:11:32 AM

Title: Inactivity
Post by: jfrasier on August 14, 2013, 10:11:32 AM
I am configuring a computer for a public library. I would like to have a script that detects when there is no activity (keystrokes or mouse) and if a certain amount of time has passed I would like the script to restart the browser (Chrome). Is this possible?

Thanks

Jane
Title: Re: Inactivity
Post by: Deana on August 14, 2013, 10:28:32 AM
You are in luck. There is a code sample containing a UserDefinedFunction called Idle_Tick to handle this in the tech database: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/DllCall~Information+Check~for~Input~Idle~-~No~User~Input.txt
Title: Re: Inactivity
Post by: jfrasier on August 14, 2013, 11:34:17 AM
Thanks. I was trying to figure out something along those lines but was stuck.
Title: Re: Inactivity
Post by: jfrasier on August 14, 2013, 01:38:05 PM
I am kind of breaking this down into smaller pieces to test.

I have :

TimeDelay(5)
;this is testing. I have to click on the application in these 5 seconds

currentwin = WinGetactive( )

; just to see what it thinks is the current window
Message("WinGetActive", "Current window is %currentwin%.")

WinClose("currentwin")
TimeDelay(5)
Run(currentwin, "incognito")



The error is that it can't find the window in this line WinClose("currentwin")

I have also tried ("~currentwin") (~new tab")    (which is the beginning of the window name.

Can you help?

thanks.
Title: Re: Inactivity
Post by: Deana on August 14, 2013, 02:00:33 PM
Quote from: jfrasier on August 14, 2013, 01:38:05 PM
I am kind of breaking this down into smaller pieces to test.

I have :

TimeDelay(5)
;this is testing. I have to click on the application in these 5 seconds

currentwin = WinGetactive( )

; just to see what it thinks is the current window
Message("WinGetActive", "Current window is %currentwin%.")

WinClose("currentwin")
TimeDelay(5)
Run("incognito","")



The error is that it can't find the window in this line WinClose("currentwin")

I have also tried ("~currentwin") (~new tab")    (which is the beginning of the window name.

Can you help?

thanks.

Quotation marks are used to specify a string. Yet, it looks like you are trying to pass the variable currentwin to the WinClose function. You will need to remove the quotation marks around your variable name to have it treated as a variable rather than a string.

Code (winbatch) Select
TimeDelay(5)
;this is testing. I have to click on the application in these 5 seconds

currentwin = WinGetActive( )

; just to see what it thinks is the current window
Message("WinGetActive", "Current window is ":currentwin:".") ;Uses the colon operator for string concatenation.

WinClose(currentwin)
TimeDelay(5)
Run("incognito","")

.
Title: Re: Inactivity
Post by: jfrasier on August 19, 2013, 01:57:16 PM
Thanks for your help. I don't know if this is possible, but it really does check for inactivity every so many seconds and restarts Chrome. Is there any way to have it not do it if no one has used the computer in between? For example,  I am sitting next to my test computer doing other things and it keeps closing Chrome and then opening it. It would only need to do that if I done something on that computer and THEN was inactive.

Thanks again.

Jane
Title: Re: Inactivity
Post by: Deana on August 19, 2013, 02:42:37 PM
Quote from: jfrasier on August 19, 2013, 01:57:16 PM
Thanks for your help. I don't know if this is possible, but it really does check for inactivity every so many seconds and restarts Chrome. Is there any way to have it not do it if no one has used the computer in between? For example,  I am sitting next to my test computer doing other things and it keeps closing Chrome and then opening it. It would only need to do that if I done something on that computer and THEN was inactive.

Thanks again.

Jane

Please post the code you are using.
Title: Re: Inactivity
Post by: jfrasier on August 20, 2013, 07:44:15 AM
#DefineFunction Idle_Tick()
   ; Get the system uptime
   systemUptime = GetTickCount()

   ; The tick at which the last input was recorded
   LastInputTicks = 0

   ; The number of ticks that passed since last input
   IdleTicks = 0

   ; Set the struct
   LastInputInfo = BinaryAlloc(8)
   BinaryPoke4(LastInputInfo,0,8);cbSize
   BinaryPoke4(LastInputInfo,4,0);dwTime

   ; If we have a value from the function
   ret = DllCall(DirWindows(1):'user32.dll',long:'GetLastInputInfo',lpbinary:LastInputInfo)
   If ret
      ; Get the number of ticks at the point when the last activity was seen
      LastInputTicks = BinaryPeek4(LastInputInfo,4) ;dwTime

      ; Number of idle ticks = system uptime ticks - number of ticks at last input
      IdleTicks = systemUptime - LastInputTicks
   EndIf

   LastInputInfo = BinaryFree(LastInputInfo)

   ;Convert milliseconds to seconds
   SystemUptime = systemUptime / 1000
   IdleTime = IdleTicks / 1000
   LastInput = LastInputTicks / 1000
   Return IdleTime
#EndFunction


While @TRUE
   tick = Idle_Tick()
   If tick==20
      currentwin = WinGetActive()
      WinClose(currentwin)
      TimeDelay(5)
      Run("Chrome","-incognito")
      TimeDelay(1)

   EndIf


EndWhile
Title: Re: Inactivity
Post by: jfrasier on December 19, 2013, 02:22:16 PM
I am still interested in finding an answer to my question above. Thanks.

Jane
Title: Re: Inactivity
Post by: Deana on December 20, 2013, 08:53:02 AM
I suppose you will need to keep track of the last input time. IF you look closely at the UDF it calculates three possible values but only returns one (IdleTime):

Code (winbatch) Select
;Convert milliseconds to seconds
SystemUptime = systemUptime / 1000
IdleTime = IdleTicks / 1000
LastInput = LastInputTicks / 1000
Return IdleTime


Maybe modify the UDF to return both IdleTime and Lastinput as a tab delimited list.
Title: Re: Inactivity
Post by: td on December 20, 2013, 12:53:22 PM
The script has several issues, not the least of which is the line "If tick==20".
Title: Re: Inactivity
Post by: Deana on December 20, 2013, 01:13:51 PM
Here is my undebugged attempt at improving that code to fit your needs:

Code (winbatch) Select
#DefineFunction GetLastInput()
   ; Note: The following script is fundamentally flawed because the 'GetTickCount' and possibly the 'GetLastInputInfo 'function will start returning negative numbers
   ; after the computer has been up for ~24.85 days   and 'GetTickCount' will reset after ~49.7 days. This means that you can get either negative or incorrect positive
   ; idle times if the computer is up long enough.

   ; Set the struct
   LastInputInfo = BinaryAlloc(8)
   BinaryPoke4(LastInputInfo,0,8);cbSize
   BinaryPoke4(LastInputInfo,4,0);dwTime

   ; If we have a value from the function
   ret = DllCall(DirWindows(1):'user32.dll',long:'GetLastInputInfo',lpbinary:LastInputInfo)
   If ret
      ; Get the number of ticks at the point when the last activity was seen
      LastInputTicks = BinaryPeek4(LastInputInfo,4) ;dwTime
   EndIf

   LastInputInfo = BinaryFree(LastInputInfo)

   Return LastInputTicks
#EndFunction

secondstowait = 90
BoxOpen('Number of seconds since last input','')
While @true

   lastinput = Int((GetTickCount() - GetLastInput()) / 1000)
   BoxText(lastinput)
   If lastinput > secondstowait  ;check if it has been 60 seconds 
      ;Do whatever commands here that you want to happen 
      ;if no user input is detected in last {n} seconds. 
      Pause("Notice","System Idle for ":secondstowait)
   EndIf
EndWhile
exit
Title: Re: Inactivity
Post by: jfrasier on December 20, 2013, 03:02:13 PM
Thank you so much for taking the time to do this. I am trying to make it work for my situation and am not quite there. i will continue to work on it next week. Right now it is kinda sorta working.

Jane