AutoHotkey Script to Eject a Portable Drive (Like an iPod)

AutoHotkey comes to my aid, again. I recently came to realize I had a need to eject my iPod at 9:00am every day. There are two parts to this problem. You need a way to check the time, on some interval. And you need a way to tell windows to do the eject. While this is not terribley complicated, there is one catch. AutoHotkeys "eject" command only works on CD/DVD drives. The help file does have an example bit of code that does the job, though. Click below to read on... (By the way, I noticed a link to this sitepointing to my other AutoHotkey entry. Autohotkey.blogspot.com appears to be starting up a collection of pointers to useful scripts) So here is the code. First things first, you need to set a timer to kick off this "subroutine," then there is the big, ugly subroutine itself. Have a look, and I'll explain below. SetTimer, eject_drive, 5000 eject_drive: { hour=%A_Hour% if (hour=09) { Driveletter = E: ; Set this to the drive letter you wish to eject. hVolume := DllCall("CreateFile" , Str, "\\.\" . Driveletter , UInt, 0x80000000 | 0x40000000 ; GENERIC_READ | GENERIC_WRITE , UInt, 0x1 | 0x2 ; FILE_SHARE_READ | FILE_SHARE_WRITE , UInt, 0 , UInt, 0x3 ; OPEN_EXISTING , UInt, 0, UInt, 0) if hVolume <> -1 { DllCall("DeviceIoControl" , UInt, hVolume , UInt, 0x2D4808 ; IOCTL_STORAGE_EJECT_MEDIA , UInt, 0, UInt, 0, UInt, 0, UInt, 0 , UIntP, dwBytesReturned ; Unused. , UInt, 0) DllCall("CloseHandle", UInt, hVolume) } } } return
  1. First, SetTimer basically says, "run eject_ipod every 5 seconds" fairly straight forward.
  2. Next is the label "eject_ipod" which sets a starting point for our ejection instructions. It gives our SetTimer instruction a place to jump to. (I'm not sure how it's technically defined, but for those of you that are pretty techie, it is a label, not a function or subroutine in the strict programming sense - note that "return" follows the code, and is NOT inside a subroutine block.)
  3. Next, I set a variable called "hour" to be equal to the current hour. I don't care about the minutes here, but you could very well make a minor mod to this code and get more granular. AutoHotkey help has the details for you, just look up A_Min under builtin variables.
  4. Now the code checks to see if it is 9am. Note that I only check for a value of 9, not 9am. A_Hour uses a 24 hour clock, so 9pm is actually 21. So, if it is 9am, all of the stuff following the curly brace is run.
  5. Whether or not it is 9am, AutoHotkey eventually finds its way down to the "return" statement. Return tells autohotkey to go back to what it was doing before it was sent here to "eject_ipod" Basically, back to wait another 5 seconds and start this monotonous little process over again. Makes me appreciate my job a little more.
So, note that I skipped over the whole lot of crud in the middle, starting with hVolume. ( I put it in italics to set it apart) That is the code that does nothing more than tell windows to eject the drive. I did not mention it on purpose, partly because I don't understand the details, and don't want to. :) This was the chunk of code I copied right from the AutoHotkey help file. Working in Windows at that level is a bit of hell on earth, thank you Microsoft!