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
[solved] Possible to detect when script reloaded
- Almost_there
- Posts: 404
- Joined: 30 Sep 2014, 10:32
[solved] Possible to detect when script reloaded
Last edited by Almost_there on 22 Jul 2015, 10:17, edited 1 time in total.
Re: Possible to detect when script reloaded
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.
Re: Possible to detect when script reloaded
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
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
- Almost_there
- Posts: 404
- Joined: 30 Sep 2014, 10:32
Re: Possible to detect when script reloaded
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.
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.
Re: Possible to detect when script reloaded
Code: Select all
if DllCall("GetCommandLine", "str") ~= "i) /r(estart)?(?!\S)"
MsgBox Script was (probably) reloaded
Code: Select all
if DllCall("GetCommandLine", "str") = """" A_AhkPath """ /restart """ A_ScriptFullPath """"
MsgBox Script was reloaded
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.
Re: Possible to detect when script reloaded
Exactly.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.
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.
Re: Possible to detect when script reloaded
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.
- Almost_there
- Posts: 404
- Joined: 30 Sep 2014, 10:32
Re: Possible to detect when script reloaded
Thanks a lot - that last piece of code made my daylexikos wrote: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") ~= "i) /r(estart)?(?!\S)" MsgBox Script was (probably) 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.Code: Select all
if DllCall("GetCommandLine", "str") = """" A_AhkPath """ /restart """ A_ScriptFullPath """" MsgBox Script was reloaded
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.