Jump to content

Run function only between 7 am and 6pm


Recommended Posts

Dear forum, 

 

although now searching for quite some time, I did not find an answer to my problem that really works. 

 

I have a small function that only moves the mouse pointer every some seconds in order to prevent the auto-screen lock from kicking-in. 

What I would like to add now is a time frame, within this function is active (between 7am and 6 pm) and have it idle during out of office hours. 

 

I tried the following - but it didn`t work (although no error message): Mouse shifting works, but it is always active. What is wrong? 


func _stop()
   Exit
endFunc

If @Hour > 6 or @Hour < 19 Then  

While True
  Sleep(100000)
 MouseMove(1,1)
 Sleep(100000)
 MouseMove(10,10)

WEnd
EndIf
 

Link to comment
Share on other sites

HotKeySet("{ESC}", "_Terminate")
While (@HOUR >= 7) And (@HOUR <= 18)
    MouseMove(1, 1)
    Sleep(60000)
    MouseMove(2, 2)
WEnd
Exit

Func _Terminate()
    MsgBox(BitOR(4096, 64), "Message :", "Script terminated by User" & @CRLF)
    Exit
EndFunc

The version above is crap !

EDIT :

The following version is 2% better, but still unsatisfying.

HotKeySet("{ESC}", "_Terminate")

While True
    If (@HOUR >= 7) And (@HOUR <= 18) Then
        MouseMove(1, 1)
        Sleep(60000)
        MouseMove(2, 2)
    Else
        Sleep(60000)
    EndIf
WEnd

Func _Terminate()
    MsgBox(BitOR(4096, 64), "Message :", "Script terminated by User" & @CRLF)
    Exit
EndFunc

 

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

  • Developers

That IF needs tot be in the loop around the action.

..  @Musashi..  that sollution only runs one time and exits the loop outside the active time. ;)

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Hi @Technohead,

you have two logic failures.

  • The current hour should be between 7 And 18, not or like you did it.
  • Your if condition has to be in the While loop, not outside.
HotKeySet('{F10}', '_Exit')

Func _Exit()
   Exit
EndFunc

Func _IsCurrentHourBetween($iBetweenFrom, $iBetweenTo)
    Local $iHour = Int(@HOUR)

    If $iHour >= $iBetweenFrom And $iHour <= $iBetweenTo Then
        Return True
    EndIf

    Return False
EndFunc

While True
    If _IsCurrentHourBetween(7, 18) Then
        Global $iRandomXPosition = Random(100, 600, 1)
        Global $iRandomYPosition = Random(200, 500, 1)

        MouseMove($iRandomXPosition, $iRandomYPosition)
    EndIf

    Sleep(1000)
WEnd

Best regards
Sven

________________
Stay innovative!
 

Edited by SOLVE-SMART

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

@Technohead

Within time frame:

#include <WinAPISysWin.au3>
HotKeySet("{ESC}","_stop")
Local $hTimerProc = DllCallbackRegister('_TimerProc', 'none', 'hwnd;uint;uint_ptr;dword')
Local $TimerTi = 1000
Local $iTimerID = _WinAPI_SetTimer(0, 0, $TimerTi , DllCallbackGetPtr($hTimerProc))
func _stop()
   Exit
endFunc

While True

WEnd

Func _Time($fromhour,$Tohour,$sleep)
    if @HOUR >= $fromhour And @HOUR <= $Tohour Then
        Sleep($sleep)
        MouseMove(1,1)
        EndIf
EndFunc
Func _TimerProc($hWnd, $iMsg, $iTimerID, $iTime)
    #forceref $hWnd, $iMsg, $iTimerId, $iTime
_Time(7,18,10000)
EndFunc   ;==>_TimerProc

 

Edited by ad777

iam ِAutoit programmer.

best thing in life is to use your Brain to

Achieve

everything you want.

Link to comment
Share on other sites

Hi folks,

2 minutes ago, Jos said:

Ah... A code barding match... And now we wait who gets the win. :)

Doesn't matter too much, but at least I tried to explain him the failures.
Should be my "point" 😅 , ... just kidding, doesn't matter.

Best regards
Sven

________________
Stay innovative!

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

6 minutes ago, Jos said:

..  @Musashi..  that sollution only runs one time and exits the loop outside the active time. ;)

You are right, of course ! A typical case of poor concentration after a long day at the office :mad2:.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Guys, you are awesome! 

 

@ad777: Your script gives me an error I was not able to solve yet: 

image.png.45a4b47f05c9aec039886d8f37742113.png

 

@solve-smart: Your script seems to work perfectly. I only adjusted the sleep settings and also removed the random mouse shifts, as the screen is shared constantly and the mouse moving in the middle of the shared screen is quite annoying. 

Will try if it really pauses outside business hours - and if so: Topic solved ;). 

 

HotKeySet('{F10}', '_Exit')

Func _Exit()
   Exit
EndFunc

Func _IsCurrentHourBetween($iBetweenFrom, $iBetweenTo)
    Local $iHour = Int(@HOUR)

    If $iHour >= $iBetweenFrom And $iHour <= $iBetweenTo Then
        Return True
    EndIf

    Return False
EndFunc

While True
    If _IsCurrentHourBetween(7, 18) Then
     Sleep(100000)
     MouseMove(1,1)
     Sleep(100000)
     MouseMove(10,10)
    EndIf

    Sleep(1000)
WEnd

 

 

 

Link to comment
Share on other sites

1 hour ago, Technohead said:

@ad777: Your script gives me an error I was not able to solve yet: 

You probably don't have the latest AutoIt-Version installed. Sometimes functions are swapped to new includes.

Use :

#include <WinAPISys.au3>

instead of :

#include <WinAPISysWin.au3>

 

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Thanks! 

Question: I tried to use adapted script below and altered time to be active only between 07:00 and 11:00. 

As I now tried with playing around with time settings it seems to be the case, that if entered 07:00 to 11:00 then the function is active until 11:59 only stopping as the full hour switches to 12. Is there any time definition that I could add to below script in order to set the timing more accurate to hh:mm instead of full hour only? 

 

HotKeySet('{F10}', '_Exit')

Func _Exit()
   Exit
EndFunc

Func _IsCurrentHourBetween($iBetweenFrom, $iBetweenTo)
    Local $iHour = Int(@HOUR)

    If $iHour >= $iBetweenFrom And $iHour <= $iBetweenTo Then
        Return True
    EndIf

    Return False
EndFunc

While True
    If _IsCurrentHourBetween(07:00, 11:00) Then
     Sleep(100000)
     MouseMove(1,1)
     Sleep(100000)
     MouseMove(10,10)
    EndIf

    Sleep(1000)
WEnd

Link to comment
Share on other sites

@Technohead

use  @MIN Macro.

$from = "12:01"
$to =   "13:53"
_Time($from,$to,100)
Func _Time($from,$to,$sleep)
    Local $tohourmin = @HOUR &":"&@MIN
    Local $fromhourmin = @HOUR &":"&@MIN
    if  $fromhourmin >= $from And $tohourmin <=  $to Then
        Sleep($sleep)
        MouseMove(1,1)
    EndIf
EndFunc

 

Edited by ad777

iam ِAutoit programmer.

best thing in life is to use your Brain to

Achieve

everything you want.

Link to comment
Share on other sites

@Earthshine: Sorry - and I understand that it might be frustrating dealing with someone who has no good programming skills like me. In fact - none, and I try to find my way around with reading and understanding. 

 

@ad777: Thanks: One question remaining: If I understand your macro, it runs if it is within specified time frame but stops afterwards. What I would like it to do is to not end the program after time is over but to stay inactive, until it is back into the "active" time frage. - Or am I misinterpreting here? 

So: It is always on, moves the mouse between 7am and 6pm and does nothing after 6pm while starting to move the mouse again as of 7am. 

Link to comment
Share on other sites

1 hour ago, Technohead said:

Would love to, but it is still not working - although I adjusted as proposed above. 

(This refers to the error that the include WinAPISys.au3 cannot be found)

Which version of AutoIt are you currently using ? Please run this small script and report the result of the MsgBox.  

MsgBox(BitOR(4096, 64), "Check :", "You are running AutoIt Version " & @AutoItVersion & @CRLF)

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

I guess a stone-old version - but running stable since 2 years on different machines: 3.3.14.5

Further I also solved one of my questions above: I changed from <= to < and now the upper hour limit is no longer included. So as shown below now fulfills my needs. 

 

Still: Perfect would be if I could time it to hh:mm instead of only full hours. So if there is a simple tweak - please shout. Otherwise I consider my request solved - and would like to thank all of you for your input, efforts and especially patience. 

image.png.c4eff4954d384676af7727ce51e14ace.png

Link to comment
Share on other sites

4 hours ago, Technohead said:

@ad777: Thanks: One question remaining: If I understand your macro, it runs if it is within specified time frame but stops afterwards. What I would like it to do is to not end the program after time is over but to stay inactive, until it is back into the "active" time frage. - Or am I misinterpreting here? 

So: It is always on, moves the mouse between 7am and 6pm and does nothing after 6pm while starting to move the mouse again as of 7am. 

just use [While] and if you want to full time including am/pm use:

#include <Date.au3>
$from = "12:01:00 AM"
$to =   "7:14:08 PM"
While 1
_Time($from,$to,100)
WEnd
Func _Time($from,$to,$sleep)
    Local $tohourmin = _NowTime()
    Local $fromhourmin = _NowTime()
    if  $fromhourmin >= $from And $tohourmin <=  $to Then;[>] -> bigger then , [<] -> less ,[=] -> equal to then
        Sleep($sleep)
        MouseMove(1,1)
    EndIf
EndFunc

 

Edited by ad777

iam ِAutoit programmer.

best thing in life is to use your Brain to

Achieve

everything you want.

Link to comment
Share on other sites

Hi @Technohead,

I changed my proposal to fulfill your new requirement to enter the time like HH:MM.
Maybe it seems to be a bit of an overhead, but the function names should be clarify what's going on 😅 , you will see.

The tricky part is the transition from one day to the next day. In the short and nice example of @ad777 you have to define "AM" or "PM" if I am not wrong.
In case you don't want to or can not do this, you could use this:

; init -------------------------------------------------------------------------
Opt('MustDeclareVars', 1)
HotKeySet('{ESC}', '_Exit')



; processing -------------------------------------------------------------------
While True
    If _IsCurrentTimeBetween('22:08', '05:50') Then
        MouseMove(1, 1)
        Sleep(1000)
        MouseMove(10, 10)
    EndIf

    Sleep(5000)
WEnd



; functions --------------------------------------------------------------------
Func _Exit()
   Exit
EndFunc

Func _IsCurrentTimeBetween($sTimeFrom, $sTimeTo)
    Local $iCurrentTime = _GetCurrentTimeHHMM()
    Local $iTimeFrom    = _ExtractHourFromHHMM($sTimeFrom) & _ExtractMinFromHHMM($sTimeFrom)
    Local $iTimeTo      = _ExtractHourFromHHMM($sTimeTo)   & _ExtractMinFromHHMM($sTimeTo)

    $iTimeTo = _HandleNextDay($iTimeFrom, $iTimeTo)

    If $iCurrentTime >= $iTimeFrom And $iCurrentTime <= $iTimeTo Then
        Return True
    EndIf

    Return False
EndFunc

Func _GetCurrentTimeHHMM()
    Return @HOUR & @MIN
EndFunc

Func _ExtractHourFromHHMM($sTime, $sDelimiter = ':')
    Return StringSplit($sTime, $sDelimiter)[1]
EndFunc

Func _ExtractMinFromHHMM($sTime, $sDelimiter = ':')
    Return StringSplit($sTime, $sDelimiter)[2]
EndFunc

Func _HandleNextDay($iTimeFrom, $iTimeTo)
    Local Const $iOneDay = 2400

    Return ($iTimeFrom <= $iTimeTo) ? $iTimeTo : $iTimeTo + $iOneDay
EndFunc

Please let us know if this fits you requirements, thanks 😀 .

Best regards
Sven

________________
Stay innovative!

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

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...