# Solved: Auto Hot Key Help Needed



## tmetford (Apr 8, 2008)

Hi,
I've recently discovered the programme "Auto Hot Key"
It's a great programme and I want to know how to do the following:

Get Auto Hot Key to execute a script at a specified time.
Get an Auto Hot Key script to load with Windows (XP) when it boots.
Thanks for your help.
Tim


----------



## stantley (May 22, 2005)

To execute a script at a specified time use the FormatTime command and an If statement like If TimeString = 8:00 PM do something.

To run a script at boot up time, put an ahk file in your Startup folder.


----------



## TheOutcaste (Aug 8, 2007)

1. Use the Task Scheduler to schedule a task. *Control Panel | Scheduled Tasks | New Task*. Select the script (or a shortcut to the script) as the program to execute.
2. Place a shortcut to the script in the startup folder (*Start | All Programs | Startup*)
Drag and drop the shortcut on that folder and it will start when the current user logs in.

If you want one script for all users, put it in the All Users startup folder:
Right click *Start*, click *Explore All Users*
Expand *Programs*, and drop your script in the *Startup* folder.

See the AutoHotKey FAQ

If you convert the scripts to exe files, they will work on PCs that don't have AutoHotKey installed.

HTH

Jerry


----------



## tmetford (Apr 8, 2008)

Thanks for that and I can get a script to run when Windows boots, that's fine, but I want to get the next bit of the script to execute an action at a specified time and I don't yet understand the Format Time command and its syntax.
For example, I want my PC to open a file (e.g. helpme.txt) for me at, say, 5pm.
How would I do this please?
Thanks
Tim


----------



## stantley (May 22, 2005)

Actually, TheOutcaste's suggestion of using Window's Task Scheduler is an easier way to set it up. Create a new task to run at 5:00 PM and when it asks which program to execute, click browse and put in the name of your script, say test.ahk.

Then test.ahk would have the commands without a hotkey to start it:

Run C:\Documents and Settings\_username_\My Documents\helpme.txt
Return

If you have Notepad associated with the .txt extension then Notepad will open that file. You can put the file anywhere, I just put an example of it being in My Documents to emphasize that you should put in the full path.


----------



## tmetford (Apr 8, 2008)

Hi, Task Scheduler would be fine BUT, it requires a login username & password which I prefer not to use on this PC. The reason is that this PC powers itself on each morning at 7am and remains running through the day fo me to operate remotely when I need to. If I set up a user account and a password on it, it wouldn't login properly and would make remote control more difficult (I guess you'll surprise me with an AutoHotKey workaround perhaps?).
Anyway. ideally at the moment, I'd like to have your help in writing a script to have Auto Hot Key open a particular AutoHotKey script at 9am and then wait until 5pm and run a different autohotkey script (these scripts are ones that I've already written and work fine).and then, finally, power off the PC (it runs Windows XP pro).
Would you be willing to coach me in the Time-related commands & syntax to help ,me achieve this please?
Many Thanks for your patience
Tim


----------



## stantley (May 22, 2005)

Well, the simplest way to do it is have the following script in the Startup folder:

#Persistent

Sleep, (60000 * 60 * 2) ;2 Hours

MsgBox, Here's where to put the first script

Sleep, (60000 * 60 * 8) ;8 Hours

MsgBox, Here's where to put the second script

Shutdown, 8

Return


So the PC starts up at 7:00 AM, the script waits for 2 hours, runs the first part, waits for 8 hours, runs the second and powers down the machine. Now it's not going to run exactly at 9:00 AM and 5:00 PM, it could be off a little bit. If it has to be exact you could try this:

#Persistent

Sleep, (60000 * 58 * 2) ; 4 Min. before 9:00 AM

Loop
{
FormatTime, TimeString,, Time
If TimeString = 9:00 AM 
{
MsgBox, Here's where to put the first script
Break
}
Sleep 20000
}

Sleep, (60000 * 59 * 8) ; 8 Min. before 5:00 PM 

Loop
{
FormatTime, TimeString,, Time
If TimeString = 5:00 PM 
{
MsgBox, Here's where to put the second script
Break
}
Sleep 20000
}

Shutdown, 8

Return

