10

Say I have an autohotkey script C:\path\to\my\script running. Is there a way to define a hotkey that re-starts it?

1

4 Answers 4

21

In order to prevent duplicate instances, I normally do not re-launch a script but use the build-in function Reload. I launch this with Ctrl+Win+Alt+R and use Ctrl+Win+Alt+E to edit the main AHK script.

^#!r::Reload

Actually, my script looks like this:

^#!r::
Send, ^s ; To save a changed script
Sleep, 300 ; give it time to save the script
Reload
Return

^!#e::Edit

As a matter of fact, all the way at the top of my script I have this to give me a visual and audio indication that the script was restarted:

#SingleInstance Force
#installKeybdHook
#Persistent
Menu, Tray, Icon , Shell32.dll, 25, 1
TrayTip, AutoHotKey, Started, 1
SoundBeep, 300, 150
Return
Sign up to request clarification or add additional context in comments.

Comments

1

Make a hotkey that runs a script, which in this case is the same script and then exit.

somehotkey::
    Run, C:\path\to\my\script.ahk
    ExitApp
return

3 Comments

In order to prevent multiple instances running at the same time (run creates a new instance), I use ^#!r::Reload
@RobertIlbrink. If that restarts the script, you should post that as an answer. Your solution may be better than Armin's if it avoids intance duplicates.
@RobertIlbrink Reload does exactly the same thing as my suggestion.
1

Restart the AutoHotkey script with a hotkey. Autohotkey 2.0 version:

#r:: ; press Win+r to reload
{ 
  msgResult := MsgBox("Do you really want to reload this script?")
  if (msgResult = "Yes")
    Reload()
  return
} 

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

I found this to be the safest option of them all, because it takes care that the correct script is reloaded when you have multiple scripts running simultaneously, which was a recurring issue for me. The combination of the following also ensures that only one instance of a script will ever run at a time. The ScriptFullPath variable includes the name of the script.

#SingleInstance Force ;put this at the top of the script
^r::run, %A_ScriptFullPath% 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.