ex_this - add to the auto-execute section from anywhere in your script

Post your working scripts, libraries and tools
User avatar
davebrny
Posts: 69
Joined: 05 Dec 2016, 06:26

ex_this - add to the auto-execute section from anywhere in your script

21 Nov 2017, 16:04

Code: [Select all] [Download] GeSHi © Codebox Plus

;execute_this
;execute_end

;exit_this
;exit_end


if all of your scripts are split up into individual files and included into one main script then there can be a bit of going back and forth between the current file and the auto-execute section any time you need to change something there
ex_this() solves this problem (in most cases) by letting you add to the auto-execute section (or exit subroutine) from anywhere in your script

it also solves the problem of getting "label not found" errors when you remove a file but forgot to remove the goSub or timer that was in the auto-execute section

another benefit of using this method is that sharing code with others becomes easier since everything is self contained in one file.
if the person youre sharing a script with is also using ex_this() then all they have to do is include it into their own script and reload to get things up and running.
if theyre not, then the execute/exit tags can act sort of like instructions about which parts need to be copied to auto-execute section of their script

lastly, since the execute/exit tags are just comments, they dont have any effect if someone decides to run the script it on its own


drawbacks

ex_this() copies the code between the execute/exit tags and then stores it in \AppData\Local\Temp\ex_this\
this means means that msgBox errors will show that location and not the source file, as will certain variables such as a_lineFile.

in certain situations you might be able to work around these problems by using the "goto error" timer or by using the following function to convert a_lineFile to the actual source file

Code: [Select all] [Download] GeSHi © Codebox Plus

source_file := ex_source(a_lineFile)


i havnt tested this it out in all the different situations it could be used in yet so there are probably more drawbacks than the ones listed above.
also, i have around 100 or so separate files included into my main script and i use about 15 execute/exit_this tags so i cant say for sure how well this will work if it has to scan 1000s of files every time you reload


other methods?

i had this idea a while back and just went and made it without thinking too much about different ways of doing it, so i would be curious to know if there are any alternative or better ways that ive missed



more words n stuff in the gist readme

gist code
User avatar
Nextron
Posts: 1063
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win7 x64 AHK: Unicode x32

Re: ex_this - add to the auto-execute section from anywhere in your script

21 Nov 2017, 17:37

davebrny wrote:it also solves the problem of getting "label not found" errors when you remove a file but forgot to remove the goSub or timer that was in the auto-execute section
Sounds familiar. Instead of moving code blocks of apart, between auto-execute and the rest, I used to keep it together and just GoSub to it as well.

other methods?
Now I prefer to put any auto-executing part of a piece of code in its own function and calling itself as a static variable (remember setting any variables global, where needed). Similarly, setting a destructor (naming?), works as an OnExit replacement:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


The order of execution of this code is something to take into account, as the static variables are set prior to auto-execute, so these lines run before it as well, meaning that any auto-execute commands changing AHK defaults such as SetTitleMatchMode have not been run either. Since AHK is still initializing at this point, there are some other limitations, but I didn't bother to remember those :think: .
But this has suited me well, and code set up like this is portable and can just be pasted in any location of an existing script and expected to run as intended.
The more I know:
The more I know,
I know nothing.
iPhilip
Posts: 222
Joined: 02 Oct 2013, 12:21

Re: ex_this - add to the auto-execute section from anywhere in your script

21 Nov 2017, 22:46

Hi Nextron,

Thank you for your interesting YourApplicationAutoExecute() function. Unfortunately, as posted, it doesn't do what you suggested. Here is a version that does so:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Cheers!
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
User avatar
davebrny
Posts: 69
Joined: 05 Dec 2016, 06:26

Re: ex_this - add to the auto-execute section from anywhere in your script

22 Nov 2017, 08:48

Nextron wrote:Now I prefer to put any auto-executing part of a piece of code in its own function and calling itself as a static variable


thats a neat trick. a lot more elegant as well
User avatar
Barney
Posts: 44
Joined: 28 May 2014, 20:03
Location: Germany

Re: ex_this - add to the auto-execute section from anywhere in your script

22 Nov 2017, 09:33

Or use a class:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
Nextron
Posts: 1063
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win7 x64 AHK: Unicode x32

Re: ex_this - add to the auto-execute section from anywhere in your script

22 Nov 2017, 15:20

iPhilip wrote:Unfortunately, as posted, it doesn't do what you suggested. Here is a version that does so
That's odd; for me it does work. What's not working properly for you?
Additionally, I usually put other code in the same function, so it gets called more often then just at start/exit but it only needs to perform its start/exit code at the appropriate time. Using the alternative code would not suffice for me.
The more I know:
The more I know,
I know nothing.
iPhilip
Posts: 222
Joined: 02 Oct 2013, 12:21

Re: ex_this - add to the auto-execute section from anywhere in your script

22 Nov 2017, 17:17

Hi Nextron,

I tested your code with MsgBox statements inside the if statements as follows:

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

The MsgBox start statement never gets executed because init != 0. Rather, during the initialization phase init = "" is true. I understand that the alternative code I provided doesn't meet your needs. Perhaps, this form would do so:

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

Cheers!
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
lexikos
Posts: 5453
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: ex_this - add to the auto-execute section from anywhere in your script

30 Nov 2017, 16:41

Nextron wrote:,_:={base:{__Delete: "YourApplicationAutoExecute"}}

The problem with this approach is the reverse of initialisation, only a little worse. The function executes after objects in global variables (such as classes) are released.

Instead, you should use OnExit() within the initialisation function's main body (not a static initialiser).
iPhilip
Posts: 222
Joined: 02 Oct 2013, 12:21

Re: ex_this - add to the auto-execute section from anywhere in your script

30 Nov 2017, 17:46

Hi lexikos,

Thank you for pointing that out. Could you provide a simple example showing the problem and solution?

Thank you.
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 18 guests