Jump to content

Go2Sleep


Keither
 Share

Recommended Posts

Hello everyone.

I would like to share with my first script which turns off/reboot/standby/hibernate computer in X (where X is <1;300>) minutes. I wrote this program because I often fall asleep while listening music from my laptop.

;by Keither
#include <GuiConstantsEx.au3>
#Include <Timers.au3>

;---------------------------------------------------------------------------------------------------------------------------------------------------------------

Global $title = "Go2Sleep"
Global $gui_dim[2] = [300,250]
Global $button_dim[2] = [90,30]
Global $go_button_name = "GO"
Global $exit_button_name = "Exit"
Global $go_button_axis[2] = [180,140]
Global $exit_button_axis[2] = [180,190]
Global $slider_axis[2] = [10,40]
Global $slider_dim[2] = [230,40]
Global $slider_max_min[2] = [300,1]
Global $slider_default = 10
Global $label1_text = "10"
Global $label1_axis[2] = [$slider_dim[0]+20, $slider_axis[1]]
Global $label2_text = "Adjust time to take action (in minutes)"
Global $label2_axis[2] = [10,10]
Global $checkbox1_text = "Show small upper-right frame with countdown"
Global $checkbox1_axis[2] = [10,80]
Global $checkbox1_default = $GUI_CHECKED
Global $radio1_text = "Shutdown"
Global $radio2_text = "Reboot"
Global $radio3_text = "Standby"
Global $radio4_text = "Hibernate"
Global $radio_axis[2] = [10,120]
Global $radio_space = 30
Global $splash_dim[2] = [60,40]
Global $splash_axis[2] = [@DesktopWidth-$splash_dim[0], 0]
Global $mb1_title = "All set"
Global $mb1_text[2] = ["After pressing ""OK"", action will be taken in "," minute(s)"]
Global $shutdown_code[4] = [29,22,32,64]
Global $refresh = 50

;---------------------------------------------------------------------------------------------------------------------------------------------------------------

GUICreate($title, $gui_dim[0], $gui_dim[1])
$go_button = GUICtrlCreateButton($go_button_name, $go_button_axis[0], $go_button_axis[1], $button_dim[0], $button_dim[1])
$exit_button = GUICtrlCreateButton($exit_button_name, $exit_button_axis[0], $exit_button_axis[1], $button_dim[0], $button_dim[1])

$slider = GUICtrlCreateSlider($slider_axis[0], $slider_axis[1], $slider_dim[0], $slider_dim[1])
GUICtrlSetLimit($slider, $slider_max_min[0], $slider_max_min[1])
GUICtrlSetData($slider, $slider_default)

$label1 = GUICtrlCreateLabel($label1_text, $label1_axis[0], $label1_axis[1])
$label2 = GUICtrlCreateLabel($label2_text, $label2_axis[0], $label2_axis[1])

$checkbox1 = GuiCtrlCreateCheckBox($checkbox1_text,$checkbox1_axis[0],$checkbox1_axis[1])
GUICtrlSetState($checkbox1, $checkbox1_default)

$radio1 = GUICtrlCreateRadio($radio1_text, $radio_axis[0], $radio_axis[1]+0*$radio_space)
$radio2 = GUICtrlCreateRadio($radio2_text, $radio_axis[0], $radio_axis[1]+1*$radio_space)
$radio3 = GUICtrlCreateRadio($radio3_text, $radio_axis[0], $radio_axis[1]+2*$radio_space)
$radio4 = GUICtrlCreateRadio($radio4_text, $radio_axis[0], $radio_axis[1]+3*$radio_space)
GUICtrlSetState($radio1, $GUI_CHECKED)

GUISetState()

While 1
    GUICtrlSetData ($label1, GUICtrlRead($slider))
    $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $go_button
                go()
            Case $msg = $exit_button
                exit
        EndSelect
    sleep($refresh)
WEnd

Func go()
    $opt1 = GUICtrlRead($slider)
    $opt2 = GUICtrlRead($checkbox1)

    Select
        Case GUICtrlRead($radio1) = 1
            $opt3 = $shutdown_code[0]
        Case GUICtrlRead($radio2) = 1
            $opt3 = $shutdown_code[1]
        Case GUICtrlRead($radio3) = 1
            $opt3 = $shutdown_code[2]
        Case GUICtrlRead($radio4) = 1
            $opt3 = $shutdown_code[3]
    EndSelect

    GuiDelete()

    MsgBox(0,$mb1_title,$mb1_text[0] & $opt1 & $mb1_text[1])

    $time = $opt1 * 60000

    If $opt2 = 4 Then
            sleep($time)
            Shutdown($opt3)
        Else
            $starttime = _Timer_Init()
            SplashTextOn("SplashStatic1",$opt1 - Int((_Timer_Diff($starttime)/60000)),$splash_dim[0], $splash_dim[1], $splash_axis[0], $splash_axis[1], 1)

            While 1
                $show_time = $opt1 - Int((_Timer_Diff($starttime)/60000))
                $real_time = $time - _Timer_Diff($starttime)
                If $real_time < 0 then
                    Shutdown($opt3)
                EndIf
                ControlSetText("SplashStatic1", "", "Static1", $show_time)
                sleep($refresh)
            WEnd
    EndIf
