9

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

Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
  • Related: [Automatically reload AutoHotkey script when modified](https://stackoverflow.com/q/45468335/3357935) – Stevoisiak Aug 10 '17 at 23:59

5 Answers5

19

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
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Robert Ilbrink
  • 7,738
  • 2
  • 22
  • 32
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
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% 
0

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

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

Cactus
  • 1
  • 1
  • 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](/help/how-to-answer). – Community Mar 07 '23 at 00:31
-2

I asked Chat GPT to write a batch file that will kill all AHK and reload specific ahk scripts. The script works great. I used Microsoft Keyboard setup to run this script when I press button #1 using the favorites button.

@echo off @echo off

set SCRIPT_LOCATION=f:\dropbox\autohotkey if not exist %SCRIPT_LOCATION% set SCRIPT_LOCATION=c:\dropbox\autohotkey

start c:\sysinternals\pskill AutoHotkey.exe timeout 1 start c:\sysinternals\pskill autohotkeyU32.exe timeout 1 start c:\sysinternals\pskill autohotkeyU64.exe timeout 1 start c:\sysinternals\pskill autohotkeyux.exe

start "" "%SCRIPT_LOCATION%\bbb-1-myahkdefault.ahk" timeout 1 start "" "%SCRIPT_LOCATION%\bbb-getactiveur2-works.ahk" timeout 1 start "" "%SCRIPT_LOCATION%\WindowSnipping.ahk" timeout 1 rem start "" "%SCRIPT_LOCATION%\WindowSnipping.ahk"