 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Raccoon
Joined: 02 Jan 2008 Posts: 70 Location: Freenode IRC
|
Posted: Sat Jul 05, 2008 4:52 pm Post subject: #AutoReload (automatically reload changes to a script) |
|
|
#AutoReload
______________________________
Monitors the script file for changes and reloads the script automatically.
#AutoReload [on|ask|off] [, includes]
Parameters:
on|ask|off
#AutoReload without one of the following words after it is equivalent to #AutoReload ask.
On: The script will be monitored for changes and automatically Reload if changes are made.
Ask (default): The script will be monitored for changes and a dialog box is displayed asking whether to reload the script or cancel.
Off: The script will no longer be monitored for changes.
[, include]
Include: The word INCLUDE will cause the script to monitor changes to any script dependencies such as those introduced by the #Include directive or Libraries of Functions (\Lib folder).
Remarks:
This command is useful for scripts that are frequently changed.
A script file can be modified while the script is running, but the old version of the script will continue to execute until it has been reloaded, either by right-clicking the tray icon and selecting "Reload This Script" or via Reload command. This behavior can be changed with the #AutoReload directive as described above.
This directive is a convenient alternative to setting a hotkey to reload your script as outlined in the examples section of Reload. It may be useful in situations where the script is modified remotely over a network file share or FTP.
Related:
Reload, #Include, Libraries of Functions
Example:
#AutoReload on
#AutoReload ask
#AutoReload off
#AutoReload on, includes _________________ Need help right away? Get live support on IRC.
Already have an IRC client installed? /join #autohotkey |
|
| Back to top |
|
 |
Slanter
Joined: 28 May 2008 Posts: 307 Location: Minnesota, USA
|
Posted: Mon Jul 07, 2008 2:34 am Post subject: |
|
|
This really doesn't need to be built in to AHK, as it would probably only be used for testing scripts. Just make yourself a hotkey
or, if that really isn't enough for you, have the script check it's modified time.
| Code: | FileGetTime, ModTime, %A_ScriptFullPath%, M
SetTimer, CheckTime, 1000
Return
CheckTime:
FileGetTime, ModTime2, %A_ScriptFullPath%, M
If (ModTime2 != ModTime)
Reload
Return |
|
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6804 Location: Pacific Northwest, US
|
Posted: Mon Jul 07, 2008 6:32 am Post subject: |
|
|
the implementation on compiled scripts would be difficult. what is the proposed operation in that case?
I could see this being useful, even if it just includes some other code.
you could probably add a function to your std library, then have it autoinclude the file for you. _________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM |
|
| Back to top |
|
 |
Azerty
Joined: 19 Dec 2006 Posts: 72 Location: France
|
Posted: Thu Jul 31, 2008 7:26 am Post subject: |
|
|
I think such a feature would be useful.
But I'd add
#AutoReload [on|ask|off | function()] [, includes]
where the function() would return a boolean indicating wether reload must occur, so in specific cases, we get an escape way.
As for compiled scripts, the autoreload would have no effect. Moreover (though unchecked) I think the compiled executable is probably locked when it's running and probably can't be changed on the fly. |
|
| Back to top |
|
 |
Slanter
Joined: 28 May 2008 Posts: 307 Location: Minnesota, USA
|
Posted: Thu Jul 31, 2008 7:43 am Post subject: |
|
|
| Azerty wrote: | | Moreover (though unchecked) I think the compiled executable is probably locked when it's running and probably can't be changed on the fly. |
Actually, they can. I'm pretty sure the script is completely loaded into memory when it is run, and from that point on the exe file isn't needed. Same thing goes for uncompiled scripts. Once you run it, you can move, rename or delete the .ahk file.
Anyway, here's a function that seems to work. I haven't done much testing, but the basic functionality for the main script seems to work.
| Code: | ; -----
; AutoReload([Mode,MonitorIncluded])
; -----
; Modes:
; On - (default) Checks once every second for changes and auto-reloads
; Ask - Checks once every second for changes and asks to reload
; Off - Off
; MonitorIncluded:
; 1 - Monitors included files for changes
; 0 - (default) Monitors only the main script for changes
AutoReload(Mode="on",MonitorIncluded=0,Init=1)
{
Static IncludeFiles,IncludeTimes,RelMode,PrevInit
If (Init)
RelMode := Mode
If (Init && !PrevInit && (Mode = "on" || Mode = "ask"))
{
PrevInit := 1
IncludeFiles := A_ScriptFullPath
FileGetTime, IncludeTimes, %A_ScriptFullPath%, M
If (MonitorIncluded = 1)
{
Loop, Read, %A_ScriptFullPath%
{
If (RegExMatch(A_LoopReadLine,"i)^\s*\#include"))
{
File := RegExReplace(A_LoopReadLine,"i)^\s*\#include ([^;]+)","$1")
FileGetTime, Time, %File%, M
IncludeFiles .= "|" . File
IncludeTimes .= "|" . Time
}
}
}
SetTimer, CheckReload, 1000
}
Else If (Mode = "off")
SetTimer, CheckReload, off
Else If (Mode = "on")
SetTimer, CheckReload, 1000
Else
{
StringSplit, Files, IncludeFiles, |
StringSplit, Times, IncludeTimes, |
Loop, %Files0%
{
File := Files%A_Index%
Time := Times%A_Index%
FileGetTime, Time2, %File%, M
If (Time != Time2 && RelMode != "ask")
Reload
Else If (Time != Time2)
{
MsgBox, 4, Reload?, Would you like to reload the script?
IfMsgBox Yes
Reload
Else
{
FileGetTime, Time, %File%, M
IncludeTimes := RegExReplace(IncludeTimes,"(([^\|]\|){" (A_Index - 1) "})\d+",Time)
}
}
}
}
}
CheckReload:
AutoReload("","",0)
Return |
Edit:
11000th post in the wish list forum  _________________ Unless otherwise stated, all code is untested
(\__/) This is Bunny.
(='.'=) Cut, copy, and paste bunny onto your sig.
(")_(") Help Bunny gain World Domination. |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Jul 31, 2008 6:55 pm Post subject: |
|
|
Rather than using a timer, I suggest using the API (DLL) call "ReadDirectoryChangesW" with "FILE_NOTIFY_CHANGE_LAST_WRITE" and RegisterCallback().
http://msdn.microsoft.com/en-us/library/aa365465(VS.85).aspx
This way, AHK is notified whenever changes to the script directory occurs and not before.
[ Moderator!: MSDN link fixed ] |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6804 Location: Pacific Northwest, US
|
Posted: Fri Aug 01, 2008 4:13 am Post subject: |
|
|
exes that are running are locked. _________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|