So it sleeps until a few minutes before 9:00 AM, then checks the time every 20 sec. and the same thing at 5:00. You'll have to test it out and tweak the times according to how long your scripts run.


----------



## tmetford (Apr 8, 2008)

Right, that looks as though it'll do the job, I'll try it out.
Now, just so that I understand how to put the script in correctly, would I simply replace the line:

MsgBox, Here's where to put the first script

with a script, e.g.

Run C:\AutoHotKey\helpme.txt

Is that right?
Thanks
Tim


----------



## TheOutcaste (Aug 8, 2007)

You don't need to use your main account for Task Scheduler. Either put a password on the Administrator account and use it, or create a 2nd account with a password to use just for running tasks. Your main account can remain without a password, and will still automatically log in (though some settings may need to be changed depending on your current setup)

If you are using the Administrator account as your main account (not a good idea) it will not appear on the Welcome screen if you create a 2nd account, This can be changed so it will appear as well.

Or use TweakUI to log into your account with a password.

HTH

Jerry


----------



## stantley (May 22, 2005)

tmetford said:


> Right, that looks as though it'll do the job, I'll try it out.
> Now, just so that I understand how to put the script in correctly, would I simply replace the line:
> 
> MsgBox, Here's where to put the first script
> ...


Yes, that's correct. The helpme.txt file will remain open indefinitely unless you give a command to close it.


----------



## tmetford (Apr 8, 2008)

Hi, Thank you, I'm beginning to grasp the "IF then Else" syntax and I'll run your script to see how it works. I'll play with Tweak UI too.
The final bit I want to sort out at this stage (promise I'll learn as fast as I can so I don't have to plague you with so many questions), is to have the PC run script "A" if it discovers it's a Saturday/Sunday, or, run script "B" at 9am and script "C" at 5pm if it discovers it's a Monday through Friday.
Hope that makes sense.
I really appreciate your help with all this especially now that I'm beginning to see some of the logic of this form of scripting.


----------



## stantley (May 22, 2005)

FormatTime, DayNumber,, WDay gives you the days of the week as numbers, 1 for Sunday through 7 for Saturday.


```
[SIZE="3"]#Persistent
FormatTime, DayNumber,, WDay
If ((DayNumber = 1 ) or (DayNumber = 7))
	WeekSection := Weekend
Else
	WeekSection := Weekday

Sleep, (60000 * 58 * 2) ; 4 Min. before 9:00 AM

Loop
{
	FormatTime, TimeString,, Time
	If TimeString = 9:00 AM
		{
		If (WeekSection = Weekend)
			{
			insert script "A" 
			}
		Else 
			{
			insert script "B" 
			}
		Break
		}
	Sleep 20000
}
Sleep, (60000 * 59 * 8) ; 8 Min. before 5:00 PM

Loop
{
	FormatTime, TimeString,, Time
	If TimeString = 5:00 PM
		{
		If (WeekSection = Weekday)
			{
			insert script "C" 
			}
		Break
		}
	Sleep 20000
}
Shutdown, 8
Return[/SIZE]
```
You said "run script "C" at 5pm if it discovers it's a Monday through Friday", so the way it's written it does nothing at 5pm on the weekend. If something else needs to run at 5pm on the weekend, add an Else like in the 9:00 part.

I wrote this in a way that you should be able to understand. It could be written more efficiently, but I'll leave that for you to ponder.


----------



## tmetford (Apr 8, 2008)

That's great, this is a really useful script for me, so thank you and I shall puzzle over a more efficient form of it in due course.
I need to iron out a problem though.
Below is script "A", it runs the Hyperterminal programme which then dials a telephone number that causes the Call Divert facility on the phone at my clinic to divert phone calls to my home. Unfortunately, whilst it does that job alright, it doesn't complete the job by closing the Hyperterminal application and the script remains loaded in memory (i.e. the AutoHotKey icon remains in the system tray at the bottom right of the screen).
Is there a way to get it to close Hyperterminal and unload itself?
I used the Auto Script Writer application to help create the script and I've tried using ExitApp, Exit, and Return at the very end of the script but this doesn't seem to make a difference.
Any ideas?

