AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

AHK-Scheduler
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Boskoop



Joined: 01 Jan 2005
Posts: 71

PostPosted: Sat Sep 02, 2006 11:57 am    Post subject: AHK-Scheduler Reply with quote



AHK Scheduler is some sort of Window's Scheduled Tasks clone, completely written in AHK. The scheduler
  • Survives a restart
  • Can be uses with accounts without password (unlike Windows Scheduled Tasks)
  • Can optionally wake the computer up from standby mode to execute a task
  • It's possible to schedule more than one task
.

The scripts are testet on XPHome SP1 and XP Pro SP2. It should work on Windows98, Windows 2000 too. It's tested with AHK 1.0.44.7, but it should work with v1.0.36.07+. Wake up from Hibernate requires hardware support for S4-mode. You can find more info about standby-modes here

AHK-scheduler consists of to scripts. Both scripts must be in the same directory:

1. The GUI Script
The GUI script sets the timer and starts the timer script.
Code:
WakeUpMain=WakeUp.ahk

;GUI
Gui, Add, GroupBox, x10 y8 w150 h126, Application:
Gui, Add, Text, xp+6 y27 w60 h20, StartDate:
Gui, Add, Text, xp y57 wp hp , StartTime:
Gui, Add, DateTime, x70 y27 wp20 h20 vStartDate Section,
Gui, Add, DateTime, xp y57 wp hp vStartTime 1, HH:mm:00 ;time

Gui, Add, Text, x16 y84 w80 h20 , Run (with path):
Gui, Add, ComboBox, xp yp+16 w134 choose1 vStart, sol.exe|notepad.exe|cmd.exe

Gui, Add, GroupBox, x10 y140 w150 h60 , Wake up from Standby?
Gui, Add, Radio, xp+25 yp+25 vWakeUp, &No
Gui, Add, Radio, xp+55 yp Checked, &Yes

Gui, Add, GroupBox, x170 y8 w150 h126, Power Management
Gui, Add, Radio, xp+36 y27 w80 h26 Checked, &Keep running
Gui, Add, Radio, xp y+10 wp hp vStandby, &Hibernate NOW
Gui, Add, Radio, xp y+10 wp hp , &Suspend NOW

Gui, Add, Button, x170 y160 w48, &Help
Gui, Add, Button, xp+50 yp wp, &Cancel
Gui, Add, Button, xp+50 yp wp Default, &OK
Gui, Show,, AHK-Scheduler
Return



; SUBROUTINES
ButtonOK:
   Gui, submit, nohide

   ; Format date and time for wake-up-function
   Stringmid, YYYY,StartDate,1,4   ;Year
   Stringmid, MM,StartDate,5,2   ;Month
   Stringmid, DD,StartDate,7,2   ;Day
   Stringmid, Hour,StartTime,9,2   ;hour
   Stringmid, Min,StartTime,11,2 ;minute

   ;Add "" to application name
   Start="%Start%"


   ; Prepare code for standby-type: 0=no standby 1= hibernate 2= suspend (Radio Buttons in a group are numbered!)
   Standby:=Standby-1

   ; Prepare code for wake-up: 0=no wake up 1= wake up
   Resume:=WakeUp-1
   
   ; Set Timer by running timer script
   run, autohotkey.exe %WakeUpMain% %YYYY% %MM% %DD% %Hour% %Min% %Standby% %Start% %Resume%
   return

Esc::
ButtonCancel/Exit:
GuiClose:
   ExitApp   

ButtonHelp:
   MsgBox, Help file is under construction.
   return


2. The Timer Script
Code:
/*

Writes a timer script to Startup-directory. The Wake-Up-Timer script deletes itself when the timer is finished
Hibernates the computer, depending on variable "Hibernate"

*/


; *************
; CONFIGURATION
; *************

YYYY=%1%            ;Parameter 1: Year 
MM=%2%               ;Parameter 2: Month
DD=%3%               ;Parameter 3: Day
Hour=%4%            ;Parameter 4: hour
Min=%5%               ;Parameter 5: minute

