"Post-Include" command.

Propose new features and changes
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

"Post-Include" command.

14 Dec 2017, 08:19

I don't really know what I'm thinking, I just feel that AHK should have some sort of command/function to "post-include" a script into a compiled script.
So, similar to FileInstall, it would load the file, if said file is in the directory specified, the compiled script then loads it and uses it, but it'd only load into the script upon starting, not conditional. However, I think that, if the file could not load in the file (Whether file format is different such as Unicode vs ASCII, or the file isn't in the AHK syntax language) it would return ErrorLevel where the script could work off of it and do something, or display a message, etc etc.

The way I think it should work as to not throw a bunch of errors would be:

Code: Select all

/*
	Plugin.ahk would be...

	MsgBox, Hello there!
	If (A_ScriptName = "Plugin.ahk") {
		x := y
	}
*/

PostInclude, Plugin.ahk, UseErrorLevel

If (InStr(A_LastError, "Error Loading File")) { ; If the file could not be loaded...
	MsgBox, 16, Error!
		, % "There was an error loading the file, said file could not be loaded into the program, ""Plugin.ahk"""
	ExitApp
}

Some_Function_That_Executes_AFTER_Script_Executes()
It's just something that I would think would be quite useful, esspecially for plugins or just allowing users to customize certain variables without having to include a library to read from a certain type of file like XML or JSON, or having to use IniRead which already is a little "wonky" when it comes to loading in UTF-8 files.

If you have any suggestions on this, comments, or even criticisms, please do reply! :D

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: "Post-Include" command.

17 Dec 2017, 12:35

If you're talking about #Include, to include the script at the bottom of the script rather than at the point where #Include is written, there is one thing of interest here:

Functions
https://autohotkey.com/docs/Functions.htm#lib
A script may call a function in an external file without having to use #Include. For this to work, a file of the same name as the function must exist in one of the following library directories:
An example to illustrate the idea:

Code: Select all

;you would have 3 files e.g.:
;%A_Desktop%\TestInclude1.ahk
;%A_Desktop%\TestInclude2.ahk
;%A_Desktop%\Lib\TestInclude3.ahk

;==================================================

;TestInclude1.ahk

#Include TestInclude2.ahk
TestInclude3_Load() ;includes the file, not here, but at the end of the file
return

q::
MyFunc()
return

;==================================================

;TestInclude2.ahk

MsgBox, % "this is script 2"

;==================================================

;TestInclude3.ahk

MsgBox, % "this is script 3"

TestInclude3_Load()
{
}

MyFunc()
{
	MsgBox, % "this is script 3's function"
}

;==================================================
Re. UTF-8 and ini:
UTF-8 ini files - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=38511

Here I mention a proposal for this, called #IncludeAtEnd:
Wish List 2.0 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 13&t=36789

You said quite a few things in your post, so it might be helpful to break it down a bit, explain further what you had in mind. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: "Post-Include" command.

02 Dec 2022, 20:10

It takes a bit of work, but changes in v1.1.34+ and v2.0-a135+ make this possible.

The compiled script can invoke itself with the /script switch to run external scripts, or to enable the use of the /include switch with scripts embedded in the EXE. For instance, /script /include plugin.ahk *#1 would execute the default embedded script but with plugin.ahk inserted at the top. Here's a proof of concept for v2.0-rc.2:

Code: Select all

; Main compiled script
if !IsSet(plugin) {
    if FileExist("plugin.ahk") {
        exitCode := RunWait('"' A_ScriptFullPath '" /script /force /include plugin.ahk *#1')
        if exitCode = 2
            MsgBox "Plugin failed to load"
        else
            ExitApp exitCode
    }
    else
        MsgBox "Plugin not found"
}
MsgBox "Plugin" (IsSet(plugin) ? "" : " not") " loaded"
if IsSet(plugin)
    plugin("Hello, world!")
MsgBox "Exiting"
plugin.ahk should define a function plugin(x).

Due to limitations of the current design and the semantics of #include, it is not possible to conditionally #include a file based on whether the file can be successfully loaded. Instead, the main script can attempt to restart itself with the additional file included, and detect failure by one or more of the following means:
  • The exit code as shown above - simplest, but ambiguous.
  • ErrorStdOut.
  • The existence of a new ahk_class AutoHotkey window owned by the new process; this is not created if there are syntax errors.
  • The existence of an error dialog before the above window is created.
/include is pre-include rather than post-include. There are a number of approaches that could be used for the latter, including:
  • Embed a script like the following in the EXE and run it with /script *resourceName. A_LineFile can also be used to differentiate between execution of the main embedded script (*#1) and this one.

    Code: Select all

    #include *#1
    #include plugin.ahk
  • Feed similar code to the new process via stdin, with /script * and CreateProcess or WScript.Shell & Exec.

Also note that when a script is compiled with an EXE as the base, it is possible for the compiled script to #include external files; however, I am unsure whether Ahk2Exe currently provides any way to leave the untouched #Include directive in the script. It might always attempt to preprocess the directive. In that case, the "Embed a script" option above requires that the code be embedded as a plain RCDATA resource, using whatever means necessary to prevent Ahk2Exe from preprocessing it a script.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 22 guests