WinWait, Program Manager, FolderView
IfWinNotActive, Program Manager, FolderView, WinActivate, Program Manager, FolderView
WinWaitActive, Program Manager, FolderView
Run C:\Documents and Settings\Tim\Start Menu\Programs\Accessories\Communications\HyperTerminal\Divert to Home.ht
Sleep, 100
WinWait, Connect To, Enter details for th
IfWinNotActive, Connect To, Enter details for th, WinActivate, Connect To, Enter details for th
WinWaitActive, Connect To, Enter details for th
Send, {TAB 2}{ENTER 2}
SetWinDelay, 10000
WinWait, Connect, Dial Now
IfWinNotActive, Connect, Dial Now, WinActivate, Connect, Dial Now
WinWaitActive, Connect, Dial Now
Send, {TAB}{ENTER}
WinWait, Cancel Divert - HyperTerminal, Cnct/Discnct
IfWinNotActive, Cancel Divert - HyperTerminal, Cnct/Discnct, WinActivate, Cancel Divert - HyperTerminal, Cnct/Discnct
WinWaitActive, Cancel Divert - HyperTerminal, Cnct/Discnct
Send, {ALTDOWN}{F4}{ALTUP}
Return


----------



## stantley (May 22, 2005)

So it's sounds like the Alt-F4 command isn't working. You could try putting a sleep command after it.

WinWaitActive, Cancel Divert - HyperTerminal, Cnct/Discnct
Send, {ALTDOWN}{F4}{ALTUP}

Sleep, 1000

ExitApp

If you had the ExitApp after the Return, that would explain why it wasn't working. Try putting different values in the Sleep command.

I'm not sure why you have SetWinDelay, 10000 in there, 10 seconds seems like too long of a delay. Maybe change it to SetWinDelay, 1000 or just replace it with a sleep command.

You could also try a WinClose command or a WinKill command.

WinWaitActive, Cancel Divert - HyperTerminal, Cnct/Discnct
Send, {ALTDOWN}{F4}{ALTUP}
Sleep, 1000
WinClose, Cancel Divert - HyperTerminal, Cnct/Discnct
WinWaitClose, Cancel Divert - HyperTerminal, Cnct/Discnct
Sleep, 1000

ExitApp


----------



## tmetford (Apr 8, 2008)

Thank you for your help in creating a script that's got me started and my apologies for the delay in replying.
When I need more help I'll get back to you (which may be pretty soon!) but, for the mean time I think it'd be better that I close this case for now.
Kind regards
Tim


----------



## Squashman (Apr 4, 2003)

tmetford said:


> Hi, Task Scheduler would be fine BUT, it requires a login username & password which I prefer not to use on this PC.


That is incorrect. You can have a username and password on an account and still have it autologin!


----------



## tmetford (Apr 8, 2008)

Hi, Me again! How do you get a script that's marked PERSISTANT to close itself (i.e. unload from memory so that its system tray icon disappears) once it's done its job?
The script is as follows:

#Persistent 

Sleep, (60000 * 50) ;7.50am

Run C:\Documents and Settings\Metford & Francis\Start Menu\Programs\Accessories\Communications\HyperTerminal\Divert to Home.ht
Sleep, 100
WinWait, Connect To, Enter details for th
IfWinNotActive, Connect To, Enter details for th, WinActivate, Connect To, Enter details for th
WinWaitActive, Connect To, Enter details for th
Send, {TAB 2}{ENTER 2}
Sleep, 10000
WinWait, Connect, Dial Now
IfWinNotActive, Connect, Dial Now, WinActivate, Connect, Dial Now
WinWaitActive, Connect, Dial Now
Send, {TAB}{ENTER}
Sleep, 100
WinWait, Divert to Home - HyperTerminal, Cnct/Discnct
IfWinNotActive, Divert to Home - HyperTerminal, Cnct/Discnct, WinActivate, Divert to Home - HyperTerminal, Cnct/Discnct
WinWaitActive, Divert to Home - HyperTerminal, Cnct/Discnct
Send, {ALTDOWN}{F4}{ALTUP}
Sleep, 1000
Return
ExitApp

Thank you, your suggestions will be much appreciated.
Tim


----------



## stantley (May 22, 2005)

Take out Return.


----------



## tmetford (Apr 8, 2008)

Thank you, that works.


----------

