home *** CD-ROM | disk | FTP | other *** search
/ PC World 1999 October / PCWorld_1999-10_cd2.bin / Corel / Scripts / schedule.csc < prev    next >
Text File  |  1999-03-12  |  41KB  |  1,248 lines

  1. REM Runs programs at specified intervals
  2. REM Schedule.csc  April 26, 1996
  3. REM Copyright 1996 Corel Corporation. All rights reserved.
  4.  
  5. REM ***************************************************************
  6. REM * Global Data                                                 *
  7. REM ***************************************************************
  8.  
  9. 'Standard constants
  10. #include "ScpConst.csi"
  11. DIM DATFOL$
  12. DATFOL$=GetCurrFolder()
  13.  
  14. 'Maximum number of events
  15. #define MAX_EVENTS 100
  16.  
  17. 'Global variables
  18. GLOBAL Days$(7)
  19. GLOBAL Months$(12)
  20. GLOBAL CurrentYear$ AS STRING
  21. GLOBAL CurrentMonth$
  22. GLOBAL SelectedYear%
  23. GLOBAL SelectedMonthIndex%
  24. GLOBAL EventData(MAX_EVENTS, 6) AS VARIANT
  25. GLOBAL ScheduledEvent$(MAX_EVENTS)
  26. GLOBAL NumberOfEvents%
  27. GLOBAL CurrentDay%
  28.  
  29.  
  30. 'Date formats
  31. GLOBAL CONST DayNumeric$ = "dd"
  32. GLOBAL CONST DayFormat$ = "dddd"
  33. GLOBAL CONST MonthFormat$ = "MMMM"
  34. GLOBAL CONST MonthNumeric$ = "MM"
  35. GLOBAL CONST YearFormat$ = "yyyy"
  36.  
  37. REM ***************************************************************
  38. REM * Main Dialog                                                 *
  39. REM ***************************************************************
  40. BEGIN DIALOG OBJECT MainDialog 447, 142, "Corel SCRIPT Scheduler", SUB MainDialogSub
  41.     LISTBOX  6, 7, 432, 111, .MainList
  42.     PUSHBUTTON  11, 124, 65, 14, .AddEvent, "Add Event"
  43.     PUSHBUTTON  99, 124, 65, 14, .RemoveEvent, "Remove Event"
  44.     PUSHBUTTON  188, 124, 65, 14, .EditEvent, "Edit Event"
  45.     PUSHBUTTON  276, 124, 65, 14, .ExecuteEvent, "Execute Event"
  46.     PUSHBUTTON  365, 124, 65, 14, .ExitButton, "Exit"
  47. END DIALOG
  48.  
  49. REM ***************************************************************
  50. REM * Event scheduling Dialog                                     *
  51. REM ***************************************************************
  52. BEGIN DIALOG OBJECT ScheduleEvent 264, 276, "Schedule Event", SUB ScheduleEventSub
  53.     OPTIONGROUP .ScheduleOption
  54.         OPTIONBUTTON  10, 47, 62, 10, .ScheduleOnce, "Schedule Once"
  55.         OPTIONBUTTON  10, 119, 66, 11, .ScheduleHourly, "Schedule Hourly"
  56.         OPTIONBUTTON  10, 163, 62, 10, .ScheduleDaily, "Schedule Daily"
  57.         OPTIONBUTTON  10, 207, 69, 10, .ScheduleMonthly, "Schedule Monthly"
  58.     GROUPBOX  6, 0, 250, 38, .ProgramGroup, "Program"
  59.     TEXTBOX  20, 13, 180, 13, .ProgramFile
  60.     PUSHBUTTON  203, 13, 46, 14, .BrowseButton, "Browse"
  61.     GROUPBOX  5, 38, 165, 72, .OnceGroup
  62.     TEXT  10, 62, 13, 8, .OnceON, "On"
  63.     DDLISTBOX  25, 61, 47, 77, .OnceMonth
  64.     SPINCONTROL  122, 61, 44, 12, .OnceYear
  65.     TEXT  109, 65, 13, 8, .OnceTH, "th,"
  66.     SPINCONTROL  74, 61, 34, 12, .OnceDay
  67.     TEXT  15, 90, 10, 8, .OnceAT, "At"
  68.     SPINCONTROL  25, 88, 23, 12, .OnceHour
  69.     TEXT  49, 89, 5, 9, .OnceCOLON, ":"
  70.     SPINCONTROL  55, 88, 23, 12, .OnceMinutes
  71.     OPTIONGROUP .OnceOption
  72.         OPTIONBUTTON  89, 82, 28, 10, .OnceAM, "AM"
  73.         OPTIONBUTTON  89, 95, 28, 10, .OncePM, "PM"
  74.     GROUPBOX  5, 110, 165, 42, .HourlyGroup
  75.     TEXT  19, 137, 9, 8, .HourlyAT, "At"
  76.     SPINCONTROL  31, 135, 23, 12, .HourlyMinutes
  77.     TEXT  55, 137, 72, 8, .HourlyAFTER, "Minutes after the Hour"
  78.     GROUPBOX  5, 152, 165, 46, .DailyGroup
  79.     TEXT  14, 177, 10, 8, .DailyAT, "At"
  80.     SPINCONTROL  25, 175, 23, 12, .DailyHour
  81.     TEXT  48, 175, 5, 9, .DailyCOLON, ":"
  82.     SPINCONTROL  54, 175, 23, 12, .DailyMinutes
  83.     OPTIONGROUP .DailyOption
  84.         OPTIONBUTTON  86, 169, 28, 10, .DailyAM, "AM"
  85.         OPTIONBUTTON  86, 181, 28, 10, .DailyPM, "PM"
  86.     GROUPBOX  5, 198, 165, 71, .MonthlyGroup
  87.     TEXT  11, 223, 19, 8, .MonthlyEVERY, "Every"
  88.     SPINCONTROL  32, 221, 31, 12, .MonthlyDay
  89.     TEXT  65, 223, 10, 8, .MonthlyTH, "th"
  90.     TEXT  75, 223, 50, 8, .MonthlyOF, "of the month"
  91.     TEXT  14, 250, 10, 8, .MonthlyAT, "At"
  92.     SPINCONTROL  25, 248, 23, 12, .MonthlyHour
  93.     TEXT  48, 248, 5, 9, .MonthlyCOLON, ":"
  94.     SPINCONTROL  54, 248, 23, 12, .MonthlyMinutes
  95.     OPTIONGROUP .MonthlyOption
  96.         OPTIONBUTTON  86, 242, 28, 10, .MonthlyAM, "AM"
  97.         OPTIONBUTTON  86, 254, 28, 10, .MonthlyPM, "PM"
  98.     GROUPBOX  174, 38, 82, 98, .DaysGroup, "Execute only on days"
  99.     CHECKBOX  182, 51, 64, 9, .MondayBox, "Monday"
  100.     CHECKBOX  182, 62, 64, 9, .TuesdayBox, "Tuesday"
  101.     CHECKBOX  182, 74, 64, 9, .WednesdayBox, "Wednesday"
  102.     CHECKBOX  182, 86, 64, 9, .ThursdayBox, "Thursday"
  103.     CHECKBOX  182, 97, 64, 9, .FridayBox, "Friday"
  104.     CHECKBOX  182, 109, 64, 9, .SaturdayBox, "Saturday"
  105.     CHECKBOX  182, 121, 64, 9, .SundayBox, "Sunday"
  106.     OKBUTTON  176, 232, 77, 14, .OK1
  107.     CANCELBUTTON  176, 253, 77, 14, .Cancel1
  108. END DIALOG
  109.  
  110. REM ***************************************************************
  111. REM * Initialization: determines the names of the months and      *
  112. REM * days.                                                       *
  113. REM ***************************************************************
  114. SUB Initialize()
  115. DIM StartDate AS DATE
  116.     DIM i AS INTEGER
  117.     DIM x AS DOUBLE
  118.     DIM y AS DOUBLE
  119.  
  120.     StartDate = 29221        'Jan 1 1980
  121.  
  122.     'Initialize 12 months
  123.     i% = 0
  124.     FOR x# = 1 TO 365 STEP 31
  125.         i% = i% + 1
  126.         Months$(i%) = FORMATDATE(StartDate + x#, MonthFormat$)
  127.     NEXT x#
  128.  
  129.     'Initialize 7 days
  130.     FOR y# = 1 TO 7
  131.         Days$(y#) = FORMATDATE(StartDate + y# + 5, DayFormat$) 'January 6th was a Monday
  132.     NEXT y#
  133.     
  134.     'Initialize current date
  135.     CurrentYear$ = FORMATDATE(GETCURRDATE(), YearFormat$)
  136.     SelectedYear% = CINT(CurrentYear$)
  137.  
  138.     CurrentMonth$ = FORMATDATE(GETCURRDATE(), MonthFormat$)
  139.     SelectedMonthIndex% = CINT(FORMATDATE(GETCURRDATE(), MonthNumeric$))
  140.  
  141.     CurrentDay% = FORMATDATE(GETCURRDATE(), DayNumeric$)
  142.  
  143.     'Fill in the Schedule Event Dialog "Day boxes".
  144.     WITH ScheduleEvent
  145.         .MondayBox.SetText Days$(1)
  146.         .TuesdayBox.SetText Days$(2)
  147.         .WednesdayBox.SetText Days$(3)
  148.         .ThursdayBox.SetText Days$(4)
  149.         .FridayBox.SetText Days$(5)
  150.         .SaturdayBox.SetText Days$(6)
  151.         .SundayBox.SetText Days$(7)
  152.     END WITH
  153.  
  154. END SUB
  155.  
  156. REM ***************************************************************
  157. REM * NumeralToOrdinal$: Returns the appropriate ordinal          *
  158. REM * abbreviation ("th", "st", "nd", "rd"                        *
  159. REM ***************************************************************
  160. FUNCTION NumeralToOrdinal$ (Number%)
  161.     IF ((INT(Number% / 10) MOD 10) =  1) THEN
  162.         NumeralToOrdinal$ = "th"
  163.     ELSE
  164.         SELECT CASE (Number% MOD 10)
  165.             CASE 1
  166.                 NumeralToOrdinal$ = "st"
  167.             CASE 2
  168.                 NumeralToOrdinal$ = "nd"
  169.             CASE 3
  170.                 NumeralToOrdinal$ = "rd"
  171.             CASE ELSE
  172.                 NumeralToOrdinal$ = "th"
  173.         END SELECT
  174.     ENDIF
  175. END FUNCTION
  176.  
  177. REM ***************************************************************
  178. REM * MakePrettyString: Transforms Event Data into a string to be *
  179. REM * displayed in the Main Dialog List Box                       *
  180. REM ***************************************************************
  181. SUB MakePrettyString (Index%)
  182.     DIM Event AS STRING
  183.     DIM DaysRunned AS STRING
  184.     DIM X AS LONG
  185.     
  186.     SELECT CASE EventData(Index%,1)
  187.         CASE "O"
  188.             'Schedule once
  189.             Event$ = "On "
  190.             'Date
  191.             Event$ = Event$ & CDAT(CDBL(EventData(Index%, 5)))
  192.             Event$ = Event$ & " at "
  193.             'Time
  194.             beep
  195.             Event$ = Event$ & CDAT(CDBL(EventData(Index%, 6)))
  196.             Event$ = Event$ & ", run "
  197.             Event$ = Event$ & EventData(Index%, 2)
  198.         CASE "H"
  199.             'Schedule hourly
  200.             Event$ = "Every hour"
  201.             IF NOT(EventData(Index%, 4) = 0) THEN
  202.                 Event$ = Event$ & " at "
  203.                 'Number of minutes (ommitted if 0).
  204.                 Event$ = Event$ & EventData(Index%, 5)
  205.                 Event$ = Event$ & " minutes after the hour"
  206.             ENDIF
  207.             Event$ = Event$ & ", run "
  208.             Event$ = Event$ & EventData(Index%, 2)
  209.         CASE "D"
  210.             'Schedule daily
  211.             Event$ = "Every day at "
  212.             'Time
  213.             Event$ = Event$ & CDAT(CDBL(EventData(Index%, 5)))
  214.             Event$ = Event$ & ", run "
  215.             Event$ = Event$ & EventData(Index%, 2)
  216.         CASE "M"
  217.             'Schedule monthly
  218.             Event$ = "Every "
  219.             'Day number
  220.             Event$ = Event$ & EventData(Index%, 5)
  221.             Event$ = Event$ & NumeralToOrdinal(CINT(EventData(Index%, 5)))
  222.             Event$ = Event$ & " of the month, at "
  223.             Event$ = Event$ & CDAT(CDBL(EventData(Index%, 6)))
  224.             Event$ = Event$ & ", run "
  225.             Event$ = Event$ & EventData(Index%, 2)
  226.         CASE ELSE
  227.             'Other (error)
  228.             Event$ = "Run " & EventData(Index%, 2) & " manually"
  229.     END SELECT
  230.     'Determine on which days the event is active
  231.     IF NOT((EventData(Index%, 3)) = "XXXXXXX") THEN
  232.         IF EventData(Index%, 3) = "       " THEN
  233.             'None
  234.             Event$ = Event$ & ", disabled (no active days)"
  235.         ELSE
  236.             'Concatenate the days
  237.             DaysRunned$ = ""
  238.             FOR X = 1 TO 7
  239.                 IF MID(EventData(Index%, 3),X,1) = "X" THEN DaysRunned$ = DaysRunned$ & ", " & Days$(X)
  240.             NEXT X
  241.             'Remove the first comma when adding to the string
  242.             Event$ = Event$ & ", run only on" & MID(DaysRunned$,2)
  243.         ENDIF
  244.     ENDIF
  245.     'Indicate last run
  246.     Event$ = Event$ & ", last run on " & CDAT(CDBL(EventData(Index%,4)))
  247.     'Set the string in the list
  248.     ScheduledEvent$(Index%) = Event$
  249. END SUB                
  250.  
  251. REM ***************************************************************
  252. REM * ResetScheduleEvent: Fills the Schedule dialog with starting *
  253. REM * data and takes care of some dialog initialization           *
  254. REM ***************************************************************
  255. SUB ResetScheduleEvent
  256.     WITH ScheduleEvent
  257.         .ScheduleOption.SetValue 0
  258.         .ProgramFile.SetText ""
  259.         .OnceMonth.SetArray Months$
  260.         .OnceMonth.SetSelect SelectedMonthIndex%
  261.         .OnceDay.SetValue CurrentDay%
  262.         .OnceYear.SetValue SelectedYear%
  263.         .OnceHour.SetValue 12
  264.         .OnceMinutes.SetValue 0
  265.         .OnceOption.SetValue 0
  266.         .HourlyMinutes.SetValue 0
  267.         .DailyHour.SetValue 12
  268.         .DailyMinutes.SetValue 0
  269.         .DailyOption.SetValue 0
  270.         .MonthlyDay.SetValue CurrentDay%
  271.         .MonthlyHour.SetValue 12
  272.         .MonthlyMinutes.SetValue 0
  273.         .MonthlyOption.SetValue 0
  274.         
  275.         .OnceTH.SetText NumeralToOrdinal$(CINT(.OnceDay.GetValue()))&","
  276.         .MonthlyTH.SetText NumeralToOrdinal$(CINT(.MonthlyDay.GetValue()))
  277.         
  278.         .MondayBox.SetThreeState FALSE
  279.         .TuesdayBox.SetThreeState FALSE
  280.         .WednesdayBox.SetThreeState FALSE
  281.         .ThursdayBox.SetThreeState FALSE
  282.         .FridayBox.SetThreeState FALSE
  283.         .SaturdayBox.SetThreeState FALSE
  284.         .SundayBox.SetThreeState FALSE
  285.         .MondayBox.SetValue 1
  286.         .TuesdayBox.SetValue 1
  287.         .WednesdayBox.SetValue 1
  288.         .ThursdayBox.SetValue 1
  289.         .FridayBox.SetValue 1
  290.         .SaturdayBox.SetValue 1
  291.         .SundayBox.SetValue 1
  292.         
  293.         .OnceYear.SetMinRange CINT(CurrentYear$)
  294.         .OnceYear.SetMaxRange 3000
  295.         
  296.         .OnceMinutes.SetIncrement 15
  297.         .HourlyMinutes.SetIncrement 15
  298.         .DailyMinutes.SetIncrement 15
  299.         .MonthlyMinutes.SetIncrement 15
  300.     END WITH
  301. END SUB
  302.  
  303. REM ***************************************************************
  304. REM * FillScheduleEvent: Fills the Schedule Dialog with data      *
  305. REM * obtained from the specified event. The routine relies on    *
  306. REM * initialization performed in ResetScheduleEvent and does not *
  307. REM * give new values to those variables where the inital values  *
  308. REM * are appropriate.                                            *
  309. REM ***************************************************************
  310. SUB FillScheduleEvent (Index%)
  311.     DIM Year AS LONG
  312.     DIM Month AS LONG
  313.     DIM Day AS LONG
  314.     DIM DayWeek AS LONG
  315.     DIM Hours AS LONG
  316.     DIM Minutes AS LONG
  317.     DIM Seconds AS LONG
  318.     
  319.     CALL ResetScheduleEvent
  320.     WITH ScheduleEvent
  321.         SELECT CASE EventData(Index%, 1)
  322.             CASE "O"
  323.                 'Scheduled Once
  324.                 .ScheduleOption.SetValue 0
  325.                 'Date
  326.                 GETDATEINFO CDAT(CDBL(EventData(Index%,5))),Year&, Month&, Day&, DayWeek&
  327.                 .OnceMonth.SetSelect Month&
  328.                 .OnceDay.SetValue Day&
  329.                 .OnceYear.SetValue Year&
  330.                 'Time
  331.                 GETTIMEINFO CDAT(CDBL(EventData(Index%,6))),Hours&, Minutes&, Seconds&
  332.                 IF(Hours& >= 13) THEN
  333.                     Hours& = Hours& - 12
  334.                     .OnceOption.SetValue 1
  335.                 ENDIF
  336.                 IF Hours& = 12 THEN
  337.                     .OnceOption.SetValue 1
  338.                 ENDIF
  339.                 IF Hours& = 0 THEN
  340.                     Hours& = 12
  341.                 ENDIF                
  342.                 .OnceHour.SetValue Hours&
  343.                 .OnceMinutes.SetValue Minutes&
  344.             CASE "H"
  345.                 'Scheduled Hourly
  346.                 .ScheduleOption.SetValue 1
  347.                 'Minutes after Hours
  348.                 .HourlyMinutes.SetValue EventData(Index%,5)
  349.             CASE "D"
  350.                 'Scheduled Daily
  351.                 .ScheduleOption.SetValue 2
  352.                 'Time
  353.                 GETTIMEINFO CDAT(CDBL(EventData(Index%,5))),Hours&, Minutes&, Seconds&
  354.                 IF(Hours& >= 13) THEN
  355.                     Hours& = Hours& - 12
  356.                     .DailyOption.SetValue 1
  357.                 ENDIF
  358.                 IF Hours& = 12 THEN
  359.                     .DailyOption.SetValue 1
  360.                 ENDIF
  361.                 IF Hours& = 0 THEN
  362.                     Hours& = 12
  363.                 ENDIF                
  364.                 .DailyHour.SetValue Hours&
  365.                 .DailyMinutes.SetValue Minutes&
  366.             CASE "M"
  367.                 'Scheduled Monthly
  368.                 .ScheduleOption.SetValue 3
  369.                 'Day of the month
  370.                 .MonthlyDay.SetValue EventData(Index%, 5)
  371.                 'Time
  372.                 GETTIMEINFO CDAT(CDBL(EventData(Index%,6))),Hours&, Minutes&, Seconds&
  373.                 IF(Hours& >= 13) THEN
  374.                     Hours& = Hours& - 12
  375.                     .MonthlyOption.SetValue 1
  376.                 ENDIF
  377.                 IF Hours& = 12 THEN
  378.                     .MonthlyOption.SetValue 1
  379.                 ENDIF
  380.                 IF Hours& = 0 THEN
  381.                     Hours& = 12
  382.                 ENDIF                
  383.                 .MonthlyHour.SetValue Hours&
  384.                 .MonthlyMinutes.SetValue Minutes&
  385.             CASE ELSE
  386.                 'Other; error.
  387.         END SELECT
  388.         
  389.         .OnceTH.SetText NumeralToOrdinal$(CINT(.OnceDay.GetValue())) & ","
  390.         .MonthlyTH.SetText NumeralToOrdinal$(CINT(.MonthlyDay.GetValue()))
  391.         
  392.         'Set the program name
  393.         .ProgramFile.SetText EventData(Index%,2)
  394.         
  395.         'Set the active days, one by one.
  396.         IF MID(EventData(Index%,3),1,1) = " " THEN
  397.             .MondayBox.SetValue 0
  398.         ENDIF
  399.         IF MID(EventData(Index%,3),2,1) = " " THEN
  400.             .TuesdayBox.SetValue 0
  401.         ENDIF
  402.         IF MID(EventData(Index%,3),3,1) = " " THEN
  403.             .WednesdayBox.SetValue 0
  404.         ENDIF
  405.         IF MID(EventData(Index%,3),4,1) = " " THEN
  406.             .ThursdayBox.SetValue 0
  407.         ENDIF
  408.         IF MID(EventData(Index%,3),5,1) = " " THEN
  409.             .FridayBox.SetValue 0
  410.         ENDIF
  411.         IF MID(EventData(Index%,3),6,1) = " " THEN
  412.             .SaturdayBox.SetValue 0
  413.         ENDIF
  414.         IF MID(EventData(Index%,3),7,1) = " " THEN
  415.             .SundayBox.SetValue 0
  416.         ENDIF
  417.     END WITH
  418. END SUB
  419.  
  420. REM ***************************************************************
  421. REM * RemoveFromList: Removes an element from the array, sending  *
  422. REM * all other elements upwards. This procedure is recursive: it *
  423. REM * copies the next element in the current position, then calls *
  424. REM * itself on the next element. The last element is simply      *
  425. REM * deleted.                                                    *
  426. REM ***************************************************************
  427. SUB RemoveFromList(Index%)
  428.     IF Index% = NumberOfEvents% THEN
  429.         'Empty the data
  430.         SETEMPTY EventData(Index%, 1)
  431.         SETEMPTY EventData(Index%, 2)
  432.         SETEMPTY EventData(Index%, 3)
  433.         SETEMPTY EventData(Index%, 4)
  434.         SETEMPTY EventData(Index%, 5)
  435.         SETEMPTY ScheduledEvent$(Index%)
  436.         'Remove the event
  437.         NumberOfEvents% = NumberOfEvents% - 1
  438.         'Exit
  439.         EXIT SUB
  440.     ENDIF
  441.     'Copy the data from the next
  442.     EventData(Index%,1) = EventData(Index% + 1, 1)
  443.     EventData(Index%,2) = EventData(Index% + 1, 2)
  444.     EventData(Index%,3) = EventData(Index% + 1, 3)
  445.     EventData(Index%,4) = EventData(Index% + 1, 4)
  446.     EventData(Index%,5) = EventData(Index% + 1, 5)
  447.     ScheduledEvent$(Index%) = ScheduledEvent$(Index% + 1)
  448.     'Now, delete the newly-copied next element
  449.     CALL RemoveFromList CINT(Index% + 1)
  450. END SUB
  451.  
  452. REM ***************************************************************
  453. REM * ExecuteEvent: This executes the program associated with the *
  454. REM * specified event. After calling this SUB, the caller should  *
  455. REM * update the Dialog list.                                     *
  456. REM ***************************************************************
  457. SUB ExecuteEvent(Index%)
  458.     IF UCASE(RIGHT(EventData(Index%,2),4)) = ".EXE" THEN
  459.         'Executable
  460.         STARTPROCESS EventData(Index%,2)
  461.     ELSE
  462.         'Script (assumed); run through Script editor.
  463.         WITHOBJECT "CorelScript.Automation.9"
  464.             IF (.FileOpen(EventData(Index%,2)) = FALSE) THEN
  465.                 MESSAGE "Error loading" + CHR(13) +  EventData(Index%,2) + "."
  466.             ELSE
  467.                 IF (.Execute() = FALSE) THEN
  468.                     MESSAGE "Error executing" + CHR(13) +  EventData(Index%,2) + "."
  469.                 ENDIF
  470.                 .FileClose
  471.             ENDIF
  472.         END WITHOBJECT
  473.     ENDIF
  474.     'Set last run time to now.
  475.     EventData(Index%,4) = GETCURRDATE()
  476.     CALL MakePrettyString(Index%)
  477. END SUB
  478.  
  479. REM ***************************************************************
  480. REM * RunPrograms: Checks if any programs need running, and runs  *
  481. REM * them if appropriate. The caller should update the entire    *
  482. REM * list after running this function.                           *
  483. REM ***************************************************************
  484. SUB RunPrograms()
  485.     DIM Year AS LONG
  486.     DIM Month AS LONG
  487.     DIM Day AS LONG
  488.     DIM DayWeek AS LONG
  489.     DIM Hours AS LONG
  490.     DIM Minutes AS LONG
  491.     DIM Seconds AS LONG
  492.     DIM Index AS INTEGER
  493.     DIM RunDate AS DATE
  494.     DIM RunDay AS INTEGER
  495.  
  496.     GETTIMEINFO GETCURRDATE(), Hours&, Minutes&, Seconds&
  497.     GETDATEINFO GETCURRDATE(), Year&, Month&, Day&, DayWeek&
  498.     IF DayWeek& = 1 THEN
  499.         'Sunday is seven
  500.         DayWeek& = 7
  501.     ELSE
  502.         'Our index begins at monday
  503.         DayWeek& = DayWeek& - 1
  504.     ENDIF
  505.     FOR Index% = 1 TO NumberOfEvents%
  506.         'Check if active today
  507.         IF (MID(EventData(Index%,3),DayWeek&,1) = "X") THEN
  508.             SELECT CASE EventData(Index%,1)
  509.                 CASE "O"
  510.                     'Scheduled once
  511.                     'Create execution time
  512.                     RunDate = CDAT(CDBL(EventData(Index%,5)) + CDBL(EventData(Index%,6)))
  513.                     'Test for run conditions (later than the condition and not run since)
  514.                     IF (GETCURRDATE() >= Rundate) AND (RunDate > CDAT(CDBL(EventData(Index%, 4)))) THEN
  515.                         CALL ExecuteEvent Index%
  516.                     ENDIF
  517.                 CASE "H"
  518.                     'Scheduled hourly
  519.                     'Create execution time
  520.                     RunDate = BUILDDATE(Year&, Month&, Day&) + BUILDTIME(Hours&, EventData(Index%,5), 0)
  521.                     'Test for run conditions (later than the condition and not run since)
  522.                     IF (GETCURRDATE() >= Rundate) AND (RunDate > CDAT(CDBL(EventData(Index%, 4)))) THEN
  523.                         CALL ExecuteEvent Index%
  524.                     ENDIF
  525.                     'Test for run conditions (later than the condition and not run since)
  526.                     IF (GETCURRDATE() >= Rundate) AND (RunDate > CDAT(CDBL(EventData(Index%, 4)))) THEN
  527.                         CALL ExecuteEvent Index%
  528.                     ENDIF
  529.                 CASE "D"
  530.                     'Scheduled daily
  531.                     'Create execution time
  532.                     RunDate = BUILDDATE(Year&, Month&, Day&) + CDAT(CDBL(EventData(Index%,5)))
  533.                     'Test for run conditions (later than the condition and not run since)
  534.                     IF (GETCURRDATE() >= Rundate) AND (RunDate > CDAT(CDBL(EventData(Index%, 4)))) THEN
  535.                         CALL ExecuteEvent Index%
  536.                     ENDIF
  537.                 CASE "M"
  538.                     RunDay% = EventData(Index%,5)
  539.                     'Check if day exists in this month; if not, then do not run
  540.                     IF (Runday% <= 30) OR (Month& = 1 OR Month& = 3 OR Month& = 5 OR Month& = 7 OR Month& = 8 OR Month& = 10 OR Month& = 12) THEN
  541.                         IF (Runday% <= 29) OR (Month& <> 2) THEN
  542.                             IF (Runday% <= 28) OR (Month& <> 2) OR (((Year& MOD 4) = 0) AND (NOT ((Year& MOD 100) = 0) OR ((Year& MOD 400) = 0))) THEN
  543.                                 'The day exists
  544.                                 'Create execution time
  545.                                 RunDate = BUILDDATE(Year&, Month&, Runday%) + CDAT(CDBL(EventData(Index%,6)))
  546.                                 'Test for run conditions (later than the condition and not run since)
  547.                                 IF (GETCURRDATE() >= Rundate) AND (RunDate > CDAT(CDBL(EventData(Index%, 4)))) THEN
  548.                                     CALL ExecuteEvent Index%
  549.                                 ENDIF
  550.                             ENDIF
  551.                         ENDIF
  552.                     ENDIF                                
  553.                 CASE ELSE
  554.                     'Error; must be run manually
  555.             END SELECT
  556.         ENDIF
  557.     NEXT Index%
  558. END SUB
  559.                 
  560. REM ***************************************************************
  561. REM * EnableOption: This enables the Scheduled Event Dialog       *
  562. REM * controls corresponding to the selected scheduling option.   *
  563. REM ***************************************************************
  564. SUB EnableOption (Option%)
  565.     WITH ScheduleEvent
  566.         SELECT CASE Option%
  567.             CASE 0
  568.                 'Scheduled Once
  569.                 .OnceON.Enable TRUE
  570.                 .OnceMonth.Enable TRUE
  571.                 .OnceYear.Enable TRUE
  572.                 .OnceTH.Enable TRUE
  573.                 .OnceDay.Enable TRUE
  574.                 .OnceAT.Enable TRUE
  575.                 .OnceHour.Enable TRUE
  576.                 .OnceCOLON.Enable TRUE
  577.                 .OnceMinutes.Enable TRUE
  578.                 .OnceAM.Enable TRUE
  579.                 .OncePM.Enable TRUE
  580.                 .HourlyAT.Enable FALSE
  581.                 .HourlyMinutes.Enable FALSE
  582.                 .HourlyAFTER.Enable FALSE
  583.                 .DailyAT.Enable FALSE
  584.                 .DailyHour.Enable FALSE
  585.                 .DailyCOLON.Enable FALSE
  586.                 .DailyMinutes.Enable FALSE
  587.                 .DailyAM.Enable FALSE
  588.                 .DailyPM.Enable FALSE
  589.                 .MonthlyEvery.Enable FALSE
  590.                 .MonthlyDay.Enable FALSE
  591.                 .MonthlyTH.Enable FALSE
  592.                 .MonthlyOF.Enable FALSE
  593.                 .MonthlyAT.Enable FALSE
  594.                 .MonthlyHour.Enable FALSE
  595.                 .MonthlyCOLON.Enable FALSE
  596.                 .MonthlyMinutes.Enable FALSE
  597.                 .MonthlyAM.Enable FALSE
  598.                 .MonthlyPM.Enable FALSE
  599.             CASE 1
  600.                 'Scheduled Hourly
  601.                 .OnceON.Enable FALSE
  602.                 .OnceMonth.Enable FALSE
  603.                 .OnceYear.Enable FALSE
  604.                 .OnceTH.Enable FALSE
  605.                 .OnceDay.Enable FALSE
  606.                 .OnceAT.Enable FALSE
  607.                 .OnceHour.Enable FALSE
  608.                 .OnceCOLON.Enable FALSE
  609.                 .OnceMinutes.Enable FALSE
  610.                 .OnceAM.Enable FALSE
  611.                 .OncePM.Enable FALSE
  612.                 .HourlyAT.Enable TRUE
  613.                 .HourlyMinutes.Enable TRUE
  614.                 .HourlyAFTER.Enable TRUE
  615.                 .DailyAT.Enable FALSE
  616.                 .DailyHour.Enable FALSE
  617.                 .DailyCOLON.Enable FALSE
  618.                 .DailyMinutes.Enable FALSE
  619.                 .DailyAM.Enable FALSE
  620.                 .DailyPM.Enable FALSE
  621.                 .MonthlyEvery.Enable FALSE
  622.                 .MonthlyDay.Enable FALSE
  623.                 .MonthlyTH.Enable FALSE
  624.                 .MonthlyOF.Enable FALSE
  625.                 .MonthlyAT.Enable FALSE
  626.                 .MonthlyHour.Enable FALSE
  627.                 .MonthlyCOLON.Enable FALSE
  628.                 .MonthlyMinutes.Enable FALSE
  629.                 .MonthlyAM.Enable FALSE
  630.                 .MonthlyPM.Enable FALSE
  631.             CASE 2
  632.                 'Scheduled Daily
  633.                 .OnceON.Enable FALSE
  634.                 .OnceMonth.Enable FALSE
  635.                 .OnceYear.Enable FALSE
  636.                 .OnceTH.Enable FALSE
  637.                 .OnceDay.Enable FALSE
  638.                 .OnceAT.Enable FALSE
  639.                 .OnceHour.Enable FALSE
  640.                 .OnceCOLON.Enable FALSE
  641.                 .OnceMinutes.Enable FALSE
  642.                 .OnceAM.Enable FALSE
  643.                 .OncePM.Enable FALSE
  644.                 .HourlyAT.Enable FALSE
  645.                 .HourlyMinutes.Enable FALSE
  646.                 .HourlyAFTER.Enable FALSE
  647.                 .DailyAT.Enable TRUE
  648.                 .DailyHour.Enable TRUE
  649.                 .DailyCOLON.Enable TRUE
  650.                 .DailyMinutes.Enable TRUE
  651.                 .DailyAM.Enable TRUE
  652.                 .DailyPM.Enable TRUE
  653.                 .MonthlyEvery.Enable FALSE
  654.                 .MonthlyDay.Enable FALSE
  655.                 .MonthlyTH.Enable FALSE
  656.                 .MonthlyOF.Enable FALSE
  657.                 .MonthlyAT.Enable FALSE
  658.                 .MonthlyHour.Enable FALSE
  659.                 .MonthlyCOLON.Enable FALSE
  660.                 .MonthlyMinutes.Enable FALSE
  661.                 .MonthlyAM.Enable FALSE
  662.                 .MonthlyPM.Enable FALSE
  663.             CASE 3
  664.                 'Scheduled Monthly
  665.                 .OnceON.Enable FALSE
  666.                 .OnceMonth.Enable FALSE
  667.                 .OnceYear.Enable FALSE
  668.                 .OnceTH.Enable FALSE
  669.                 .OnceDay.Enable FALSE
  670.                 .OnceAT.Enable FALSE
  671.                 .OnceHour.Enable FALSE
  672.                 .OnceCOLON.Enable FALSE
  673.                 .OnceMinutes.Enable FALSE
  674.                 .OnceAM.Enable FALSE
  675.                 .OncePM.Enable FALSE
  676.                 .HourlyAT.Enable FALSE
  677.                 .HourlyMinutes.Enable FALSE
  678.                 .HourlyAFTER.Enable FALSE
  679.                 .DailyAT.Enable FALSE
  680.                 .DailyHour.Enable FALSE
  681.                 .DailyCOLON.Enable FALSE
  682.                 .DailyMinutes.Enable FALSE
  683.                 .DailyAM.Enable FALSE
  684.                 .DailyPM.Enable FALSE
  685.                 .MonthlyEvery.Enable TRUE
  686.                 .MonthlyDay.Enable TRUE
  687.                 .MonthlyTH.Enable TRUE
  688.                 .MonthlyOF.Enable TRUE
  689.                 .MonthlyAT.Enable TRUE
  690.                 .MonthlyHour.Enable TRUE
  691.                 .MonthlyCOLON.Enable TRUE
  692.                 .MonthlyMinutes.Enable TRUE
  693.                 .MonthlyAM.Enable TRUE
  694.                 .MonthlyPM.Enable TRUE
  695.             CASE ELSE
  696.                 'Other (Disable all)
  697.                 .OnceON.Enable FALSE
  698.                 .OnceMonth.Enable FALSE
  699.                 .OnceYear.Enable FALSE
  700.                 .OnceTH.Enable FALSE
  701.                 .OnceDay.Enable FALSE
  702.                 .OnceAT.Enable FALSE
  703.                 .OnceHour.Enable FALSE
  704.                 .OnceCOLON.Enable FALSE
  705.                 .OnceMinutes.Enable FALSE
  706.                 .OnceAM.Enable FALSE
  707.                 .OncePM.Enable FALSE
  708.                 .HourlyAT.Enable FALSE
  709.                 .HourlyMinutes.Enable FALSE
  710.                 .HourlyAFTER.Enable FALSE
  711.                 .DailyAT.Enable FALSE
  712.                 .DailyHour.Enable FALSE
  713.                 .DailyCOLON.Enable FALSE
  714.                 .DailyMinutes.Enable FALSE
  715.                 .DailyAM.Enable FALSE
  716.                 .DailyPM.Enable FALSE
  717.                 .MonthlyEvery.Enable FALSE
  718.                 .MonthlyDay.Enable FALSE
  719.                 .MonthlyTH.Enable FALSE
  720.                 .MonthlyOF.Enable FALSE
  721.                 .MonthlyAT.Enable FALSE
  722.                 .MonthlyHour.Enable FALSE
  723.                 .MonthlyCOLON.Enable FALSE
  724.                 .MonthlyMinutes.Enable FALSE
  725.                 .MonthlyAM.Enable FALSE
  726.                 .MonthlyPM.Enable FALSE
  727.         END SELECT
  728.     END WITH
  729. END SUB
  730.  
  731. REM ***************************************************************
  732. REM * GetEventFromDialog: This takes the data from the Schedule   *
  733. REM * Event Dialog and puts it in the specified Event Data        *
  734. REM * record.                                                     *
  735. REM ***************************************************************
  736. SUB GetEventFromDialog(Index%)
  737.     DIM DaysToRun AS STRING
  738.  
  739.     WITH ScheduleEvent
  740.         SELECT CASE .ScheduleOption.GetValue()
  741.             CASE 0
  742.                 'Scheduled Once
  743.                 EventData(Index%,1) = "O"
  744.                 'Get Program
  745.                 EventData(Index%,2) = .ProgramFile.GetText()
  746.                 'Get Date
  747.                 EventData(Index%,5) = BUILDDATE(.OnceYear.GetValue(), .OnceMonth.GetSelect(), .OnceDay.GetValue())
  748.                 'Get Time
  749.                 IF (.OnceHour.GetValue() = 12) THEN
  750.                     IF (.OnceOption.GetValue() = 0) THEN
  751.                         EventData(Index%,6) = BUILDTIME(0, .OnceMinutes.GetValue(), 0)
  752.                     ELSE
  753.                         EventData(Index%,6) = BUILDTIME(12, .OnceMinutes.GetValue(), 0)
  754.                     ENDIF
  755.                 ELSE 
  756.                     IF (.OnceOption.GetValue() = 0) THEN
  757.                         EventData(Index%,6) = BUILDTIME(.OnceHour.GetValue(), .OnceMinutes.GetValue(), 0)
  758.                     ELSE
  759.                         EventData(Index%,6) = BUILDTIME(.OnceHour.GetValue() + 12, .OnceMinutes.GetValue(), 0)
  760.                     ENDIF
  761.                 ENDIF
  762.             CASE 1
  763.                 'Scheduled Hourly
  764.                 EventData(Index%,1) = "H"
  765.                 'Get Program
  766.                 EventData(Index%,2) = .ProgramFile.GetText()
  767.                 'Get Minutes after Hour
  768.                 EventData(Index%,5) = .HourlyMinutes.GetValue()
  769.             CASE 2
  770.                 'Scheduled Daily
  771.                 EventData(Index%,1) = "D"
  772.                 'Get Program
  773.                 EventData(Index%,2) = .ProgramFile.GetText()
  774.                 'Get Time
  775.                 IF (.DailyHour.GetValue() = 12) THEN
  776.                     IF (.DailyOption.GetValue() = 0) THEN
  777.                         EventData(Index%,5) = BUILDTIME(0, .DailyMinutes.GetValue(), 0)
  778.                     ELSE
  779.                         EventData(Index%,5) = BUILDTIME(12, .DailyMinutes.GetValue(), 0)
  780.                     ENDIF
  781.                 ELSE 
  782.                     IF (.DailyOption.GetValue() = 0) THEN
  783.                         EventData(Index%,5) = BUILDTIME(.DailyHour.GetValue(), .DailyMinutes.GetValue(), 0)
  784.                     ELSE
  785.                         EventData(Index%,5) = BUILDTIME(.DailyHour.GetValue() + 12, .DailyMinutes.GetValue(), 0)
  786.                     ENDIF
  787.                 ENDIF
  788.             CASE 3
  789.                 'Scheduled Monthly
  790.                 EventData(Index%,1) = "M"
  791.                 'Get Program
  792.                 EventData(Index%,2) = .ProgramFile.GetText()
  793.                 'Get Day of the Month
  794.                 EventData(Index%,5) = .MonthlyDay.GetValue()
  795.                 'Get Time
  796.                 IF (.MonthlyHour.GetValue() = 12) THEN
  797.                     IF (.MonthlyOption.GetValue() = 0) THEN
  798.                         EventData(Index%,6) = BUILDTIME(0, .MonthlyMinutes.GetValue(), 0)
  799.                     ELSE
  800.                         EventData(Index%,6) = BUILDTIME(12, .MonthlyMinutes.GetValue(), 0)
  801.                     ENDIF
  802.                 ELSE 
  803.                     IF (.MonthlyOption.GetValue() = 0) THEN
  804.                         EventData(Index%,6) = BUILDTIME(.MonthlyHour.GetValue(), .MonthlyMinutes.GetValue(), 0)
  805.                     ELSE
  806.                         EventData(Index%,6) = BUILDTIME(.MonthlyHour.GetValue() + 12, .MonthlyMinutes.GetValue(), 0)
  807.                     ENDIF
  808.                 ENDIF
  809.             CASE ELSE
  810.                 'Other (Error)
  811.                 MESSAGE "Error, unrecognized schedule option"
  812.                 EventData(Index%,1) = "X"
  813.                 'Get program anyway.
  814.                 EventData(Index%,2) = .ProgramFile.GetText()
  815.         END SELECT
  816.         DaysToRun$ = ""
  817.         'Create the Active Days matrix by getting the data
  818.         'from the day boxes, one by one.
  819.         IF NOT (.MondayBox.GetValue() = 0) THEN 
  820.             DaysToRun$ = DaysToRun$ & "X"
  821.         ELSE
  822.             DaysToRun$ = DaysToRun$ & " "
  823.         ENDIF
  824.         IF NOT (.TuesdayBox.GetValue() = 0) THEN
  825.             DaysToRun$ = DaysToRun$ & "X"
  826.         ELSE
  827.             DaysToRun$ = DaysToRun$ & " "
  828.         ENDIF
  829.         IF NOT (.WednesdayBox.GetValue() = 0) THEN
  830.             DaysToRun$ = DaysToRun$ & "X"
  831.         ELSE
  832.             DaysToRun$ = DaysToRun$ & " "
  833.         ENDIF
  834.         IF NOT (.ThursdayBox.GetValue() = 0) THEN
  835.             DaysToRun$ = DaysToRun$ & "X"
  836.         ELSE
  837.             DaysToRun$ = DaysToRun$ & " "
  838.         ENDIF
  839.         IF NOT (.FridayBox.GetValue() = 0) THEN
  840.             DaysToRun$ = DaysToRun$ & "X"
  841.         ELSE
  842.             DaysToRun$ = DaysToRun$ & " "
  843.         ENDIF
  844.         IF NOT (.SaturdayBox.GetValue() = 0) THEN
  845.             DaysToRun$ = DaysToRun$ & "X"
  846.         ELSE
  847.             DaysToRun$ = DaysToRun$ & " "
  848.         ENDIF
  849.         IF NOT (.SundayBox.GetValue() = 0) THEN
  850.             DaysToRun$ = DaysToRun$ & "X"
  851.         ELSE
  852.             DaysToRun$ = DaysToRun$ & " "
  853.         ENDIF
  854.         'Set the Active Days matrix.
  855.         EventData(Index%, 3) = DaysToRun$
  856.         
  857.         'Set the last execution to right now, so that the event will
  858.         'not be executed if the conditions are in the past
  859.         EventData(Index%,4) = GETCURRDATE()
  860.     END WITH
  861. END SUB                
  862.  
  863. REM ***************************************************************
  864. REM * MainDialogSub: Event handler for the Main Dialog. This SUB  *
  865. REM * handles all of the user commands; it is, in effect, the     *
  866. REM * heart of the program.                                       *
  867. REM ***************************************************************
  868. SUB MainDialogSub(BYVAL ControlID%, BYVAL Event%)
  869.     DIM ReturnCode AS INTEGER
  870.     DIM Index AS INTEGER
  871.     DIM Mess AS STRING
  872.     DIM DoRemove AS INTEGER
  873.     DIM DoExec AS INTEGER
  874.     DIM DoExit AS INTEGER
  875.  
  876.     IF Event% = EVENT_INITIALIZATION THEN
  877.         'Initialize
  878.         MainDialog.MainList.SetArray ScheduledEvent$
  879.     ENDIF
  880.     IF Event% = EVENT_MOUSE_CLICK THEN
  881.         SELECT CASE ControlID%
  882.             CASE MainDialog.AddEvent.GetID()
  883.                 'Add an event; check if room left
  884.                 IF NumberOfEvents% < MAX_EVENTS THEN
  885.                     'Give a blank event
  886.                     CALL ResetScheduleEvent
  887.                     'Create the Schedule Event Dialog
  888.                     ReturnCode% = DIALOG(ScheduleEvent)
  889.                     IF ReturnCode% = MSG_OK THEN
  890.                         'Add the new entry to the list
  891.                         NumberOfEvents% = NumberOfEvents% + 1
  892.                         CALL GetEventFromDialog NumberOfEvents%
  893.                         CALL MakePrettyString NumberOfEvents%
  894.                         MainDialog.MainList.AddItem ScheduledEvent$(NumberOfEvents%),NumberOfEvents%
  895.                         MainDialog.MainList.SetSelect NumberOfEvents%
  896.                     ENDIF
  897.                 ENDIF
  898.             CASE MainDialog.EditEvent.GetID()
  899.                 'Edit an event; check for a selection
  900.                 IF MainDialog.MainList.GetSelect() <> 0 THEN
  901.                     'Fill the Schedule Event Dialog
  902.                     Index% = MainDialog.MainList.GetSelect()
  903.                     CALL FillScheduleEvent Index%
  904.                     'Create the Dialog
  905.                     ReturnCode% = DIALOG(ScheduleEvent)
  906.                     IF ReturnCode% = MSG_OK THEN
  907.                         'Get the data
  908.                         CALL GetEventFromDialog Index%
  909.                         'Update the list
  910.                         CALL MakePrettyString Index%
  911.                         MainDialog.MainList.RemoveItem Index%
  912.                         MainDialog.MainList.AddItem ScheduledEvent$(Index%),Index%
  913.                         MainDialog.MainList.SetSelect Index%
  914.                     ENDIF
  915.                 ENDIF
  916.             CASE MainDialog.RemoveEvent.GetID()
  917.                 'Remove an event; check for a selection
  918.                 IF MainDialog.MainList.GetSelect() <> 0 THEN
  919.                     'Ask for confirmation
  920.                     Index% = MainDialog.MainList.GetSelect()
  921.                     Mess$ = "Remove execution of" & CHR(13) & EventData(Index%,2) & "?"
  922.                     DoRemove% = MESSAGEBOX(Mess$,"Confirm Remove", MB_YES_NO + MB_QUESTION_ICON)
  923.                     IF DoRemove% = MSG_YES THEN
  924.                         'Remove from the list
  925.                         MainDialog.MainList.RemoveItem(Index%)
  926.                         CALL RemoveFromList Index%
  927.                     ENDIF
  928.                 ENDIF
  929.             CASE MainDialog.ExecuteEvent.GetID()
  930.                 'Execute an event; check for a selection
  931.                 IF MainDialog.MainList.GetSelect() <> 0 THEN
  932.                     'Ask for a confirmation
  933.                     Index% = MainDialog.MainList.GetSelect()
  934.                     Mess$ = "Execute" & CHR(13) & EventData(Index%,2) & "?"
  935.                     DoExec% = MESSAGEBOX(Mess$,"Confirm Execute", MB_YES_NO + MB_QUESTION_ICON)
  936.                     IF DoExec% = MSG_YES THEN
  937.                         'Execute
  938.                         CALL ExecuteEvent Index%
  939.                         'Update the list
  940.                         MainDialog.MainList.RemoveItem(Index%)
  941.                         MainDialog.MainList.AddItem ScheduledEvent$(Index%), Index%
  942.                         MainDialog.MainList.SetSelect Index%
  943.                     ENDIF
  944.                 ENDIF
  945.             CASE MainDialog.ExitButton.GetID()
  946.                 'Exit
  947.                 DoExit% = MESSAGEBOX("Exit Corel SCRIPT Scheduler?", "Confirm Exit", MB_YES_NO + MB_QUESTION_ICON)
  948.                 IF DoExit% = MSG_YES THEN MainDialog.CloseDialog 1
  949.             CASE ELSE
  950.                 ' Nothing
  951.         END SELECT
  952.     END IF
  953.     IF Event% = EVENT_TIMER_EVENT THEN
  954.         'Run the programs if needed
  955.         CALL RunPrograms
  956.         'Update the screen
  957.         Index% = MainDialog.MainList.GetSelect()
  958.         MainDialog.MainList.SetArray ScheduledEvent$
  959.         MainDialog.MainList.SetSelect Index%
  960.     ENDIF
  961.     'Enable the Edit, Remove and Execute buttons if a selection
  962.     'is made
  963.     IF MainDialog.MainList.GetSelect() = 0 THEN
  964.         MainDialog.RemoveEvent.Enable FALSE
  965.         MainDialog.EditEvent.Enable FALSE
  966.         MainDialog.ExecuteEvent.Enable FALSE
  967.     ELSE
  968.         MainDialog.RemoveEvent.Enable TRUE
  969.         MainDialog.EditEvent.Enable TRUE
  970.         MainDialog.ExecuteEvent.Enable TRUE
  971.     ENDIF
  972. END SUB
  973.  
  974. REM ***************************************************************
  975. REM * ScheduleEventSub: Event handler for the Schedule Event      *
  976. REM * Dialog. This is a fairly complex dialog, but its main       *
  977. REM * purpose is data entry, unlike the main dialog which is      *
  978. REM * mostly a controller.                                        *
  979. REM ***************************************************************
  980. SUB ScheduleEventSub(BYVAL ControlID%, BYVAL Event%)
  981.     DIM Option AS INTEGER
  982.     DIM Day AS INTEGER
  983.     DIM Month AS INTEGER
  984.     DIM Year AS INTEGER
  985.     DIM FileName AS STRING
  986.     
  987.     WITH ScheduleEvent
  988.         IF Event% = EVENT_INITIALIZATION THEN
  989.             ' Enable the appropriate controls
  990.             Option% = .ScheduleOption.GetValue()
  991.             EnableOption(Option%)
  992.                     
  993.         ENDIF
  994.         
  995.         IF Event% = EVENT_CHANGE_IN_CONTENT THEN
  996.             SELECT CASE ControlID%
  997.                 CASE .OnceDay.GetID()
  998.                     'Adjust the day if exceeds the "month limits"
  999.                     Day% = .OnceDay.GetValue()
  1000.                     Month% =  .OnceMonth.GetSelect()
  1001.                     Year% = .OnceYear.GetValue()
  1002.                     'Too low
  1003.                     IF Day% < 1 THEN
  1004.                         IF Month% = 1 THEN
  1005.                             'Before january, go back one year
  1006.                             IF Year% = CurrentYear$ THEN
  1007.                                 ' Do not allow change
  1008.                                 .OnceDay.SetValue 1
  1009.                             ELSE
  1010.                                 .OnceYear.SetValue Year% - 1
  1011.                                 .OnceMonth.SetSelect 12
  1012.                                 .OnceDay.SetValue 31
  1013.                             ENDIF
  1014.                         ELSE
  1015.                             'Go back one month
  1016.                             Month% = Month% - 1
  1017.                             .OnceMonth.SetSelect Month%
  1018.                             IF Month% = 2 THEN
  1019.                                 ' Febuary; this tests for a leap year
  1020.                                 IF ((Year% MOD 4) = 0) AND (NOT((Year% MOD 100) = 0) OR ((Year% MOD 400) = 0)) THEN
  1021.                                     .OnceDay.SetValue 29
  1022.                                 ELSE
  1023.                                     .OnceDay.SetValue 28
  1024.                                 ENDIF
  1025.                             ELSEIF (Month% = 1) OR (Month% = 3) OR (Month% = 5) OR (Month% = 7) OR (Month% = 8) OR (Month% = 10) OR (Month% = 12) THEN
  1026.                                 .OnceDay.SetValue 31
  1027.                             ELSE
  1028.                                 .OnceDay.SetValue 30
  1029.                             ENDIF
  1030.                         ENDIF
  1031.                     ELSEIF Day% > 28 THEN
  1032.                         'We may have moved passed the limit
  1033.                         IF Month% = 2 THEN
  1034.                             ' Febuary
  1035.                             ' This tests for a leap year; if it exceeds anyway, then we advance to March
  1036.                             IF NOT((Year% MOD 4) = 0) AND (NOT ((Year% MOD 100) = 0) OR ((Year% MOD 400) = 0)) OR Day% > 29 THEN
  1037.                                 .OnceMonth.SetSelect 3
  1038.                                 .OnceDay.SetValue 1
  1039.                             ENDIF
  1040.                         ELSEIF (Month% = 12) THEN
  1041.                             ' December; we may have to advance a year
  1042.                             IF Day% > 31 THEN
  1043.                                 .OnceYear.SetValue Year + 1
  1044.                                 .OnceMonth.SetSelect 1
  1045.                                 .OnceDay.SetValue 1
  1046.                             END IF
  1047.                         ELSEIF (Day% >31) OR (NOT((Month% = 1) OR (Month% = 3) OR (Month% = 5) OR (Month% = 7) OR (Month% = 8) OR (Month% = 10)) AND Day% > 30) THEN
  1048.                             ' Other month
  1049.                             .OnceMonth.SetSelect Month% + 1
  1050.                             .OnceDay.SetValue 1
  1051.                         ENDIF
  1052.                     ENDIF                
  1053.                     
  1054.                     .OnceTH.SetText NumeralToOrdinal$(CINT(.OnceDay.GetValue())) & ","
  1055.  
  1056.                 CASE .MonthlyDay.GetID()
  1057.                     ' Wraparound if we come out of the limits. As we cannot limit ourselves to
  1058.                     ' the length of the shorter months, we have to accept any value up to 31.
  1059.                     Day% = .MonthlyDay.GetValue()
  1060.                     IF Day% < 1 THEN
  1061.                         .MonthlyDay.SetValue 31
  1062.                     ELSEIF Day% > 31 THEN
  1063.                         .MonthlyDay.SetValue 1
  1064.                     ENDIF
  1065.                                         
  1066.                     .MonthlyTH.SetText NumeralToOrdinal$(CINT(.MonthlyDay.GetValue()))
  1067.  
  1068.                 'Wraparounds for hours
  1069.                 CASE .DailyHour.GetID()
  1070.                     IF .DailyHour.GetValue() > 12 THEN .DailyHour.SetValue 1
  1071.                     IF .DailyHour.GetValue() < 1 THEN .DailyHour.SetValue 12
  1072.                 CASE .OnceHour.GetID()
  1073.                     IF .OnceHour.GetValue() > 12 THEN .OnceHour.SetValue 1
  1074.                     IF .OnceHour.GetValue() < 1 THEN .OnceHour.SetValue 12
  1075.                 CASE .MonthlyHour.GetID()
  1076.                     IF .MonthlyHour.GetValue() > 12 THEN .MonthlyHour.SetValue 1
  1077.                     IF .MonthlyHour.GetValue() < 1 THEN .MonthlyHour.SetValue 12
  1078.                 'Wraparounds for Minutes.
  1079.                 CASE .OnceMinutes.GetID()
  1080.                     WHILE .OnceMinutes.GetValue() > 59
  1081.                         .OnceMinutes.SetValue .OnceMinutes.GetValue() - 60
  1082.                     WEND
  1083.                     WHILE .OnceMinutes.GetValue() < 0
  1084.                         .OnceMinutes.SetValue .DailyMinutes.GetValue() + 60
  1085.                     WEND
  1086.                 CASE .DailyMinutes.GetID()
  1087.                     WHILE .DailyMinutes.GetValue() > 59
  1088.                         .DailyMinutes.SetValue .DailyMinutes.GetValue() - 60
  1089.                     WEND
  1090.                     WHILE .DailyMinutes.GetValue() < 0
  1091.                         .DailyMinutes.SetValue .DailyMinutes.GetValue() + 60
  1092.                     WEND
  1093.                 CASE .HourlyMinutes.GetID()
  1094.                     WHILE .HourlyMinutes.GetValue() > 59
  1095.                         .HourlyMinutes.SetValue .HourlyMinutes.GetValue() - 60
  1096.                     WEND
  1097.                     WHILE .HourlyMinutes.GetValue() < 0
  1098.                         .HourlyMinutes.SetValue .HourlyMinutes.GetValue() + 60
  1099.                     WEND
  1100.                 CASE .MonthlyMinutes.GetID()
  1101.                     WHILE .MonthlyMinutes.GetValue() > 59
  1102.                         .MonthlyMinutes.SetValue .MonthlyMinutes.GetValue() - 60
  1103.                     WEND
  1104.                     WHILE .MonthlyMinutes.GetValue() < 0
  1105.                         .MonthlyMinutes.SetValue .MonthlyMinutes.GetValue() + 60
  1106.                     WEND
  1107.             END SELECT
  1108.         END IF
  1109.         IF Event% = EVENT_MOUSE_CLICK THEN
  1110.             SELECT CASE ControlID%
  1111.                 'Enable appropriate controls for the schedule options
  1112.                 CASE .ScheduleOnce.GetID()
  1113.                     Option% = .ScheduleOption.GetValue()
  1114.                     EnableOption(Option%)
  1115.                 CASE .ScheduleDaily.GetID()
  1116.                     Option% = .ScheduleOption.GetValue()
  1117.                     EnableOption(Option%)
  1118.                 CASE .ScheduleHourly.GetID()
  1119.                     Option% = .ScheduleOption.GetValue()
  1120.                     EnableOption(Option%)
  1121.                 CASE    .ScheduleMonthly.GetID()
  1122.                     Option% = .ScheduleOption.GetValue()
  1123.                     EnableOption(Option%)
  1124.                 'Browse
  1125.                 CASE .BrowseButton.GetID()
  1126.                     Filename$ = GETFILEBOX("Executable Files (*.exe)|*.exe|Corel SCRIPT Files (*.csc)|*.csc|All Files|*.*", "Choose an application or Script to Run", 0)
  1127.                     IF Filename$ <> "" THEN
  1128.                         .ProgramFile.SetText(FileName$)
  1129.                     ENDIF
  1130.                 CASE ELSE
  1131.                     'Nothing
  1132.             END SELECT
  1133.         ENDIF
  1134.     END WITH
  1135. END SUB
  1136.  
  1137.  
  1138. REM ***************************************************************
  1139. REM * Main Program                                                *
  1140. REM ***************************************************************
  1141. 'Initialize data
  1142. DIM dbl AS double
  1143. DIM index AS INTEGER
  1144. CALL Initialize
  1145.  
  1146. 'Create the file if it does not exist
  1147. OPEN DATFOL$ & "\Schedule.Dat" FOR APPEND AS 1
  1148. CLOSE
  1149.  
  1150. 'Load the file
  1151. OPEN DATFOL$ & "\Schedule.Dat" FOR INPUT AS 1
  1152.  
  1153. WHILE ((NOT EOF(1)) AND (NumberOfEvents% < MAX_EVENTS))
  1154.     NumberOfEvents% = NumberOfEvents% + 1
  1155.     ' Read schedule
  1156.     INPUT #1, EventData(NumberOfEvents%, 1)
  1157.     ' Read program
  1158.     INPUT #1, EventData(NumberOfEvents%, 2)
  1159.     ' Read active days matrix
  1160.     INPUT #1, EventData(NumberOfEvents%, 3)
  1161.     ' Read last execution
  1162.     INPUT #1, dbl#
  1163.     EventData(NumberOfEvents%, 4)=dbl#
  1164.     SELECT CASE EventData(NumberOfEvents%, 1)
  1165.         CASE "O"
  1166.             'Scheduled Once
  1167.             'Read date
  1168.             INPUT #1, dbl#
  1169.             EventData(NumberOfEvents%, 5)=dbl#
  1170.             'Read time
  1171.             INPUT #1, dbl#
  1172.             EventData(NumberOfEvents%, 6) = dbl#
  1173.         CASE "H"
  1174.             'Scheduled Hourly
  1175.             'Read number of minutes
  1176.             INPUT #1, dbl#
  1177.             EventData(NumberOfEvents%, 5)=dbl#
  1178.         CASE "D"
  1179.             'Scheduled Daily
  1180.             'Read time
  1181.             INPUT #1, dbl#
  1182.             EventData(NumberOfEvents%, 5)=dbl#
  1183.         CASE "M"
  1184.             'Scheduled Monthly
  1185.             'Read day of the month
  1186.             INPUT #1, dbl#
  1187.             EventData(NumberOfEvents%, 5)=dbl#
  1188.             'Read time
  1189.             INPUT #1, dbl#
  1190.             EventData(NumberOfEvents%, 6)=dbl#
  1191.         CASE ELSE
  1192.             MESSAGE "File Error!"
  1193.             NumberOfEvents% = NumberOfEvents% - 1
  1194.     END SELECT
  1195.     CALL MakePrettyString NumberOfEvents%
  1196. WEND
  1197.  
  1198. CLOSE 1
  1199.  
  1200. MainDialog.SetStyle STYLE_MINIMIZEBOX
  1201.  
  1202. 'Set Timer at one minute
  1203. MainDialog.SetTimer 10000
  1204.  
  1205. 'Have a go!
  1206. DIALOG MainDialog
  1207.  
  1208. 'Save the file
  1209. OPEN DATFOL$ & "\Schedule.Dat" FOR OUTPUT AS 1
  1210.  
  1211. FOR Index% = 1 TO NumberOfEvents%
  1212.     ' Write event type
  1213.     WRITE #1, EventData(Index%, 1),
  1214.     ' Write program
  1215.     WRITE #1, EventData(Index%, 2),
  1216.     ' Write active days matrix
  1217.     WRITE #1, EventData(Index%, 3),
  1218.     'Write last execution
  1219.     WRITE #1, CDBL(EventData(Index%, 4)),
  1220.     SELECT CASE EventData(Index%, 1)
  1221.         CASE "O"
  1222.             'Scheduled Once
  1223.             'Write date
  1224.             WRITE #1, CDBL(EventData(Index%, 5)),
  1225.             'Write time
  1226.             WRITE #1, CDBL(EventData(Index%, 6))
  1227.         CASE "H"
  1228.             'Scheduled Hourly
  1229.             'Write number of minutes
  1230.             WRITE #1, EventData(Index%, 5)
  1231.         CASE "D"
  1232.             'Scheduled Daily
  1233.             'Write time
  1234.             WRITE #1, CDBL(EventData(Index%, 5))
  1235.         CASE "M"
  1236.             'Scheduled Monthly
  1237.             'Write day
  1238.             WRITE #1, EventData(Index%, 5),
  1239.             'Write time
  1240.             WRITE #1, CDBL(EventData(Index%, 6))
  1241.         CASE ELSE
  1242.     END SELECT
  1243. NEXT Index%
  1244.  
  1245. CLOSE 1
  1246.  
  1247.  
  1248.