Jump to content

Shutdown helper


scarboni
 Share

Recommended Posts

I created a script which will help out closing programs befor shutdown/restart/hibernate your computer. The idea came to me when my computer didn't turn of properly when I clicked Start -> Shutdown, it for some reason got stuck on shuting down the programs running.

This script can solve some problems but it probably isn't a world changing script I just wanted to post it for others who might have the same problem and want to solve it with a easy script. Please send feedback, thoughts and if you find anything that could be a contribution for its upgrade.

ShutIT

** GUI **

*Menu*

File - Exit script

Tools- Clear the ini file and go into choose programs process.

Help - A little information part.

*Buttons*

Programs - choose which programs that are active to close when starting the script.

Start - Start the main function

Cancel - Exit the script

*List View*

Shows the choose programs.

*Radio buttons*

Use same settings next time - If you don't want to open the GUI next time you double click the file but start the script immediately

** Function **

  • First the script will check if you already got a ini file created if not it will create one, if you got programs checked from previously runs it will automaticly add thouse to the list view.
  • If you enter the Choose programs section you will be able to mark all, unmark all active processes. When you have marked all the processes you want to close during the script you will have to click the button Save and it will jump back to first GUI screen, during this it will save all your choosen programs and add them to the ini file.
  • Decided if you just want to close the choosen programs or if you want to close the programs and shutdown/restart/hibernate afterwards.
  • If you do not want to go into this GUI again you can mark the Save settings for next time and it will remember the programs you have choosen.

Download link: ShutIT.zip

Website: Website link

Screenshot:

Posted Image

Edited by scarboni
Link to comment
Share on other sites

No source code :)

I will post the source code in this post.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <TreeViewConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListboxConstants.au3>
#include <IE.au3>

Global $list = ProcessList()
Global $iniPath = @HomePath & "\ShutIT.ini"
Global $max = $list[0][0]+1
Global $arr[$max]

If FileExists($iniPath) Then
    $var = IniReadSection($iniPath, "Action")
    If @error Then 
        _Start()
    Else
    For $i = 1 To $var[0][0] 
        If ($var[$i][1] = "1") Then
            Global $option = $var[$i][0]
            _PreShut()
        Else
            _Start()
        EndIf
    Next
    EndIf
Else 
    _Start()
EndIf


;===============================================================
;Start Function GUI
;===============================================================
Func _Start()
$Start = GUICreate("ShutIT",230, 320)
;~~~~~~~~~~GUI Top Menu~~~~~~~~~~
$fileMenu = GUICtrlCreateMenu("&File")
$exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
$settingsMenu = GUICtrlCreateMenu("Tools")
$settingsitem = GUICtrlCreateMenuItem("Settings", $SettingsMenu)
$deleteitem = GUICtrlCreateMenuItem("Delete ini", $SettingsMenu)
$helpMenu = GUICtrlCreateMenu("Help")
$infoitem = GUICtrlCreateMenuItem("Info", $helpMenu)
;~~~~~~~~~~GUI List~~~~~~~~~~
GUICtrlCreateGroup("Program Settings", 10, 40, 210, 150)
GUICtrlCreateLabel("Chose programs to exit and which action to take when running ShutIT", 10, 10, 210,30)
$procList = GUICtrlCreateList("", 20,60, 190, 105, $LBS_NOSEL)
;~~~~~~~~~~GUI Combo~~~~~~~~~~
GUICtrlCreateGroup("Action Settings", 10, 195, 210, 68)
$labelCombo = GUICtrlCreateLabel("Action:", 35, 215)
Global $decisionCombo = GUICtrlCreateCombo("Close Programs", 75,212, 120)
GUICtrlSetData(-1, "Shutdown|Restart|Hibernate")
;~~~~~~~~~~GUI Check Box~~~~~~~~~~
$labelCheckBox = GUICtrlCreateLabel("Use same settings next time:", 35, 240)
Global $startCheckBox = GUICtrlCreateCheckBox("", 178,238,20,20)
;~~~~~~~~~~GUI Buttons~~~~~~~~~~
$buttonStart = GUICtrlCreateButton("Start", 30, 270, 75, 25)
$buttonCancel = GUICtrlCreateButton("Cancel", 125, 270, 75,25)
$buttonPrograms = GUICtrlCreateButton("Programs", 20, 160, 70, 23)




