AutoHotkey Community

It is currently May 27th, 2012, 12:57 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: January 29th, 2006, 12:34 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
I have written a small scheduler. In this case as a reminder for my training sessions. But the scheduler can of cause be applied to any task. So please take this as an example. The beauty is, that there is very little impact on CPU load, since there is no loop that is performed. Only one timer every 12 hours.
But this limits the scheduler to tasks in the afternoon. If you want to have tasks in the morning, but you want to have the PC running (no new login), you have to set the timer to an interval of the earliest taskl hour (e.g if you want to have a task scheduled at 8am set the timer to e.g. 7 hours.) If you login every morning, it doesn't matter.

Code:
;#   Notification of Training Sessions
;#
;#   OS: Windows XP
;#   AHK version: 1.0.40.08   (http://www.autohotkey.com/download/)
;#   Language: English
;#   Date: 2006-01-11

#SingleInstance force
GoSub, ReadIni
SetWorkingDir, %A_ScriptDir%

;location of icon file
If ( A_OSType = "WIN32_WINDOWS" )  ; Windows 9x
    IconFile = %A_WinDir%\system\shell32.dll
else
    IconFile = %A_WinDir%\system32\shell32.dll

;create traybar menu
Menu, Tray, Icon, %IconFile%, 44   ;icon for taskbar and for proces in task manager
Menu, Tray, NoStandard
Menu, Tray, Tip, %Gui1WinTitle%
Menu, Tray, Add, Options, BuildGui1
Menu, Tray, Default, Options
Menu, Tray, Add, Stop, Exit
Menu, Tray, Click, 1

;start timer
GoSub, SetupTimer

;Start Gui if link in autostart folder doesn't exist
RegRead, StartUp, HKCU, Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders, StartUp
IfNotExist, %StartUp%\%A_ScriptName%.lnk
    GoSub, BuildGui1
Return

;##############################################################################
;#############   End of AutoExecution-Section   ###############################
;##############################################################################

