[solved] Possible to detect when script reloaded

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

[solved] Possible to detect when script reloaded

21 Jul 2015, 11:10

Hi.

Is there possible - preferably somewhere in the header of the script - to detect if script was reloaded, as oposed to a regular run?

I know that I probably could send a parameter (for each normal run) - but that would not always be true. Is there any other way around?


Thanks
Last edited by Almost_there on 22 Jul 2015, 10:17, edited 1 time in total.
User avatar
boiler
Posts: 2409
Joined: 21 Dec 2014, 02:44

Re: Possible to detect when script reloaded

21 Jul 2015, 16:48

You could set a value in an ini file before sending a Reload command, and the script could check that upon starting then set it back to normal.
MegaloDon
Posts: 107
Joined: 22 Jul 2014, 17:54

Re: Possible to detect when script reloaded

21 Jul 2015, 17:22

Code: Select all

#SingleInstance, force
WasReloaded = %1%
If WasReloaded = Reloaded
	msgbox Script was reloaded

Gui, Add, Button, gReloadIt, Reload
Gui, Show, w200 h100
Return

ReloadIt:
	Run "%A_ScriptFullPath%" Reloaded
	ExitApp
Return
Run "%A_ScriptFullPath%" Reloaded passes Reloaded back to the program as %1%.
The first time you run the program, %1% will be blank. Any time it gets reloaded, (in this case by clicking the reload button) %1% will equal Reloaded
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Possible to detect when script reloaded

21 Jul 2015, 18:49

Thanks Boiler and Don for your ideas.

I think I would go for the ini file solution, I would try avoid any user interaction during the test.

Don's solution also wuld work, but I doesn't want any user interaction. Is that possible somehow without the gui?

How can I make the script differ from reload command and exit? I guess that's the big question here.
lexikos
Posts: 6404
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Possible to detect when script reloaded

21 Jul 2015, 19:04

Code: Select all

if DllCall("GetCommandLine", "str") ~= "i) /r(estart)?(?!\S)"
    MsgBox Script was (probably) reloaded
The command line format when reloading is "%A_AhkPath%" /restart "%A_ScriptFullPath%" for uncompiled scripts and "%A_ScriptFullPath%" /restart for compiled scripts. If your script has its own /r or /restart switch, you may need to be more precise. For example, this works with the Reload command in an uncompiled script:

Code: Select all

if DllCall("GetCommandLine", "str") = """" A_AhkPath """ /restart """ A_ScriptFullPath """"
	MsgBox Script was reloaded
Alternatively, you could parse the command line the way AutoHotkey does. If the /r or /restart switch appears before the script path for an uncompiled script, it's reloading, and AutoHotkey will look for and terminate one previous instance. For a compiled script, /r or /restart probably has to appear before the first unrecognized switch, if there are any.


Don's GUI is just to demonstrate; you don't need the GUI, just the "Reloaded" (or whatever) command line switch and code to detect it.
MegaloDon
Posts: 107
Joined: 22 Jul 2014, 17:54

Re: Possible to detect when script reloaded

21 Jul 2015, 20:29

lexikos wrote:Don's GUI is just to demonstrate; you don't need the GUI, just the "Reloaded" (or whatever) command line switch and code to detect it.
Exactly.
I just wanted to give a working example.
How exactly would your script get "reloaded"? If you have something in the script to tell it to reload (without user interaction or otherwise) you simply pass the variable there like in my example.
lexikos
Posts: 6404
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Possible to detect when script reloaded

21 Jul 2015, 23:07

Scripts can be reloaded via the tray icon or menu in the main window, without executing any script subroutines. Other scripts can also send the corresponding message to trigger a reload, or even just Run AutoHotkey with the /restart switch. It is possible for the script to intercept the menu messages via OnMessage, but it is easier (via copy-paste) to check the command line as I showed, which also catches other methods of reloading.
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Possible to detect when script reloaded

22 Jul 2015, 10:15

lexikos wrote:

Code: Select all

if DllCall("GetCommandLine", "str") ~= "i) /r(estart)?(?!\S)"
    MsgBox Script was (probably) reloaded
The command line format when reloading is "%A_AhkPath%" /restart "%A_ScriptFullPath%" for uncompiled scripts and "%A_ScriptFullPath%" /restart for compiled scripts. If your script has its own /r or /restart switch, you may need to be more precise. For example, this works with the Reload command in an uncompiled script:

Code: Select all

if DllCall("GetCommandLine", "str") = """" A_AhkPath """ /restart """ A_ScriptFullPath """"
	MsgBox Script was reloaded
Alternatively, you could parse the command line the way AutoHotkey does. If the /r or /restart switch appears before the script path for an uncompiled script, it's reloading, and AutoHotkey will look for and terminate one previous instance. For a compiled script, /r or /restart probably has to appear before the first unrecognized switch, if there are any.


Don's GUI is just to demonstrate; you don't need the GUI, just the "Reloaded" (or whatever) command line switch and code to detect it.
Thanks a lot - that last piece of code made my day :D

Return to “Ask For Help”

Who is online

Users browsing this forum: bennybroseph, Google [Bot], vsub and 209 guests