whilst writing a piece of an ahk code, saving the newly edited file is as easy as pressing ctrl & s, or whatever other keyboard shortcut your editor uses. however, updating a running ahk script using the standard method of mousing to the system tray, right-clicking on the respective ahk icon, and selecting the appropriate entry from the context menu, gets rather time-consuming and laborious over time, especially when debugging and trying out a succession of small adjustments to the code.
i feel that the professional ahk coder must be using something more streamlined.
what methods do you use to refresh the running ahk script in the fastest way possible? is there an actual ahk command for this? would it be possible to have a master ahk script sensitive to a hotkey, that would itself update the other script that you are trying to edit? or any other, more obvious way to do this?
thank you.
refreshing script while writing code: workflow / best practice
Re: refreshing script while writing code: workflow / best practice
I use SetTimer like this: (Edit: combined with Reload)beeski wrote:what methods do you use to refresh the running ahk script in the fastest way possible? is there an actual ahk command for this?
Code: Select all
; within AutoExecute section
SetTimer, AutoReload ; auto-reload when script changes
; more code
;-------------------------------------------------------------------------------
AutoReload: ; auto-reload when script changes
;-------------------------------------------------------------------------------
If Not TimeStamp ; first pass
FileGetTime, TimeStamp, %A_ScriptFullPath%
FileGetTime, Current_TimeStamp, %A_ScriptFullPath%
If (Current_TimeStamp = TimeStamp)
Return ; do nothing yet
; script has changed
SetTimer, %A_ThisLabel%, Off
TrayTip, Reloading ..., %A_ScriptFullPath%,, 1
Sleep, 2000
Reload
; if reload fails, turn timer back on
TimeStamp := Current_TimeStamp
SetTimer, %A_ThisLabel%, On
TrayTip ; off
Return
Last edited by wolf_II on 17 Apr 2016, 13:14, edited 1 time in total.
Re: refreshing script while writing code: workflow / best practice
thank you, that seems very elegant!
one question as i am looking at the script: there is a chunk commented on with 'if reload fails, turn timer back on'. however, i see no if (sic)-statements in use, and instead, a 'Reload'-command precedes it. doesn't it mean that ahk actually never reaches this part of the code that follows after 'Reload'?
thank you.
one question as i am looking at the script: there is a chunk commented on with 'if reload fails, turn timer back on'. however, i see no if (sic)-statements in use, and instead, a 'Reload'-command precedes it. doesn't it mean that ahk actually never reaches this part of the code that follows after 'Reload'?
thank you.
Re: refreshing script while writing code: workflow / best practice
It is possible, that the edited ahk-script contains an error, which will prevent it from running. (load-time error -> reload fails)
It was convenient for me to catch this case and keep the script (that is still loaded in memory, but does not exist in this state anymore on disk) running.
It was convenient for me to catch this case and keep the script (that is still loaded in memory, but does not exist in this state anymore on disk) running.
Re: refreshing script while writing code: workflow / best practice
Fancy idea that autoreload Wolf.
But... How do you retrieve that script from memory then if i may ask? Is there a way for it to dump it's code?
I had a case recently where a script got wiped due to an error but the old version was still running.
Pressing Edit would just open the empty file since it got wiped...
Luckily I was able to use Windows' "Previous Versions" feature to retrieve it but that might not always work.
But... How do you retrieve that script from memory then if i may ask? Is there a way for it to dump it's code?
I had a case recently where a script got wiped due to an error but the old version was still running.
Pressing Edit would just open the empty file since it got wiped...
Luckily I was able to use Windows' "Previous Versions" feature to retrieve it but that might not always work.
Re: refreshing script while writing code: workflow / best practice
To recover from the "damage" that you describe, you can also have a Routine that restores the original script.Guest wrote:Fancy idea that autoreload Wolf.
But... How do you retrieve that script from memory then if i may ask? Is there a way for it to dump it's code?
I had a case recently where a script got wiped due to an error but the old version was still running.
Pressing Edit would just open the empty file since it got wiped...
Luckily I was able to use Windows' "Previous Versions" feature to retrieve it but that might not always work.
Something like this: (not yet matured, still experimental)
Code: Select all
Gosub, Restore_Preparation ; within Auto-Execute section
; code
^F12:: ; hotkey = Ctrl + F12 (change to your liking)
FileDelete, %A_ScriptFullPath%
FileAppend, Original_Script, %A_ScriptFullPath%, UTF-8
Return
;-------------------------------------------------------------------------------
Restore_Preparation: ; read original script into a variable
;-------------------------------------------------------------------------------
FileRead, Original_Script, %A_ScriptFullPath%
Return
Last edited by wolf_II on 17 Apr 2016, 16:11, edited 1 time in total.
Re: refreshing script while writing code: workflow / best practice
Yeah that should do the trick nicely
Thanks for sharing, I never actually spent time on these things before but me thinks this will come in handy.
What I do/did for making scripting easier or faster for me was including a gui with the script itself loaded in an Edit field (Ms Office 2016 gui by TheDewd).
There I have some basic text editor functions with a save and/or reload menu.
Thinking about it one could probably develop that into a full-fledged AHK editor with autocomplete, help, snippets and examples
Thanks for sharing, I never actually spent time on these things before but me thinks this will come in handy.
What I do/did for making scripting easier or faster for me was including a gui with the script itself loaded in an Edit field (Ms Office 2016 gui by TheDewd).
There I have some basic text editor functions with a save and/or reload menu.
Thinking about it one could probably develop that into a full-fledged AHK editor with autocomplete, help, snippets and examples
Re: refreshing script while writing code: workflow / best practice
Alternatively, you could use an editor such as SciTE4AutoHotkey which understands AHK syntax. It allows you to test-run the script you are editing with full diagnostic facilities such as break points and examination of variable contents by simply hovering the mouse over them.
In my case, once the script is fully debugged I compile it into a separate 'production' folder for everyday use.
In my case, once the script is fully debugged I compile it into a separate 'production' folder for everyday use.
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe
Re: refreshing script while writing code: workflow / best practice
a lot of editors have a "run" type feature, I simply press f5 or ctrl+f5 (different actions I have setup) to rerun/reload the script.
this can be done in notepad++ and s4ahk and ahk:studio and sublime and countless others. Some editors are easier than others, some have it built-in.
this can be done in notepad++ and s4ahk and ahk:studio and sublime and countless others. Some editors are easier than others, some have it built-in.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
*poke*
Is it December 21, 2012 yet?
Who is online
Users browsing this forum: roeleboele and 360 guests