Question: I want to create a "PolyEdit" script. 
Below is the script. 

```
!M::
IfWinActive, ahk_class TMainForm
{
sleep 2000
Send Now is the time
Return
}

```

The purpose of the script is to:

Send keystrokes and mouse clicks to my default program for opening text files.
That default program is called "PolyEdit".

But I need the script to run without a hotkey being defined.

Right now, in it's present form, with a hotkey defined, it runs just fine.

My question is:

How can I make the script run automatically, without a hotkey being defined?

Answer: 
As Armin already wrote, use #Persistent. Also, If you want to create hotkeys that are only active when a specific application is in focus you can do the following: In this case the script will no longer execute on startup, only when you press the hotkey though...

```
#Persistent
#SingleInstance
#IfWinActive, ahk_class TMainForm
!M::
    sleep 2000
    Send Now is the time
Return
!n::SoundBeep, 500, 500
!o::MsgBox, OK
#IfWinActive

```

This way all 3 (dummy) hotkeys will only be active when your application is in focus! You can define the same hotkeys for another application, just repeat the code but use the ID if the other application in the `#IfWinActive, ahk_class TMainForm` line.

If you want to send a message every 2 seconds when your application is active do the following:

```
#Persistent
#SingleInstance
SetTimerMatchMode, CheckApp, 2000
Return

CheckApp:
IfWinActive, ahk_class TMainForm
{
    Send, Now is the time
}
Return

```

If you want to execute a script every time you (re)activate (put in focus) your application (so not every two seconds) then use the following:

```
#Persistent
#installKeybdHook
#SingleInstance

Gui +LastFound 
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,Hwnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return

ShellMessage( wParam )
{
    If (wParam = 4)
    {
        IfWinActive ahk_class TMainForm
        {
            Send, Now is the time
        }
    }
}
Return

```
Take a look at [#Persistent](http://www.autohotkey.com/docs/commands/_Persistent.htm) which will keep script running.

*If this directive is present anywhere in the script, that script will stay running after the auto-execute section (top part of the script) completes*