Jump to content

Creating a ComboBox with values for schtasks /ST


Go to solution Solved by mdwerne,

Recommended Posts

So I apologize in advance for the horrible mess you see below. While there may be many issues with my code, I only need help with one part. This GUI is an interface for customers to install software pushed via SCCM. While I have Continue and Abort buttons working just fine, I'm trying to implement a scheduler that will allow the customer to defer the install for later in the day up until 11:30pm.

I'd like to have the combobox display the current time (12 hour format) and then all hours (both top and bottom of the hour) until 11:30pm. So if they check the "Defer" box, select a time from the dropdown (such as 8:30pm) and click "Go"...the time chosen will be sent to my 'unfinished' Scheduler function which will then create a task to be executed at the specified time. I hope that all makes some sense.

So again, the only thing I would like some help with is how to correctly create the combobox with the possible values based on the current local time. I should be able to figure out the rest.

Thanks for any suggestions,

-Mike

Edit:...Oops, if the moderator would like to move this post to the "Graphical User Interface (GUI) Help and Support", I wasn't thinking when I hit submit...sorry.

#include "_XMLDomWrapper.au3"
#include <Constants.au3>
#include <GuiRichEdit.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Date.au3>

;Global $sFile = (@ScriptDir & "\Config.xml")
Global $hGUI, $FormTitle, $RTFName, $ProgramName, $TrayTip, $ShutdownTimer, $ParamName, $AbortAllowed, $AbortButtonName, $ContinueButtonName, $GoButton, $Scheduler
Global $hRichEdit, $ExitCode, $Width, $Height, $sHours

If @DesktopWidth > 1024 Then
    $Width = 1024
Else
    $Width = @DesktopWidth
EndIf

If @DesktopHeight > 768 Then
    $Height = 768
Else
    $Height = @DesktopHeight
EndIf

;_XMLFileOpen($sFile)

$FormTitle = _XMLGetAttrib("/MessageInfo/Title", "FormTitle")
$RTFName = _XMLGetAttrib("/MessageInfo/Display", "RTFName")
$ProgramName = _XMLGetAttrib("/MessageInfo/Program", "ProgramName")
$TrayTip = _XMLGetAttrib("/MessageInfo/TrayTip", "TrayTip")
$ShutdownTimer = _XMLGetAttrib("/MessageInfo/ShutdownTimer", "ShutdownTimer")
$ParamName = _XMLGetAttrib("/MessageInfo/Param", "ParamName")
$AbortAllowed = _XMLGetAttrib("/MessageInfo/Abort", "AbortAllowed")
$AbortButtonName = _XMLGetAttrib("/MessageInfo/ABN", "AbortButtonName")
$ContinueButtonName = _XMLGetAttrib("/MessageInfo/CBN", "ContinueButtonName")
$Scheduler = _XMLGetAttrib("/MessageInfo/Sched", "Scheduler")

_Main()