EndFunc

I would be grateful for any feedback.

go2sleep_english.au3

Link to comment
Share on other sites

I wrote this program because I often fall asleep while listening music from my laptop.

Ya, good job. You know, that's pretty much exactly why I wrote this, and because I sometimes don't fall asleep, the script is never done..:unsure:

Edited by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Link to comment
Share on other sites

Nice, I like how small the window is and that all shutdown options are available.

For what it's worth I never uploaded my concept, I call it Mr.Sleepy:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstants.au3>
#include <StaticConstants.au3>
#include <UpdownConstants.au3>
#include <EditConstants.au3>

;~ Coded by Ian Maxwell (llewxam)

Local $Hour, $Minute, $ShutdownType, $Status, $MainWindowWidth = 500, $MainWindowHeight = 200, $SleepType = "When"

_GetCurrentTime()

$MainWindow = GUICreate("Mr.Sleepy", $MainWindowWidth, $MainWindowHeight, (@DesktopWidth / 2) - ($MainWindowWidth / 2), (@DesktopHeight / 2) - ($MainWindowHeight / 2))
GUISetFont(10)
$Sleep = GUICtrlCreateGroup("Sleep Settings", 10, 10, 330, 75)
$SleepWhen = GUICtrlCreateRadio("Sleep At Specified Time", 20, 30)
$SleepIn = GUICtrlCreateRadio("Sleep In Specified Duration", 20, 50)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlSetState($SleepWhen, $GUI_CHECKED)
$HourLabel = GUICtrlCreateLabel("Hour", 250, 20, 40, Default, $SS_CENTER)
$MinuteLabel = GUICtrlCreateLabel("Min", 290, 20, 40, Default, $SS_CENTER)
$SleepInputHour = GUICtrlCreateInput(StringFormat("%.2i", $Hour), 250, 40, 40, Default, BitAND($ES_READONLY, $SS_CENTER))
$SleepInputMinute = GUICtrlCreateInput(StringFormat("%.2i", $Minute), 290, 40, 40, Default, BitAND($ES_READONLY, $SS_CENTER))
$SleepHourUpDown = GUICtrlCreateUpdown($SleepInputHour, $UDS_ALIGNLEFT)
$SleepMinuteUpDown = GUICtrlCreateUpdown($SleepInputMinute, $UDS_ALIGNRIGHT)
$Shutdown = GUICtrlCreateGroup("Shutdown Type", 380, 10, 110, 80)
$ShutdownHibernate = GUICtrlCreateRadio("Hibernate", 390, 30)
$ShutdownShutdown = GUICtrlCreateRadio("Shutdown", 390, 50)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlSetState($ShutdownHibernate, $GUI_CHECKED)
$Set = GUICtrlCreateButton("SET", 225, 100, 50)
$Status = GUICtrlCreateLabel("No sleep time set", 10, 150, 480, Default, $SS_CENTER)
GUISetState(@SW_SHOW, $MainWindow)

_Main()

Func _Main()
    Do
        Sleep(50)

        $Hour = GUICtrlRead($SleepInputHour)
        $Minute = GUICtrlRead($SleepInputMinute)
        _FormatDisplayedTime()
        GUICtrlSetData($SleepInputHour, StringFormat("%.2i", $Hour))
        GUICtrlSetData($SleepInputMinute, StringFormat("%.2i", $Minute))

        $MSG = GUIGetMsg()
        If $MSG == $GUI_EVENT_CLOSE Then Exit
        If $MSG == $Set Then
            If $SleepType == "When" Then
                $Hour = GUICtrlRead($SleepInputHour)
                $Minute = GUICtrlRead($SleepInputMinute)
            EndIf
            If $SleepType == "In" Then
                $Hour = @HOUR
                $Minute = @MIN
                $Added = GUICtrlRead($SleepInputMinute) + (GUICtrlRead($SleepInputHour) * 60)
                $Minute += $Added
                If $Minute >= 60 Then
                    Do
                        $Hour += 1
                        $Minute -= 60
                    Until $Minute < 60
                EndIf
                If $Hour > 23 Then $Hour -= 24
            EndIf
            If GUICtrlRead($ShutdownHibernate) == 1 Then $ShutdownType = "hibernate"
            If GUICtrlRead($ShutdownShutdown) == 1 Then $ShutdownType = "shutdown"
            GUICtrlSetData($Status, "Timer set for " & StringFormat("%.2i", $Hour) & ":" & StringFormat("%.2i", $Minute) & " by " & $ShutdownType)
            GUICtrlDelete($Set)
            GUICtrlSetState($Sleep, $GUI_DISABLE)
            GUICtrlSetState($SleepWhen, $GUI_DISABLE)
            GUICtrlSetState($SleepIn, $GUI_DISABLE)
            GUICtrlSetState($SleepInputHour, $GUI_DISABLE)
            GUICtrlSetState($SleepInputMinute, $GUI_DISABLE)
            GUICtrlSetState($SleepHourUpDown, $GUI_DISABLE)
            GUICtrlSetState($SleepMinuteUpDown, $GUI_DISABLE)
            GUICtrlSetState($HourLabel, $GUI_DISABLE)
            GUICtrlSetState($MinuteLabel, $GUI_DISABLE)
            GUICtrlSetState($Shutdown, $GUI_DISABLE)
            GUICtrlSetState($ShutdownHibernate, $GUI_DISABLE)
            GUICtrlSetState($ShutdownShutdown, $GUI_DISABLE)
            AdlibRegister("_CheckTime", 8000)
            _CheckTime()
        EndIf

        If $SleepType = "When" And GUICtrlRead($SleepIn) == 1 Then
            $Hour = 0
            $Minute = 15
            GUICtrlSetData($SleepInputMinute, StringFormat("%.2i", $Minute))
            GUICtrlSetData($SleepInputHour, StringFormat("%.2i", $Hour))
            $SleepType = "In"
        EndIf
        If $SleepType = "In" And GUICtrlRead($SleepWhen) == 1 Then
            _GetCurrentTime()
            GUICtrlSetData($SleepInputMinute, StringFormat("%.2i", $Minute))
            GUICtrlSetData($SleepInputHour, StringFormat("%.2i", $Hour))
            $SleepType = "When"
        EndIf
    Until 1 = 2