;~~~~~~~~~~List chosen programs to close ~~~~~~~~~~
For $i = 1 to $list[0][0]
    If ($list[$i][0] == "[System Process]") Or ($list[$i][0] == "svchost.exe") Then
        ;Do Nothing
    Else
    $iniCheck = IniRead($iniPath, "Checked", $list[$i][0], "empty")
        If FileExists($iniPath) Then 
            If Not(($iniCheck == 4) Or ($iniCheck == 260) Or ($iniCheck == 0) Or ($iniCheck = "empty")) Then
                $test = GUICtrlSetData($procList, $list[$i][0])
            Else
                ;Do Nothing 
            EndIf
        Else
            ;Do Nothing
        EndIf
    EndIf   
Next

GUISetState()
While 1
$msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE Or $msg = $exititem Or $msg = $buttonCancel
            ExitLoop
        Case $msg = $settingsitem Or $msg = $buttonPrograms
            GUIDelete()
            Sleep(100)
            _ProcessChoice()
        Case $msg = $buttonStart
            _Shutit()
        Case $msg = $infoitem
            _Info()
        Case $msg = $deleteitem
            FileDelete($iniPath)
            TrayTip("ShutIT", "The ini file has been deleted", 5,1)         
    EndSelect
WEnd
_Close()
EndFunc

;===============================================================
;Process Picker GUI
;===============================================================
Func _ProcessChoice()
Global $Choice = GUICreate("ShutIT",300, 530)
;~~~~~~~~~~GUI TreeView~~~~~~~~~~
$CBTree = GUICtrlCreateTreeView(10,50,280,390, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_DISABLEDRAGDROP, $TVS_CHECKBOXES), $WS_EX_CLIENTEDGE)
;~~~~~~~~~~GUI Buttons~~~~~~~~~~
$buttonCheck = GUICtrlCreateButton("Save", 140, 500, 70, 25)
$buttonCancel = GUICtrlCreateButton("Cancel", 220, 500, 70, 25)
$buttonCheckAll = GUICtrlCreateButton("Check All", 10, 445, 70, 20)
$buttonUnCheckAll = GUICtrlCreateButton("UnCheck All", 90, 445, 70, 20)
;~~~~~~~~~~Misc~~~~~~~~~~
$labelTop = GUICtrlCreateLabel("Choose which programs/applications you want to close during the shutdown process", 10, 10, 280,40)
$iniFile = IniReadSection($iniPath, "Checked")

;~~~~~~~~~~Check previously chosen programs~~~~~~~~~~
For $i = 1 to $list[0][0]

    If ($list[$i][0] == "[System Process]") Or ($list[$i][0] == "svchost.exe") Then
        ;Do Nothing
    Else
        $arr[$i] = GUICtrlCreateTreeViewItem($list[$i][0], $CBTree)
        $iniCheck = IniRead($iniPath, "Checked", $list[$i][0], "empty")
        
        If FileExists($iniPath) Then 
            If Not(($iniCheck == 4) Or ($iniCheck == 260)) Then
                For $z = 1 To $iniFile[0][0]
                    If ($list[$i][0] ==  $iniFile[$z][0]) Then
                        GUICtrlSetState($arr[$i], $GUI_CHECKED)
                    Else
                        ;Do nothing
                    EndIf
                Next
            Else
                ;Do Nothing 
            EndIf
        Else
            ;Do nothing
        EndIf
    EndIf
Next

GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $buttonCheck
            _Check()
            GUIDelete()
            Sleep(100)
            _Start()
        Case $msg = $buttonCancel
            GUIDelete()
            Sleep(100)
            _Start()
        Case $msg = $buttonCheckAll
            For $z = 1 To $max-1
            GUICtrlSetState($arr[$z], $GUI_CHECKED)
            Next
        Case $msg = $buttonUnCheckAll
            For $z = 1 To $max-1
            GUICtrlSetState($arr[$z], $GUI_UNCHECKED)
            Next
    EndSelect
