[Batch] Need help on Shutdown Timer

Discussion in 'Scripting' started by gamepoint, Dec 31, 2010.

  1. gamepoint

    gamepoint MDL Senior Member

    Jan 4, 2010
    457
    59
    10
    OK..How can i make this clear?

    I love to listen to a song until i fall asleep in the night.But i don't like to turn on my laptop even after the playlist have finished.

    Let say my Playlist is only 1 hour, after i fall asleep, my laptop is still on and wasting my electricity bill. If i use shutdown timer, the shutdown timer shuts down my computer without closing my iTunes first.Is there any way i can create a script to safely close iTunes first before shutting down my laptop?:confused:
     
  2. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    #2 Calistoga, Dec 31, 2010
    Last edited by a moderator: Apr 20, 2017
    AutoIt is your answer, dude.

    Code:
    While @HOUR < 12
    Sleep(500); Check the clock two times per second
    WEnd
    
    ; When the time is right,
    
    ProcessClose("itunes.exe"); This will close the process as if you clicked the X in the upper right corner of the iTunes window.
    
    Exit
    I really have to get going now, but I would have added some error checking in there, etc. If you want to make use of this approach then I'll be happy to assist you writing a proper script.

    Later dude :)
     
  3. gamepoint

    gamepoint MDL Senior Member

    Jan 4, 2010
    457
    59
    10
    #3 gamepoint, Dec 31, 2010
    Last edited: Dec 31, 2010
    (OP)
    thanks for the reply calistoga.Will check it rite now

    update : so how do i find or run the script? Is it the script that u've posted above?save as .au3 files in notepad?
     
  4. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    #4 Calistoga, Jan 1, 2011
    Last edited by a moderator: Apr 20, 2017
    Yeah, Notepad is fine, but I would recommend using the SciTE editor that I've mentioned here (follow the steps in 1.2 if you haven't done already). It's got syntax highlighting, code completion etc :)

    This is one of the functions you might find useful:
    Code:
    Shutdown(1);
    Where the numerical value means:
    Code:
    0 = Logoff
    1 = Shutdown
    2 = Reboot
    4 = Force
    8 = Power down
    32= Standby
    64= Hibernate
    If you want to know about a specific function, I can recommend the AutoIt Help File (F1 in SciTe) as it is very complete and well written.

    Please do ask if you got any questions :)
     
  5. gamepoint

    gamepoint MDL Senior Member

    Jan 4, 2010
    457
    59
    10
    #5 gamepoint, Jan 1, 2011
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Code:
    While @HOUR < 12
    Sleep(500); Check the clock two times per second
    WEnd
    
    ; When the time is right,
    
    ProcessClose("itunes.exe"); This will close the process as if you clicked the X in the upper right corner of the iTunes window.
    
    Exit
    "when the time is right"..i might have confused a bit there
     
  6. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    #6 Calistoga, Jan 1, 2011
    Last edited by a moderator: Apr 20, 2017
    This loop will run as long as @HOUR is less than 12 (ex 12 PM). The Sleep() thingy in there is because we don't want to make the CPU run at 100% while processing the loop.

    This code is evaluating two times per second (because of Sleep(500), where 500 milliseconds = 1/2 sec.
    Code:
    While @HOUR < 12
    Sleep(500); Check the clock two times per second
    WEnd
    
    With this I mean, "when the clock has reached 12 or any other desired point in time" (when the above loop has finished beacause the clock reached 12). You could say we're pausing the program, until you want it to end itunes and shut down the computer.
    Code:
    ; When the time is right,
    
    ProcessClose("itunes.exe"); This will close the process as if you clicked the X in the upper right corner of the iTunes window.
    This one isn't really necessary, as the script will end anyways, but I usually keep it in there for readability ;)
    Code:
    Exit
     
  7. gamepoint

    gamepoint MDL Senior Member

    Jan 4, 2010
    457
    59
    10
    thanks man.will try it later.Thank you so much.haha
     
  8. wulai

    wulai MDL Novice

    Mar 24, 2011
    5
    0
    0
    While @HOUR < 12
    Sleep(500); Check the clock two times per second
    WEnd


    12 in the morning or evening? if the time should be specified clearly?
     
  9. jlgager

    jlgager MDL Developer

    Oct 30, 2009
    365
    3,230
    10
    it works by 24 hour time so 12 would be noon and 24 would be midnight
     
  10. ricktendo64

    ricktendo64 MDL Expert

    Apr 20, 2008
    1,397
    2,024
    60
    #10 ricktendo64, Mar 25, 2011
    Last edited by a moderator: Apr 20, 2017
    Dude, the Windows Task Sheduler is all you need...you can execute any command including taskkill.exe and shutdown.exe

    I have a timer if I fall asleep my laptop will shut off at a certain time

    Code:
    schtasks /create /sc DAILY /tn "Power Off" /tr "shutdown /s /t 60 /c \"Your PC will shutdown in approximately 1 minute.\"" /st 05:00 /f
    You can also add other stuff to execute like taskkill.exe to kill any processes (I recommend process.exe its very good a closing apps, less forceful)

    Code:
    process.exe -q <task_name>.exe