Hibernate=%6%         ;Parameter 6: Hibernate, suspend, keep running?
Start=%7%            ;Parameter 7: Application to run
Resume=%8%            ;Parameter 8; Resume to run application? Yes/No

ScheduledTime=%YYYY%%MM%%DD%%Hour%%Min%%A_sec%

; ***********
; AUTOEXECUTE
; ***********

;Get name of application to be started from it's path:
SplitPath, Start, FileName
StringReplace, FileName,FileName,", ;Remove "

;Writes the timer script to Startup-directory
WriteAutostartFile(YYYY,MM,DD,Hour,Min,Start,Resume,FileName)

;Starts the timer from Startup-directory
Run, %A_startup%\%ScheduledTime%_%Filename%.ahk

;Hibernates the computer, depending on variable "Hibernate":
   If Hibernate=1       ;Hibernate
         {
         DllCall("PowrProf\SetSuspendState", "int", 1, "int", 0, "int", 0)
         }

   If Hibernate=2      ;Suspend
         {
         DllCall("PowrProf\SetSuspendState", "int", 0, "int", 0, "int", 0)
         }


; *********
; FUNCTIONS
; *********

WriteAutostartFile(YYYY,MM,DD,Hour,Min,Start,Resume,FileName)
; Writes Wake-Up-Timer script to Startup directory . After a restart the script will run again and set the timer.
; The Wake-Up-Timer script deletes itself when the timer is finished or when duetime is over at startup
{
   ScheduledTime=%YYYY%%MM%%DD%%Hour%%Min%%A_sec%
   FileAppend,
   (
   YYYY=%YYYY%            
   MM=%MM%               
   DD=%DD%               
   Hour=%Hour%            
   Min=%Min%            
   Start=%Start%            ; Application to run
   Resume=%Resume%            ; Resume to run application? Yes/No
   
   ScheduledTime=%YYYY%%MM%%DD%%Hour%%Min%%A_sec%
   Name=%A_Now%            ;Name of the Timer Object
   
   If (A_Now>ScheduledTime)
   {
         ;x := (A_Now-%ScheduledTime%)
;         msgbox, `%A_Now`%`(A_Now)``n%ScheduledTime% (scheduledTime)``n`%x`%
;         exitApp
      msgbox, The application ``n     %FileName% ``ncould not be started as scheduled``n     %YYYY%-%MM%-%DD% %Hour%:%Min%
      FileDelete, %A_startup%\%ScheduledTime%_%Filename%.ahk
      exitApp
   }
   else
   {
      Menu, tray, tip,  Wake-Up-Timer``nStart Time: %YYYY%-%MM%-%DD% %Hour%:%Min%``nApplication: %FileName%
      WakeUp(YYYY, MM, DD, Hour, Min, Resume, Name)
      run, %Start%
      FileDelete, %A_startup%\%ScheduledTime%_%Filename%.ahk
      return
   }   

   ; FUNCTIONS
   WakeUp(Year, Month, Day, Hour, Minute, Resume, Name)
   ;Awaits duetime, then returns to the caller (like some sort of "sleep until duetime").
   ;If the computer is in hibernate or suspend mode
   ;at duetime, it will be reactivated (hardware support provided)
   ;Parameters: Year, Month, Day, Hour, Minute together produce duetime
   ;Resume: If Resume=1, the system is restored from power save mode at due time
   ;Name: Arbitrary name for the timer
   {
      duetime:=GetUTCFileTime(Year, Month, Day, Hour, Minute)

      Handle:=DLLCall("CreateWaitableTimer"
            ,"char *", 0
            ,"Int",0
            ,"Str",name, "UInt")

      DLLCall("CancelWaitableTimer","UInt",handle)

      DLLCall("SetWaitableTimer"
           ,"Uint", handle
           ,"Int64*", duetime        ;duetime must be in UTC-file-time format!
           ,"Int", 1000
           ,"uint",0
           ,"uint",0
           ,"int",resume)



      Signal:=DLLCall("WaitForSingleObject"
            ,"Uint", handle
            ,"Uint",-1)

      DllCall("CloseHandle", uint, Handle)   ;Closes the handle
   }


   GetUTCFiletime(Year, Month, Day, Hour, Min)
   ;Converts "System Time" (readable time format) to "UTC File Time" (number of 100-nanosecond intervals since January 1, 1601 in  Coordinated Universal Time UTC)
   {
      DayOfWeek=0

      Second=00
      Millisecond=00


      ;Converts System Time to Local File Time:
      VarSetCapacity(MyFiletime  , 64, 0)
      VarSetCapacity(MySystemtime, 32, 0)

      InsertInteger(Year,       MySystemtime,0)
      InsertInteger(Month,      MySystemtime,2)
      InsertInteger(DayOfWeek,  MySystemtime,4)
      InsertInteger(Day,        MySystemtime,6)
      InsertInteger(Hour,       MySystemtime,8)
      InsertInteger(Min,        MySystemtime,10)
      InsertInteger(Second,     MySystemtime,12)
      InsertInteger(Millisecond,MySystemtime,14)

      DllCall("SystemTimeToFileTime", Str, MySystemtime, UInt, &MyFiletime)
      LocalFiletime := ExtractInteger(MyFiletime, 0, false, 8)

      ;Converts local file time to a file time based on the Coordinated Universal Time (UTC):
      VarSetCapacity(MyUTCFiletime  , 64, 0)
      DllCall("LocalFileTimeToFileTime", Str, MyFiletime, UInt, &MyUTCFiletime)
      UTCFiletime := ExtractInteger(MyUTCFiletime, 0, false, 8)

      Return UTCFileTime
   }


   ExtractInteger(ByRef pSource, pOffset = 0, pIsSigned = false, pSize = 32)
   ; Documented in Autohotkey Help
   {
      Loop `%pSize`% 
         result += *(&pSource + pOffset + A_Index-1) << 8*(A_Index-1)
      if (!pIsSigned OR pSize > 4 OR result < 0x80000000)
         return result 
      return -(0xFFFFFFFF - result + 1)
   }


   InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4)
   ; Documentated in Autohotkey Help
   {
     Loop `%pSize`%
           DllCall("RtlFillMemory", UInt, &pDest + pOffset + A_Index-1
                 , UInt, 1, UChar, pInteger >> 8*(A_Index-1) & 0xFF)
   }
   
   ), %A_startup%\%ScheduledTime%_%Filename%.ahk
}




Edit 2006-09-03: Changed GUI (thanks Bobo)


Last edited by Boskoop on Sun Sep 03, 2006 6:38 pm; edited 1 time in total
Back to top
View user's profile Send private message
BoBo
Guest





PostPosted: Sat Sep 02, 2006 4:01 pm    Post subject: Reply with quote

Currently I try to reproduce how it feels like being toralf (while he's programming a Gui Wink ... ahhh it must be heaven Laughing). So I've abused your Gui code to start. Hope you don't mind. Take it that way "degustibus non est disputandum". Very Happy

Code:
Gui, Add, GroupBox, x10 y8 w150 h126, Application:
Gui, Add, Text, xp+6 y27 w60 h20, StartDate:
Gui, Add, Text, xp y57 wp hp , StartTime:
Gui, Add, DateTime, x70 y27 wp20 h20 vStartDate Section,
Gui, Add, DateTime, xp y57 wp hp vStartTime 1, HH:mm:00 ;time

Gui, Add, Text, x16 y84 w80 h20 , Run (with path):
Gui, Add, ComboBox, xp yp+16 w134 choose1 vStart, sol.exe|notepad.exe|cmd.exe

Gui, Add, GroupBox, x170 y8 w150 h126, Power Management
Gui, Add, Radio, xp+36 y27 w80 h26 Checked, &Keep running
Gui, Add, Radio, xp y+10 wp hp vStandby, &Hibernate NOW
Gui, Add, Radio, xp y+10 wp hp , &Standby NOW

Gui, Add, GroupBox, x170 y140 w150 h60 , Wake up from Standby?
Gui, Add, Radio, xp+25 yp+25 vWakeUp, &No
Gui, Add, Radio, xp+55 yp Checked, &Yes

Gui, Add, Button, x10 y160 w48 Default, &OK
Gui, Add, Button, xp+50 yp wp, &Help
Gui, Add, Button, xp+50 yp wp, &Cancel 
Gui, Show,, AHK-Scheduler
Return
Back to top
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10474

PostPosted: Sat Sep 02, 2006 5:00 pm    Post subject: Reply with quote

Nice-looking script and GUI. I'm sure it will be popular.
Back to top
View user's profile Send private message Send e-mail
toralf



Joined: 31 Jan 2005
Posts: 3841
Location: Bremen, Germany

PostPosted: Sat Sep 02, 2006 8:40 pm    Post subject: Reply with quote

BoBo wrote:
Currently I try to reproduce how it feels like being toralf (while he's programming a Gui :wink: ... ahhh it must be heaven :lol:).
:)
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
BoBo
Guest





PostPosted: Sun Sep 03, 2006 10:41 am    Post subject: Reply with quote

I thought it make sense to visualize the Gui version I've provided above. That way Gui noobs (like I am) can check the impact of changes I made if compared to the original one provided by Boskoop.

Back to top
Boskoop



Joined: 01 Jan 2005
Posts: 71

PostPosted: Sun Sep 03, 2006 6:40 pm    Post subject: Reply with quote

@Bobo: Thanks! Actually I like your GUI better than mine (GUI-design er something I just want to get over with). I changed the script accordingly (with a few changes, as I feel that the OK button must be placed in the lower right corner). By the way- I could use a nice icon too Wink

@Chris: Thank you for your nice comment.

I hope for feedback and suggestions.

Boskoop


Last edited by Boskoop on Sun Sep 03, 2006 6:44 pm; edited 1 time in total
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Mon Sep 04, 2006 10:40 am    Post subject: Reply with quote

Boskoop wrote:
I changed the script accordingly (with a few changes, as I feel that the OK button must be placed in the lower right corner).
That's not conforming to Windows' GUI guidelines... That's why users will be confused. I would put the OK and Cancel buttons together, too (for BoBo...).
Well, we have the source, we can change that, that's not a biggie.

I didn't knew the WaitableTimer API, that's an interesting one. Thanks for sharing.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
BoBo
Guest





PostPosted: Mon Sep 04, 2006 11:06 am    Post subject: Reply with quote

Quote:
That's not conforming to Windows' GUI guidelines...
Where I can get those?? Do you have a specific source/link? Thx in advance. Cool

Why I've done it that way ...
1) I thought it would make sense to trigger the Gui option clockwise (hm, would it make sense to do some eyetracking and therefore arrange options like you would read a text? So the buttons on arabic Guis are on the left, right? Or left on the right? Means left right, right? Oh, left left, huh?).
2) I thought that the majority of changes will be done top/left and so the bottom left allignment of buttons would make sense too as you can 'drop' straight forward to them.
3) I've separated the OK and Cancel button with the Help button by intention as it should minimize the risk to execute by mistake (a typicall mouse-pusher-desease Laughing)
Back to top
Boskoop



Joined: 01 Jan 2005
Posts: 71

PostPosted: Tue Sep 05, 2006 7:57 pm    Post subject: Reply with quote

I'm trying to find a simple way to shut running timers down. The waitable timer function blocks the script completely until the timer is signaled. That way neither hotkeys nor tray menu commands do work (the waiting timer script does not even show a tray menu).

This code
Code:
process, close, %PID%

closes the timer-script, but it must be executed from another script. That gets complicated when more than one timer is running. And it makes it impossible to abort the timer script directly on system shutdown.

Anybody who has a good idea how I can interact with that blocked script?

Thanks
Boskoop
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Wed Sep 06, 2006 8:39 am    Post subject: Reply with quote

BoBo wrote:
Quote:
That's not conforming to Windows' GUI guidelines...
Where I can get those?? Do you have a specific source/link? Thx in advance. Cool
Well, first, look at classical Windows applications... Note that Microsoft tends to break these rules with recent software, like using HTML forms for Visual Studio wizards or the Add/Remove Program dialog...

Next, I found back the related page:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/anch_uidesigndev.asp
There are links like "Official Guidelines for User Interface Developers and Designers" and "Windows User Interface".
The User Interface Text is rather important:
Quote:
Access Keys
Always define an access key for the label of any control or menu item. The only exceptions are the OK and Cancel buttons because the ENTER and ESC keys typically serve as the access keys to these buttons. Also, avoid having the same access key used by more than one control in a single context or more than one item on the same menu.

Quote:
Ellipses
If a command requires additional information to complete its intended action, and you use a dialog box to supply that information, follow the command with an ellipsis (…). The ellipsis provides a visual cue that the command requires additional information.
[...]
However, not every command that results in the display of a window should include an ellipsis. Do not include an ellipsis for commands that simply display a window or view or change the existing view within a window. Similarly, do not use ellipses for commands that display a collection of objects or options, unless the intended action requires that the user select or confirm the selection of one or more elements of the collection. Also, do not include an ellipsis for commands that may result in a confirming message box (like an About box).

_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Guest






PostPosted: Fri Sep 22, 2006 8:39 pm    Post subject: Reply with quote

Awsome script, I have been looking for something like this. I am still new to this language, and programming in general. Any way you could simplify this and make a small script to run a program a set time like 30min from the current time?

I ask this because I set your script up so it will run another one of mine that shut downs some programs for me, but I would like to be able to skip the GUI part and have one link to click to automatically launch my script 30min from now.
Back to top
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10474

PostPosted: Sat Sep 23, 2006 12:31 am    Post subject: Reply with quote

Quote:
run a program a set time like 30min from the current time?
If you want the ultimate in simplicity, something like:
Code:
Sleep 30 * 60 * 1000  ; 30 minutes worth of milliseconds.
Run Notepad.exe
Back to top
View user's profile Send private message Send e-mail
.AHK



Joined: 26 Apr 2006
Posts: 662
Location: USA

PostPosted: Sat Sep 23, 2006 1:02 am    Post subject: Reply with quote

Quote:
If you want the ultimate in simplicity, something like:


Or you could use my simple idea. I posted it in scripts and functions the link is below.

http://www.autohotkey.com/forum/viewtopic.php?t=11994&highlight=easy+milliseconds
Back to top
View user's profile Send private message Visit poster's website AIM Address
Micahs
Guest





PostPosted: Tue Nov 14, 2006 9:18 pm    Post subject: wake-up in win98 Reply with quote

@all

Howdy,
Does anybody have an idea how to make the wake-up function work in windows 98? The author says that it should, but I have not been able to get it to work.
Any commands after the WakeUp call execute immediately, with no delay. It does work correctly in xp, though.

Thanks,
Micahs
Back to top
also interested
Guest





PostPosted: Fri Nov 17, 2006 1:51 pm    Post subject: Re: wake-up in win98 Reply with quote

I can only offer this:
1. I checked the msdn site for info on "CreateWaitableTimer" and it
confirms that this function should work on win98.
2. It also state the the function will return "0" if the is a problem, and
by adding a debug statement after the call to "CreateWaitableTimer"
I found that "0" is being returned (not a valid handle).

That's about the limit of my ability to debug this, maybe one of the 'gurus'
here will help.

LOL
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group