WEnd
GUIDelete()
Sleep(100)
_Start()

EndFunc

;===============================================================
;Check if a Process has been saved befor
;===============================================================
Func _Check()
for $i = 1 to $max-1
$test = GUICtrlRead($arr[$i])
$name = GUICtrlRead($arr[$i],1)
    If ($name == "0") Then
        ;Do Nothing
    Else
        IniWrite($iniPath, "Checked", $name, $test)
    EndIf
Next
EndFunc

;===============================================================
;Start the ShutIT function
;===============================================================
Func _Shutit()
$inputDecision = GUICtrlRead($decisionCombo)
$inputSave = GUICtrlRead($startCheckBox)

;~~~~~~~~~~Close the chosen programs~~~~~~~~~~
For $i = 1 to $list[0][0]
    If ($list[$i][0] == "[System Process]") Or ($list[$i][0] == "svchost.exe") Then
        ;Do Nothing
    Else
    $iniCheck = IniRead($iniPath, "Checked", $list[$i][0], "empty")
        If FileExists($iniPath) Then 
            If Not(($iniCheck == 4) Or ($iniCheck == 260)) Then
                ProcessClose($list[$i][0])
                TrayTip("ShutIT", "Closing "&$list[$i][0],3,1)
            Else
                ;Do Nothing 
            EndIf
        Else
            ;Do Nothing
        EndIf
    EndIf   
Next

If ($inputDecision = "Shutdown") Then
    If ($inputSave = 1) Then
        $cont = MsgBox(36,"","You have set ShutIT to "&$inputDecision&", this setting will be saved and the next time you run ShutIT this action will take place without any window pop-up."&@CRLF&""&@CRLF&"To be able to chose new programs or another action you will have to change the value under [Action] section in shutit.ini to a 0.")
        Select
            Case $cont = $GUI_EVENT_CLOSE
                GUIDelete()
                _Start()
            Case $cont = 6
                IniWrite($iniPath, "Action", $inputDecision, "1")
            Case $cont = 7
                GUIDelete()
                _Start()
        EndSelect
    Else
        IniWrite($iniPath, "Action", $inputDecision, "0")
    EndIf
    Sleep(1000)
    Shutdown(5, "Forced AutoIT Shutdown")
ElseIf ($inputDecision = "Restart") Then
    If ($inputSave = 1) Then
        $cont = MsgBox(36,"","You have set ShutIT to "&$inputDecision&", this setting will be saved and the next time you run ShutIT this action will take place without any window pop-up."&@CRLF&""&@CRLF&"To be able to chose new programs or another action you will have to change the value under [Action] section in shutit.ini to a 0.")
        Select
            Case $cont = $GUI_EVENT_CLOSE
                GUIDelete()
                _Start()
            Case $cont = 6
                IniWrite($iniPath, "Action", $inputDecision, "1")
            Case $cont = 7
                GUIDelete()
                _Start()
        EndSelect
    Else
        IniWrite($iniPath, "Action", $inputDecision, "0")
    EndIf
    Sleep(1000)
    Shutdown(6, "Forced AutoIT Restart")
ElseIf ($inputDecision = "Hibernate") Then
    If ($inputSave = 1) Then
    $cont = MsgBox(36,"","You have set ShutIT to "&$inputDecision&", this setting will be saved and the next time you run ShutIT this action will take place without any window pop-up."&@CRLF&""&@CRLF&"To be able to chose new programs or another action you will have to change the value under [Action] section in shutit.ini to a 0.")
        Select
            Case $cont = $GUI_EVENT_CLOSE
                GUIDelete()
                _Start()
            Case $cont = 6
                IniWrite($iniPath, "Action", $inputDecision, "1")
            Case $cont = 7
                GUIDelete()
                _Start()
        EndSelect
    Else
        IniWrite($iniPath, "Action", $inputDecision, "0")
    EndIf
    Sleep(1000)
    Shutdown(68, "Forced AutoIT Hibernate")
