String manip on built-in variables with #include Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WatsonEnterprises
Posts: 19
Joined: 25 May 2020, 23:04

String manip on built-in variables with #include

Post by WatsonEnterprises » 07 Sep 2020, 21:20

I have a script named "ThisScriptShouldIncludeJoe.ahk". I want this script to #include a script named "Joe.ahk".

I want it to rely on a built-in variable, so other scripts can reuse the same logic. (ThisScriptShouldIncludeSteve.ahk, ThisScriptShouldIncludeMike.ahk)

The code below expresses the kind of logic I want. (But I know this doesn't work, as #include directives are evaluated before runtime)

Code: Select all

scriptToInclude := StrReplace(A_ScriptName, "ThisScriptShouldInclude")
#include %scriptToInclude%
Is there any way to do string manipulation on built-in variables (A_LineFile, A_ScriptDir, A_ScriptName, etc) for use in an include directive, without writing a pre-processor? I want to isolate the string "Joe.ahk" from "ThisScriptShouldIncludeJoe.ahk".

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: String manip on built-in variables with #include

Post by BoBo » 08 Sep 2020, 02:29

Code: Select all

MsgBox % A_UserName						; current users login name

#Include %A_UserName%.ahk				; working (if file '<current users login name>.ahk' exists)
#Include _Super %A_UserName%.ahk		; working (if file '_Super <current users login name>.ahk' exists)

; name := A_UserName
; #Include %name%.ahk					; not working
Return
TBH, I don't know if built-in vars can be manipulated in whatever way. i.e. to use them as 'trojan horses' to bypass the following restrictions if used with the #Include directive. As you already know, so JFTR. ...
The path of a file or directory as explained below. This must not contain double quotes, wildcards, or references to non-built-in variables. Escape sequences other than semicolon (`;) must not be used, nor are they needed because characters such as percent signs are treated literally.

Percent signs which are not part of a valid variable reference are interpreted literally. All built-in variables are valid, except for ErrorLevel, A_Args and the numbered variables. Prior to [v1.1.28], only %A_ScriptDir%, %A_AppData%, %A_AppDataCommon% and [in v1.1.11+] %A_LineFile% were supported.

Known limitation: When compiling a script, variables are evaluated by the compiler and may differ from what the script would return when it is finally executed. Ahk2Exe v1.1.30.00 and earlier only support the four variables listed above. [v1.1.30.01+]: The following variables are also supported: A_AhkPath, A_ComputerName, A_ComSpec, A_Desktop, A_DesktopCommon, A_IsCompiled, A_IsUnicode, A_MyDocuments, A_ProgramFiles, A_Programs, A_ProgramsCommon, A_ScriptFullPath, A_ScriptName, A_Space, A_StartMenu, A_StartMenuCommon, A_Startup, A_StartupCommon, A_Tab, A_Temp, A_UserName, A_WinDir.

File: The name of the file to be included, which is assumed to be in the startup/working directory if an absolute path is not specified (except for Ahk2Exe, which assumes the file is in the script's own directory). Note: SetWorkingDir has no effect on #Include because #Include is processed before the script begins executing.

Directory: Specify a directory instead of a file to change the working directory used by all subsequent occurrences of #Include and FileInstall. Note: Changing the working directory in this way does not affect the script's initial working directory when it starts running (A_WorkingDir). To change that, use SetWorkingDir at the top of the script.

LibName
[v1.0.90+]: A library file or function name. For example, #include <lib> and #include <lib_func> would both include lib.ahk from one of the function library folders. LibName cannot contain variable references.

lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: String manip on built-in variables with #include  Topic is solved

Post by lexikos » 08 Sep 2020, 03:37

#Include does not support user-defined variables because it would be meaningless: there is no way to perform an assignment or evaluate an expression before the #include is processed.

All directives are processed before the script (auto-execute section) starts executing. In particular, due to the nature of #include, it must be processed while the script is still being read from file and "semi-compiled", before it starts executing.

Post Reply

Return to “Ask for Help (v1)”