Jump to content

Run a function at a specific time of the day?


Hyflex
 Share

Recommended Posts

Hey,

How do I make it run the "DC" function at a certain time of the day (06:00am)

Also Is it possible to improve the following at all, I am sure you guys can see stuff I'm doing wrong :S

; Misc Variables
Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1)
Opt("TrayAutoPause", 0)
TraySetClick(8)

; Tray Items
$Tray_DC = TrayCreateItem("Run DC")
TrayItemSetOnEvent(-1, "DC")
$Tray_Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "Terminate")

While 1
Sleep(100)
WEnd

Func DC()

If NOT ProcessExists("ri.exe") Then
Run(@ProgramFilesDir & "\R500\ri.exe", "")
EndIf

WinActivate ("R4")
WinWaitActive("R4")
Sleep(250)

; Search for Red Logo in top left then continue
While PixelSearch(2, 4, 4, 6, 0xFF0000, 2)
Sleep(250)
WEnd
Sleep(250)

; Left Click on New Option
MouseClick("left", 540, 35, 1, 1)
Sleep(250)

; Search for Logo in middle of screen
While PixelSearch(199, 202, 201, 204, 0xFF0000, 2)
Sleep(250)
WEnd
Sleep(250)

; Wait for New Window
WinWait("R Option")
WinActivate ("R Option")
WinWaitActive("R Option")
Sleep(250)

; Click Full
ControlClick("R - Option", "", "[CLASS:ThunderRT6OptionButton; INSTANCE:2]")
Sleep(250)

; Search for Yes then continue
While PixelSearch(397, 488, 400, 490, 0x00FF00, 8)
Sleep(100)
WEnd
Sleep(250)

; Search for Exit then continue
While PixelSearch(624, 479, 631, 486, 0xFF0000, 5)
Sleep(250)
WEnd
Sleep(250)

; Click on Yes
ControlClick("R - Option", "", "[CLASS:ThunderRT6CommandButton; INSTANCE:2]")
Sleep(250)

; Wait for New Window
WinWait("OPTION") 
WinActivate ("OPTION")
WinWaitActive("OPTION")
Sleep(250)

; Click on OK
ControlClick("OPTION", "", "[CLASS:Button; INSTANCE:1]")
Sleep(250)

; Wait for New Window
WinWait("R4") 
WinActivate ("R4")
WinWaitActive("R4")
Sleep(250)

; Search for Red Logo in top left then continue
While PixelSearch(2, 4, 4, 6, 0xFF0000, 2)
Sleep(250)
WEnd
Sleep(250)

; Left Click on Update
MouseClick("left", 279, 35, 1, 1)
Sleep(250)

; Search for World then continue
While PixelSearch(175, 100, 185, 115, 0x00FF00, 2)
Sleep(250)
WEnd
Sleep(250)

; Search for World 2 (if it hangs then there is no internet)
While PixelSearch(513, 225, 543, 250, 0x00FF00, 2)
Sleep(250)
WEnd
Sleep(250)

; Click on Update to Latest
ControlClick("R - Update Manager", "", "[CLASS:ThunderRT6OptionButton; INSTANCE:6]")
Sleep(250)

; Click on FullO
ControlClick("R - Update Manager", "", "[CLASS:ThunderRT6CheckBox; INSTANCE:6]")
Sleep(250)

; Click on Connect
ControlClick("R - Update Manager", "", "[CLASS:ThunderRT6CommandButton; INSTANCE:5]")
Sleep(250)
EndFunc

; Exit 
Func Terminate()
   Exit 0
EndFunc
Link to comment
Share on other sites

I imagine you'd either need to have the script already running in the background, have another script call it, or set up an OS event to run it.

edit: Have the script running, that is. Use time macros, like @HOUR, to get the hour.

http://www.autoitscript.com/autoit3/docs/macros/TimeAndDate.htm

Edited by Aru
Link to comment
Share on other sites

Aru,

I have used the following code to run something at a specific time of day (can be scheduled days in advance), however, Tasks Schd night be a better alternative, as RichardL suggests.

;
; Run Something at specific time of day
;
#include<date.au3>
HotKeySet("{ESC}", "_Fini")
global $runtime = ''
global $error     = ''
global $default_run_time  = '05:00'
global $aTmpT[3]
global $job  = 'd:sdexesd0000.exe'
get_time()
while 1
local $tmp = stringregexp($runtime,'d{2}:d{2}',3)
if @error = 0 then exitloop
$runtime = ''
$error = '     >>> time invalid <<<'
get_time()
WEnd
while 1
if stringleft($runtime,2) > '23' or stringright($runtime,2) > '59' then
  $runtime = ''
  $error = '       >>> HH or MM invalid <<<'
  get_time()
else
  ExitLoop
endif
wend
while 1
get_sleep_time()
traytip('Jobber',$job & ' is scheduled to run at ' & $runtime & ' on ' &_dateadd('s',$aTmpt[2],_NowCalcDate()),30)
sleep($aTmpT[2]*1000)
shellexecute($job)
sleep(1000)
wend
func get_time()
while not $runtime
  $runtime = inputbox('Once a Day Jobber','   *** Enter the time to run ***' & @lf & $error & _
  @lf & '             (HH:MM format)',$default_run_time,"",100,150,default,default,15)
  if @error = 1 then exit
  if @error = 2 then
   $runtime = $default_run_time
   exitloop
  endif
WEnd
endfunc
func get_sleep_time()
$aTmpT[0] = _nowcalcdate() & ' ' & $runtime & ":00"
$aTmpT[1] = _NowCalc()
$aTmpT[2] = _datediff('s',$aTmpT[1],$aTmpT[0])
if $aTmpT[2] < 0 then $aTmpT[2] = $aTmpt[2]+(24*60*60)
endfunc
func _Fini()
Exit
endfunc

Sorry for the appearence of the code...the AutoIT code insertion tool is hanging...

kylomas

Edit: spelling

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Thanks, In the While loop I made it do the following:

If @HOUR = $MHour Then
    If @Min = $MMinutes Then
        If @SEC = $MSeconds Then
            MDC()
        EndIf
    EndIf
EndIf

Works perfectly good as the macro will only run once as the main macro takes about 8 mins to run

Edited by XxXGoD
Link to comment
Share on other sites

An 'event' is the condition that triggers tasks in the task scheduler/computer manager.

Also, xxx, you can use ANDs to clean up your nested ifs if you want, like this.

If @HOUR = $MHour And @Min = $MMinutes And @SEC = $MSeconds Then
  MDC()
EndIf

I can't see what else is in your polling loop, but you might want to put a sleep() in there if it isn't. Even if it's just 1ms, it saves a lot of cpu time.

Edited by Aru
Link to comment
Share on other sites

  • 7 years later...

@kylomasI know this is getting old, but I like your scheduler script.  In case you read this, do you have a working version of it?  I think there might be some issues, maybe typos, with the posted version.

Many thanks.

Edited by jmor
Typos
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...