;##############################################################################
;#############   Actions of GUI 1   ###########################################
;##############################################################################
BuildGui1:
  Gui, 1: +ToolWindow
  Gui, 1:Add, Text, , This program will help you to get to training on time.
  Gui, 1:Add, Text,Section , How many minutes does it take`nyou from work to the gym?
  Gui, 1:Add, Edit, ys r1 w30 Number Limit3 vTravelTime, %TravelTime%
 
  Gui, 1:Add, Text,xm yp+40 , I want to train on     - for weightlifting  - for running

  Gui, 1:Add, Checkbox,xm Section Checked%ChkMon% vChkMon, Monday
  Gui, 1:Add, Radio, x+35 ys Checked%RadMonK% vRadMonK, um %TimeMonK%
  Gui, 1:Add, Radio, x+18 ys Checked%RadMonS% vRadMonS,  um %TimeMonS%
 
  Gui, 1:Add, Checkbox,xm Section Checked%ChkMit%  vChkMit, Wednesday
  Gui, 1:Add, Radio, x+16 ys Checked%RadMitK% vRadMitK,  um %TimeMitK%
  Gui, 1:Add, Radio, x+18 ys Checked%RadMitS% vRadMitS,  um %TimeMitS%

  Gui, 1:Add, Checkbox,xm Section Checked%ChkFre%  vChkFre, Friday
  Gui, 1:Add, Radio, x+45 ys Checked%RadFreK% vRadFreK, um %TimeFreK%
  Gui, 1:Add, Radio, x+18 ys Checked%RadFreS% vRadFreS,  um %TimeFreS%

  Gui, 1:Add, Button, yp+30 xm+20 Section gBtnOK , &OK- Remind me
  Gui, 1:Add, Button, ys gExit , &Stop this program
 
  IniRead, GuiSize1, %A_ScriptName%.ini, Settings, GuiSize1, %A_Space%
  Gui, 1:Show, %GuiSize1%, %Gui1WinTitle%
Return

;#############   Read Settings from IniFile   #################################
ReadIni:
  IniRead, Gui1WinTitle, %A_ScriptName%.ini, Settings, Gui1WinTitle, Reminder for training 1.0
  IniRead, TimeMonK,   %A_ScriptName%.ini, Settings, TimeMonK,   18:45
  IniRead, TimeMonS,   %A_ScriptName%.ini, Settings, TimeMonS,   19:30
  IniRead, TimeMitK,   %A_ScriptName%.ini, Settings, TimeMitK,   18:15
  IniRead, TimeMitS,   %A_ScriptName%.ini, Settings, TimeMitS,   19:00
  IniRead, TimeFreK,   %A_ScriptName%.ini, Settings, TimeFreK,   17:15
  IniRead, TimeFreS,   %A_ScriptName%.ini, Settings, TimeFreS,   18:00
  IniRead, TravelTime, %A_ScriptName%.ini, Settings, TravelTime, 45
  IniRead, ChkMon,     %A_ScriptName%.ini, Settings, ChkMon,     1
  IniRead, ChkMit,     %A_ScriptName%.ini, Settings, ChkMit,     1
  IniRead, ChkFre,     %A_ScriptName%.ini, Settings, ChkFre,     1
  IniRead, RadMonK,    %A_ScriptName%.ini, Settings, RadMonK,    1
  IniRead, RadMonS,    %A_ScriptName%.ini, Settings, RadMonS,    0
  IniRead, RadMitK,    %A_ScriptName%.ini, Settings, RadMitK,    1
  IniRead, RadMitS,    %A_ScriptName%.ini, Settings, RadMitS,    0
  IniRead, RadFreK,    %A_ScriptName%.ini, Settings, RadFreK,    1
  IniRead, RadFreS,    %A_ScriptName%.ini, Settings, RadFreS,    0
Return

;#############   Write settings into IniFile   ################################
WriteIni:
  IniWrite, %Gui1WinTitle%, %A_ScriptName%.ini, Settings, Gui1WinTitle
  IniWrite, %TimeMonK%, %A_ScriptName%.ini, Settings, TimeMonK
  IniWrite, %TimeMonS%, %A_ScriptName%.ini, Settings, TimeMonS
  IniWrite, %TimeMitK%, %A_ScriptName%.ini, Settings, TimeMitK
  IniWrite, %TimeMitS%, %A_ScriptName%.ini, Settings, TimeMitS
  IniWrite, %TimeFreK%, %A_ScriptName%.ini, Settings, TimeFreK
  IniWrite, %TimeFreS%, %A_ScriptName%.ini, Settings, TimeFreS
  IniWrite, %TravelTime%, %A_ScriptName%.ini, Settings, TravelTime
  IniWrite, %ChkMon%,   %A_ScriptName%.ini, Settings, ChkMon
  IniWrite, %ChkMit%,   %A_ScriptName%.ini, Settings, ChkMit
  IniWrite, %ChkFre%,   %A_ScriptName%.ini, Settings, ChkFre
  IniWrite, %RadMonK%,  %A_ScriptName%.ini, Settings, RadMonK
  IniWrite, %RadMonS%,  %A_ScriptName%.ini, Settings, RadMonS
  IniWrite, %RadMitK%,  %A_ScriptName%.ini, Settings, RadMitK
  IniWrite, %RadMitS%,  %A_ScriptName%.ini, Settings, RadMitS
  IniWrite, %RadFreK%,  %A_ScriptName%.ini, Settings, RadFreK
  IniWrite, %RadFreS%,  %A_ScriptName%.ini, Settings, RadFreS
Return

;#############   User wants to be informed   ##################################
BtnOK:
  ;get data from GUI and destroy it
  Gui, 1:Submit
  Gui, 1:Destroy
 
  ;write data to ini file
  GoSub, WriteIni
 
  ;Start Timer
  GoSub, SetupTimer
 
  ;Create shortcut in autostart folder, so it gets activated at next login
  RegRead, StartUp, HKCU, Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders, StartUp
  IfNotExist, %StartUp%\%A_ScriptName%.lnk
    {   
      FileCreateShortcut, %A_ScriptDir%\%A_ScriptName%                       ;Target
                        , %StartUp%\%A_ScriptName%.lnk                       ;LinkFile
                        , %A_ScriptDir%                                      ;WorkingDir
                        ,                                                    ;Args
                        , %Gui1WinTitle%                                     ;Description
                        , %IconFile%                                         ;IconFile
                        ,                                                    ;ShortCutKey
                        , 44                                                 ;IconNumber
                        ,                                                    ;RunState
      If ErrorLevel
          MsgBox, 16, Install %Gui1WinTitle%,
            (LTrim
              There was a problem to create a shortcut:
              %StartUp%\%A_ScriptName%.lnk
               
              Please create shortcut yourself to the following file:
              %A_ScriptDir%\%A_ScriptName%
            )
    }
Return

;#############   Start Timer to check every 12 hours   ########################
SetupTimer:
  ;check every 12 hours
  12Hours := 12 * 60 * 60 * 1000
  SetTimer, Check12Hour, %12Hours%
 
  ;and check now
  GoSub, Check12Hour
Return

;#############   Check if time is right for notification   ####################
Check12Hour:
  ;turn off Notification
  SetTimer, Notify, Off

  ;check if today is Monday, Wednesday or Friday and If user wants to train
  If ( A_WDay = 2 AND ChkMon)
    {
      ;get time without the ":"
      If RadMonK
          StartTime := TimeMonk
      Else
          StartTime := TimeMonS
      Menu, Tray, Tip, %Gui1WinTitle%`nToday is Monday`mTraining at %StartTime%
      StringReplace, StartTime, StartTime, :
      ;set notification
      SetTimer, Notify, % GetTime(StartTime, TravelTime)
    }
  If ( A_WDay = 4 AND ChkMit)
    {
      If RadMitK
          StartTime := TimeMitk
      Else
          StartTime := TimeMitS
      Menu, Tray, Tip, %Gui1WinTitle%`nToday is Wednesday`mTraining at %StartTime%
      StringReplace, StartTime, StartTime, :
      SetTimer, Notify, % GetTime(StartTime, TravelTime)
    }
  If ( A_WDay = 6 AND ChkFre)
    {
      If RadFreK
          StartTime := TimeFreK
      Else
          StartTime := TimeFreS
      Menu, Tray, Tip, %Gui1WinTitle%`nToday is Friday`mTraining at %StartTime%
      SetTimer, Notify, % GetTime(StartTime, TravelTime)
    }
Return

;#############   Calculate time till notification   ###########################
GetTime(StartTime, TravelTime)
  {
    ;create YYYYMMDDHHMMSS format for StartTime
    StartTime = %A_YYYY%%A_MM%%A_DD%%StartTime%
    ;add TravelTime to A_NOW
    Time2 += %TravelTime%, Minutes
    ;get time difference in seconds
    StartTime -= %Time2%, Seconds
    ;if program is started inside the TravelTime, inform immediatly
    If StartTime < 1
      StartTime = 1
    ;return time till notification in milliseconds
    Return, StartTime*1000
  }

;#############   notify user   ################################################
Notify:
  ;Turn off notification
  SetTimer, Notify, Off
 
  ;notify
  MsgBox, 4147, %Gui1WinTitle%,
    (LTrim
      It's time for you to get to the gym.
      It is %A_Hour%:%A_Min%
     
      Depending on how you answer, I leave you in peace.
      With YES, I leave you in peace. But expect you to go to the training.
      With No, I remind you again in 5 Minutes.
      With Cancel, I leave you in peace, but please don't make this a habit.
    )
  ;if user press NO, inform him again in 5 minutes
  IfMsgBox, No
      SetTimer, Notify, 300000
Return

;#############   Close the GUI   ##############################################
GuiEscape:
GuiClose:
  ;remember GUI position
  WinGetPos, PosX, PosY, SizeW, SizeH, %WinNameGui1%
  IniWrite, x%PosX% y%PosY%, %A_ScriptName%.ini, Settings, GuiSize1
Return

;#############   Exit the script   ############################################
Exit:
  ;remove the shortcut in the autostart folder
  RegRead, StartUp, HKCU, Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders, StartUp
  FileDelete, %StartUp%\%A_ScriptName%.lnk
 
  ;exit
  ExitApp
Return

;##############################################################################
;#############   End of File   ################################################
;##############################################################################

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject: very useful!
PostPosted: February 6th, 2006, 11:39 pm 
thank toralf
very useful!!!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: April 12th, 2009, 1:22 pm 
nice script.
i think i can change that for my need.

but there's one thing left to translate into english:
it says "um" instead of "at" in the config window.

(I'm from germany, too. ^^)
bye


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bon, sks and 23 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group