ElseIf ($inputDecision = "Close Programs") Then
    If ($inputSave = 1) Then
        $cont = MsgBox(36,"","You have set ShutIT to "&$inputDecision&", this setting will be saved and the next time you run ShutIT this action will take place without any window pop-up."&@CRLF&""&@CRLF&"To be able to chose new programs or another action you will have to change the value under [Action] section in shutit.ini to a 0.")
        Select
            Case $cont = $GUI_EVENT_CLOSE
                GUIDelete()
                _Start()
            Case $cont = 6
                IniWrite($iniPath, "Action", $inputDecision, "1")
            Case $cont = 7
                GUIDelete()
                _Start()
        EndSelect
    Else
        IniWrite($iniPath, "Action", $inputDecision, "0")
    EndIf
    Sleep(1000)
Else 
    ;Do nothing
EndIf

EndFunc

Func _PreShut()
;~~~~~~~~~~Close the chosen programs~~~~~~~~~~
For $i = 1 to $list[0][0]
    If ($list[$i][0] == "[System Process]") Or ($list[$i][0] == "svchost.exe") Then
        ;Do Nothing
    Else
    $iniCheck = IniRead($iniPath, "Checked", $list[$i][0], "empty")
        If FileExists($iniPath) Then 
            If Not(($iniCheck == 4) Or ($iniCheck == 260)) Then
                ProcessClose($list[$i][0])
                TrayTip("ShutIT", "Closing "&$list[$i][0],3,1)
            Else
                ;Do Nothing 
            EndIf
        Else
            ;Do Nothing
        EndIf
    EndIf   
Next

If ($option = "Shutdown") Then
    Shutdown(5, "Forced AutoIT Shutdown")
ElseIf ($option = "Restart") Then
    Shutdown(6, "Forced AutoIT Restart")
ElseIf ($option = "Hibernate") Then
    Shutdown(68, "Forced AutoIT Hibernate")
ElseIf ($option = "Close Programs") Then
    ;Do Nothing
Else 
    ;Do nothing
EndIf


EndFunc

;===============================================================
;Close the script
;===============================================================
Func _Close()
    Exit
EndFunc

;===============================================================
;Information GUI
;===============================================================
Func _Info()
    $text = ""&@CRLF&"This program is made for thouse who want to shutdown certain programs with one click. You can chose if you want to just shutdown programs or if you want to shutdown programs and Restart/Shutdown/Hybernate your computer"&@CRLF&""&@CRLF&"Step-by-Step"&@CRLF&"1. Go to Tools > Settings and chose which programs to close"&@CRLF&"2. Click the Save button"&@CRLF&"3. Decide if you want to Restart/Shutdown/Hybernate or just close programs"&@CRLF&"4. Click the Start button"&@CRLF&"5. Let the program do its job"&@CRLF&""&@CRLF&"This program is coded in AutoIt scripting language if you have any feedback or is interested in more visit http://www.rocksta.se"
    GUICreate("ShutIT - Information",400,350)
    $title = GUICtrlCreateLabel("ShutIT", 170, 20, 100)
    GUICtrlSetFont(-1, 12, 800)
    $owner = GUICtrlCreateLabel("Tony Sundström Langlet", 140, 50, 200)
    $link = GUICtrlCreateLabel("www.rocksta.se",160, 70, 100)
    GUICtrlSetColor(-1, 0x0000FF)
    $buttonClose = GUICtrlCreateButton("Close", 160, 310, 70, 25)
    $infoEdit = GUICtrlCreateEdit($text, 10, 100, 380, 200, BitOr($ES_READONLY, $ES_MULTILINE))
    GUICtrlSetBkColor(-1, 0xffffff)
    
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $buttonClose
            ExitLoop
        Case $msg = $link
            _IECreate("http://www.rocksta.se")
    EndSelect
WEnd
GUIDelete()
EndFunc
Edited by scarboni
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...