; Functions
Func _Main()
    Local $msg, $AbortButton, $ContinueButton, $ScheduleBox
    $hGUI = GUICreate($FormTitle, $Width - 20, $Height - 100, -1, 10, -1)
    $hRichEdit = _GUICtrlRichEdit_Create($hGUI, "", 10, 10, $Width - 40, $Height - 150, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
    ;GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    _GUICtrlRichEdit_SetEventMask($hRichEdit, $ENM_LINK)
    _GUICtrlRichEdit_AutoDetectURL($hRichEdit, True)
    $ContinueButton = GUICtrlCreateButton($ContinueButtonName, $Width - 500, $Height - 133, 100, 25)
    GUICtrlSetFont($ContinueButton, 10, 800, 0, "Tahoma")
    $AbortButton = GUICtrlCreateButton($AbortButtonName, $Width - 390, $Height - 133, 100, 25)
    GUICtrlSetFont($AbortButton, 10, 800, 0, "Tahoma")
    $ScheduleBox = GUICtrlCreateCheckbox("Defer", $Width - 280, $Height - 133, 75, 25)
    GUICtrlSetFont($ScheduleBox, 10, 800, 0, "Tahoma")

    Global $hCombo = GUICtrlCreateCombo("", $Width - 200, $Height - 132, 90, 25)
    GUICtrlSetData(-1, @HOUR + 1 & "|" & @HOUR + 2 & "|" & @HOUR + 3 & "|" & @HOUR + 4 & "|" & @HOUR + 5 & "|" & @HOUR + 6 & "|" & @HOUR + 7 & "|" & @HOUR + 8)

    $GoButton = GUICtrlCreateButton("Go", $Width - 75, $Height - 133, 50, 25)
    GUICtrlSetFont($GoButton, 10, 800, 0, "Tahoma")

    GUISetState()
    _GUICtrlRichEdit_StreamFromFile($hRichEdit, $RTFName)
    _GUICtrlRichEdit_SetReadOnly($hRichEdit)
    _GUICtrlRichEdit_GotoCharPos($hRichEdit, 0)
    MouseClick("main", @DesktopWidth / 2, @DesktopHeight / 2, 0)
    Sleep(1000)
    ;GUISetState(@SW_MINIMIZE)
    GUISetState(@SW_SHOW)

    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $ContinueButton
                Opt("TrayIconHide", 1)
                GUISetState(@SW_HIDE)
                $ExitCode = RunWait(@ScriptDir & "\" & $ProgramName)
                _GUICtrlRichEdit_Destroy($hRichEdit)
                ;MsgBox(0, "Program Exit Code", $ExitCode)
                Exit $ExitCode
            Case $msg = $GoButton
                Opt("TrayIconHide", 1)
                GUISetState(@SW_HIDE)
                ;Scheduler()
                _GUICtrlRichEdit_Destroy($hRichEdit)
                Exit
            Case $msg = $GUI_EVENT_CLOSE Or $msg = $AbortButton
                _GUICtrlRichEdit_Destroy($hRichEdit)
                Exit -1
        EndSelect
    WEnd
EndFunc   ;==>_Main

;~ Func Scheduler()
;~  Local $sXtra_Parms
;~  Local $sDateTime = _DateAdd('h', $sHours, _NowCalc())
;~  Local $sStartDate = StringMid($sDateTime, 6, 3) & StringMid($sDateTime, 9, 2) & "/" & StringLeft($sDateTime, 4)
;~  $sStartTime = StringRight($sDateTime, 8)

;~  If @OSVersion = "WIN_VISTA" Or @OSVersion = "WIN_7" Or @OSVersion = "WIN_8" Then $sXtra_Parms = " /Z /V1"

;~  Local $QueryTask = Run("SCHTASKS /QUERY /TN " & $FormTitle, "", @SW_HIDE, $STDOUT_CHILD)

;~  Local $line = ""

;~  While 1
;~      $line &= StdoutRead($QueryTask)
;~      If @error Then ExitLoop
;~  WEnd

;~  If $line = "" Then
;~      $sCmd = 'SCHTASKS /CREATE /SC ONCE /TN ' & $FormTitle & ' /TR ' & $ProgramName & ' /SD ' & $sStartDate & ' /ST ' & $sStartTime & ' /RU SYSTEM' & $sXtra_Parms
;~      Run($sCmd, "", @SW_HIDE)
;~      ;MsgBox(0, "", $sCmd) ; for testing
;~  Else
;~      $sCmd = 'SCHTASKS /CHANGE /TN ' & $FormTitle & ' /SD ' & $sStartDate & ' /ST ' & $sStartTime
;~      Run($sCmd, "", @SW_HIDE)
;~      ;MsgBox(0, "", $sCmd) ; for testing
;~  EndIf
;~ EndFunc   ;==>Scheduler

Here is the Config.xml file if you're interested.

<?xml version="1.0"?>
<MessageInfo>
    <Title FormTitle="EMET 4.1"/>
    <Display RTFName="RichText.rtf"/>
    <Program ProgramName="Emet installer.exe /sccm"/>
    <TrayTip TrayTip="EMET 4.1 Available"/>
    <ShutdownTimer ShutdownTimer="300000"/>
    <Param ParamName=""/>
    <Abort AbortAllowed="Y" />
    <ABN AbortButtonName="Not Now"/>
    <CBN ContinueButtonName="Continue"/>
    <Sched Scheduler="0"/>
</MessageInfo>
Edited by mdwerne
Link to comment
Share on other sites

Anyone have any suggestions on this, I've been poking at this for a couple days and not really making any progress.

If the mess above is too confusing, I can simplify and restate.

Thanks for taking a look,

-Mike

Link to comment
Share on other sites

  • Solution

Okay...I was left to my own devices long enough and actually came up with something, not pretty, but it seems to work.

(?) My question now is, where is the "0" coming from at the end of my ComboBox, and do I remove the "0". (?)

#include <GuiComboBox.au3>
#include <GUIConstantsEx.au3>
#include <Date.au3>

Global $CurrentTime, $i, $AMPM

If @HOUR > 12 Then
    $AMPM = "PM"
ElseIf @HOUR = 0 Then
    $AMPM = "AM"
Else
    $AMPM = "AM"
EndIf

GUICreate("ComboTest", 200, 40)
Global $hCombo = GUICtrlCreateCombo("New install time:", 10, 10, 180, 25)
GUICtrlSetData(-1, GetTimes())
GUISetState()
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

Func GetTimes()
    Local $CurrentHour = StringTrimRight(_NowTime(), 9)
    For $i = $CurrentHour To 11 Step +1
        _GUICtrlComboBox_BeginUpdate($hCombo)
        _GUICtrlComboBox_InsertString($hCombo, $i & ":00 " & $AMPM, -1)
        _GUICtrlComboBox_InsertString($hCombo, $i & ":30 " & $AMPM, -1)
        _GUICtrlComboBox_EndUpdate($hCombo)
    Next
EndFunc   ;==>GetTimes

Thanks for your time,

-Mike

Link to comment
Share on other sites

GetTimes() returns 0 so do not use GUICtrlSetData to add the return value of 0 to the combo.

Updated code

#include <GuiComboBox.au3>
#include <GUIConstantsEx.au3>
#include <Date.au3>

Global $CurrentTime, $i, $AMPM

If @HOUR > 12 Then
    $AMPM = "PM"
ElseIf @HOUR = 0 Then
    $AMPM = "AM"
Else
    $AMPM = "AM"
EndIf

GUICreate("ComboTest", 200, 40)
Global $hCombo = GUICtrlCreateCombo("New install time:", 10, 10, 180, 25)
; function returns value of 0
GetTimes()
GUISetState()
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

Func GetTimes()
    Local $CurrentHour = StringTrimRight(_NowTime(), 9)
    _GUICtrlComboBox_BeginUpdate($hCombo)
    For $i = $CurrentHour To 11
        _GUICtrlComboBox_InsertString($hCombo, $i & ":00 " & $AMPM, -1)
        _GUICtrlComboBox_InsertString($hCombo, $i & ":30 " & $AMPM, -1)
    Next
    _GUICtrlComboBox_EndUpdate($hCombo)
EndFunc   ;==>GetTimes
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

×
×
  • Create New...