refreshing script while writing code: workflow / best practice

Get help with using AutoHotkey and its commands and hotkeys
beeski
Posts: 17
Joined: 06 Feb 2014, 08:38

refreshing script while writing code: workflow / best practice

17 Apr 2016, 11:41

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.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: refreshing script while writing code: workflow / best practice

17 Apr 2016, 12:41

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?
I use SetTimer like this: (Edit: combined with Reload)

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.
beeski
Posts: 17
Joined: 06 Feb 2014, 08:38

Re: refreshing script while writing code: workflow / best practice

17 Apr 2016, 12:46

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.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: refreshing script while writing code: workflow / best practice

17 Apr 2016, 12:56

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.
Guest

Re: refreshing script while writing code: workflow / best practice

17 Apr 2016, 13:18

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.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: refreshing script while writing code: workflow / best practice

17 Apr 2016, 13:36

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.
To recover from the "damage" that you describe, you can also have a Routine that restores the original script.
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.
Guest

Re: refreshing script while writing code: workflow / best practice

17 Apr 2016, 15:42

Yeah that should do the trick nicely 8-)
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 :think:
TAC109
Posts: 405
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: refreshing script while writing code: workflow / best practice

17 Apr 2016, 16:30

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.
My programs:-
ReClip - a Text Reformatting and Clip Management utility
XRef - Produces Cross Reference lists for scripts
User avatar
tidbit
Posts: 1147
Joined: 29 Sep 2013, 17:15
Location: USA

Re: refreshing script while writing code: workflow / best practice

17 Apr 2016, 16:38

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.
Image
rawr. fear me.
*poke*
Is it December 21, 2012 yet?

Return to “Ask For Help”

Who is online

Users browsing this forum: 3ggg, Cerberus, flyingDman, TheBeginner and 204 guests