EndFunc   ;==>_Main

Func _GetCurrentTime()
    $Minute = @MIN
    $Hour = @HOUR
    If $Minute > 0 And $Minute < 15 Then
        $Minute = 15
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
    If $Minute > 15 And $Minute < 30 Then
        $Minute = 30
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
    If $Minute > 30 And $Minute < 45 Then
        $Minute = 45
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
    If $Minute > 45 And $Minute < 60 Then
        $Hour += 1
        $Minute = 0
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
EndFunc   ;==>_GetCurrentTime

Func _FormatDisplayedTime()
;~  Keep the displayed time in 15 minute intervals
    If $Minute == 1 Then
        $Minute = 15
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
    If $Minute == 16 Then
        $Minute = 30
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
    If $Minute == 31 Then
        $Minute = 45
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
    If $Minute > 45 Then
        $Hour += 1
        $Minute = 0
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
    If $Minute < 0 Then
        $Hour -= 1
        $Minute = 45
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
    If $Minute == 44 Then
        $Minute = 30
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
    If $Minute == 29 Then
        $Minute = 15
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
    If $Minute == 14 Then
        $Minute = 0
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
    If $Hour > 23 Then
        $Hour = 0
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
    If $Hour < 0 Then
        $Hour = 23
        ControlFocus("Mr.Sleepy", "", $Status)
    EndIf
EndFunc   ;==>_FormatDisplayedTime

Func _CheckTime()
    If $Hour == @HOUR And $Minute == @MIN Then
        AdlibUnRegister()
        If $ShutdownType == "hibernate" Then Shutdown(64)
        If $ShutdownType == "shutdown" Then Shutdown(4 + 1 + 8)
        $Set = GUICtrlCreateButton("SET", 225, 100, 50)
        GUICtrlSetState($Sleep, $GUI_ENABLE)
        GUICtrlSetState($SleepWhen, $GUI_ENABLE)
        GUICtrlSetState($SleepIn, $GUI_ENABLE)
        GUICtrlSetState($SleepInputHour, $GUI_ENABLE)
        GUICtrlSetState($SleepInputMinute, $GUI_ENABLE)
        GUICtrlSetState($SleepHourUpDown, $GUI_ENABLE)
        GUICtrlSetState($SleepMinuteUpDown, $GUI_ENABLE)
        GUICtrlSetState($HourLabel, $GUI_ENABLE)
        GUICtrlSetState($MinuteLabel, $GUI_ENABLE)
        GUICtrlSetState($Shutdown, $GUI_ENABLE)
        GUICtrlSetState($ShutdownHibernate, $GUI_ENABLE)
        GUICtrlSetState($ShutdownShutdown, $GUI_ENABLE)
        _GetCurrentTime()
        _FormatDisplayedTime()
        GUICtrlSetData($SleepInputHour, StringFormat("%.2i", $Hour))
        GUICtrlSetData($SleepInputMinute, StringFormat("%.2i", $Minute))
        GUICtrlSetData($Status, "No Sleep Time Set")
    EndIf
EndFunc   ;==>_CheckTime

My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.
Link to comment
Share on other sites

Thanks everyone for comments :unsure: I wanted to share this script, because I knew that it would be useful in everyday work on computer.

@llewxam

Wow! Great script. Mr. Sleepy looks much more sophisticated than my go2sleep. Maybe I'll add "on time" fuction as well.

Link to comment
Share on other sites

Mr. Sleepy looks much more sophisticated than my go2sleep. Maybe I'll add "on time" fuction as well.

It certainly wasn't put on your thread to brag, just so you could see someone else's ideas. I love your TextBox in the top-right showing the countdown, awesome idea that I am going to borrow and put in my script!! Everyone here can learn from each other, so thanks for posting!

Ian

My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.
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...