[Tool] - Pre-Editor - Pre-Edit AHK Scripts Before Execution!

Post your working scripts, libraries and tools for AHK v1.1 and older
User
Posts: 407
Joined: 26 Jun 2017, 08:12

[Tool] - Pre-Editor - Pre-Edit AHK Scripts Before Execution!

08 Jan 2019, 13:18

v1.1 (click here)
v1.0 (click here)

Basically, set windows to always run ".ahkpe" extension files with "Pre-Editor.exe"

- Example, run "Test.ahkpe"

- "Pre-Editor.exe" will automatically pre-edit script from "Test.ahkpe" file

- "Pre-Editor.exe" will store the pre-edited script in "Test.ahkpe.ahk" file! (in the same directory of "Test.ahkpe")

- "Pre-Editor.exe" then executes "Test.ahkpe.ahk", and exit!

- "Test.ahkpe.ahk" itself will delete "Test.ahkpe.ahk" file

Image


(Optional) Scripts can be written and tested directly in the "Pre-Editor" gui! (Execute "Pre-Editor.exe" directly! - Example below!)

Image
Last edited by User on 08 Jan 2019, 13:20, edited 1 time in total.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Tool] - Pre-Editor - Pre-Edit AHK Scripts Before Execution!

08 Jan 2019, 13:20

(v1.1)

This version makes use of "Pre_Edit_Script()" v1.3 Function, in which supports "%%#Include_Another.ahkpe_File%%" (useful to keep "%%++UniqueId%%" or "%%++Any_Var%%" correct counting that can be used, for example, to create "Anonymous Functions"!)

Pre-Editor (v1.1).rar
(344.79 KiB) Downloaded 57 times
(The ".rar" file above includes: Pre-Editor.exe, Test.ahkpe, Another.ahkpe)


"Pre-Editor" and "Pre_Edit_Script()"

Code: Select all

Cmd_Total_Parameters = %0%

Cmd_Parameter := []

loop, % Cmd_Total_Parameters
Cmd_Parameter[a_index] := %a_index%

if (Cmd_Parameter[1] != "")
{
	;msgbox, % Cmd_Parameter[1]

FileRead, File_Content, % Cmd_Parameter[1]	;"Cmd_Parameter[1]" contains the file path

File_Extension := ".ahk"

FileDelete, % Cmd_Parameter[1] File_Extension

Pre_Edited_Script := "FileDelete, " Cmd_Parameter[1] File_Extension "`r`n`r`n"

Pre_Edited_Script .= Pre_Edit_Script(File_Content)

FileAppend, % Pre_Edited_Script, % Cmd_Parameter[1] File_Extension, UTF-8	;"UTF-8" same Encoding used by ".ahk" files

run, % Cmd_Parameter[1] File_Extension

exitapp
}


;______________________________

App_Title := "Pre-Editor"

App_Version := "1.1"

;______________________________


Script =
(join`r`n

xxx := "111"

 yyy := "222"

	zzz := "333"

;ttt := "44444"

gui, add, text, , `%`%xxx`%`%  `%`%yyy`%`%  `%`%zzz`%`%  `%`%;ttt`%`%

gui, add, text, , `%`%++Count`%`%  `%`%++Count`%`%  `%`%++Count`%`%  -  `%`%Count`%`%  `%`%Count`%`%  `%`%Count`%`%

gui, add, text, , `%`%++UniqueId`%`%  `%`%++UniqueId`%`%  -  `%`%UniqueId`%`%  `%`%UniqueId`%`%

;_________

gui, add, text, w300 h200, Left click anywhere in this window

gui, add, button, gRunAnonymousFunctions, Run Anonymous Functions

gui, show

return

RunAnonymousFunctions:	;__

`%`%#Include_Another.ahkpe`%`%

Anonymous_Function`%`%++UniqueId`%`%()
Anonymous_Function`%`%UniqueId`%`%()
{
msgbox, `% A_ThisFunc
}

Anonymous_Function`%`%++UniqueId`%`%()
Anonymous_Function`%`%UniqueId`%`%()
{
msgbox, `% A_ThisFunc
}

Anonymous_Function`%`%++UniqueId`%`%()
Anonymous_Function`%`%UniqueId`%`%()
{
msgbox, `% A_ThisFunc
}

`%`%#Include_Another.ahkpe`%`%

return

guiclose:	;__
exitapp

;___________


;Function_Name := "Test"

`%`%;Function_Name`%`%()		;__________________________________________________
{
Static RunAtScriptExecution := OnMessage(0x201, Func("`%`%;Function_Name`%`%"))	;"0x201" left mouse down

Static count := 0

tooltip, `% A_ThisFunc "() - " count++, 0, 200, 1
}

   ;Function_Name := "First"

   `%`%;Function_Name`%`%()		;__________________________________________________
   {
   Static RunAtScriptExecution := OnMessage(0x201, Func("`%`%;Function_Name`%`%"))	;"0x201" left mouse down

   Static count := 1

   tooltip, `% A_ThisFunc "() - " count++, 0, 225, 2
   }



	;Function_Name := "Last"

	`%`%;Function_Name`%`%()		;__________________________________________________
	{
	Static RunAtScriptExecution := OnMessage(0x201, Func("`%`%;Function_Name`%`%"))	;"0x201" left mouse down

	Static count := 2

	tooltip, `% A_ThisFunc "() - " count++, 0, 250, 3
	}

)


Temp_Script_File_Name := "#_AHK_Temp_Pre_Edited_Script.ahk"

gui,add, button, gPreRunScript, Pré-Edit and Run Script

gui, add, button, x+5 gShowPreEdScript, show Pre-Edited code

gui, add, edit, xm w800 h600 +HScroll WantTab +HwndUserInputControlId,

ControlSetText , , % Script, % "ahk_id" UserInputControlId

gui, show, , % App_Title " (v" App_Version ")"

return

ShowPreEdScript:	;___________________________________

ControlGetText, UserScript, , % "ahk_id" UserInputControlId

gui ShowPreEdScript:default

gui, destroy

gui, add, edit, w700 h500 +HScroll +HwndPreEdScriptOut,

ControlSetText , , % Pre_Edit_Script(UserScript), % "ahk_id" PreEdScriptOut

gui, show

return

PreRunScript:	;_________________________________________

ControlGetText, UserScript, , % "ahk_id" UserInputControlId

FileDelete, % Temp_Script_File_Name

FileAppend , % Pre_Edit_Script(UserScript), % Temp_Script_File_Name

run, % Temp_Script_File_Name

return

guiclose:	;_________________________________________
exitapp


RegExEsc(String, Options := "")		;_________ RegExEsc(Function) - v1.0 __________
{

if (Options == "$")
return, RegExReplace(String, "\$", "$$$$")	;to be used with "RegExReplace" third parameter! ("$$" represents one literal "$")

return, "\E\Q" RegExReplace(String, "\\E", "\E\\E\Q") "\E"	;to be used with "RegExMatch" and "RegExReplace" second parameters! ("\\" represents one literal "\")

}


Pre_Edit_Script(Script)		;_______________ v1.3 _________________
{

Pre_Vars := []

	MatchLength := 0
	FoundPos := 1
	loop
	{
	FoundPos := RegExMatch(Script, "(?P<Set>(\n| |\t)(?P<Key>\S+?)( |\t)*?:=( |\t)*?""(?P<Value>.*?)"")|(?P<Rep>%%(?P<Var>\S+?)%%)", Match_, FoundPos + MatchLength)

	;\s, Matches any single whitespace character, mainly space, tab, and newline (`r and `n)
	;"(\n| |\t)" in use because "\s" does not work correctly!
	;\S, means "any non-whitespace character".

		if (FoundPos == "" or FoundPos = 0)
		break
		else
		{

			if (Match_ = Match_Set)
			{
			Pre_Vars[Match_Key] := Match_Value

			MatchLength := StrLen(Match_)

				;msgbox, % Match_Set " - " Match_Key " / " Match_Value
			}
			else if (Match_ = Match_Rep)
			{
				if RegExMatch(Match_Var, "i)^#Include_(.+)", Found_Include)
				{
					;msgbox, % Found_Include " / " Found_Include1

				FileRead, Include_Content, % Found_Include1

				Script := RegExReplace(Script, RegExEsc(Match_Rep), RegExEsc(Include_Content, "$"), , 1, FoundPos)

				Include_Content := ""		;free memory

				MatchLength := 0

				continue	;Skips the rest of the current loop iteration and begins a new one. (Valid inside any kind of loop.)
				}

				if RegExMatch(Match_Var, "^\+\+(.+)", Found_Var)
				{
					;msgbox, % Match_Var " / " Found_Var1

				Match_Var := Found_Var1

				Match_Var_Value := Pre_Vars[Match_Var]

				if Match_Var_Value is number
				Pre_Vars[Match_Var]++
				else
				Pre_Vars[Match_Var] := 0
				}

			Script := RegExReplace(Script, RegExEsc(Match_Rep), RegExEsc(Pre_Vars[Match_Var], "$"), , 1, FoundPos)

			MatchLength := StrLen(Pre_Vars[Match_Var])

				;msgbox, % Match_Rep " - " Match_Var
			}
		
		}
	}


return, Script 
}

Test.ahkpe

Code: Select all

xxx := "111"

 yyy := "222"

	zzz := "333"

;ttt := "44444"

gui, add, text, , %%xxx%%  %%yyy%%  %%zzz%%  %%;ttt%%

gui, add, text, , %%++Count%%  %%++Count%%  %%++Count%%  -  %%Count%%  %%Count%%  %%Count%%

gui, add, text, , %%++UniqueId%%  %%++UniqueId%%  -  %%UniqueId%%  %%UniqueId%%

;_________

gui, add, text, w300 h200, Left click anywhere in this window

gui, add, button, gRunAnonymousFunctions, Run Anonymous Functions

gui, show

return

RunAnonymousFunctions:	;__

%%#Include_Another.ahkpe%%

Anonymous_Function%%++UniqueId%%()
Anonymous_Function%%UniqueId%%()
{
msgbox, % A_ThisFunc
}

Anonymous_Function%%++UniqueId%%()
Anonymous_Function%%UniqueId%%()
{
msgbox, % A_ThisFunc
}

Anonymous_Function%%++UniqueId%%()
Anonymous_Function%%UniqueId%%()
{
msgbox, % A_ThisFunc
}

%%#Include_Another.ahkpe%%

return

guiclose:	;__
exitapp

;___________


;Function_Name := "Test"

%%;Function_Name%%()		;__________________________________________________
{
Static RunAtScriptExecution := OnMessage(0x201, Func("%%;Function_Name%%"))	;"0x201" left mouse down

Static count := 0

tooltip, % A_ThisFunc "() - " count++, 0, 200, 1
}

   ;Function_Name := "First"

   %%;Function_Name%%()		;__________________________________________________
   {
   Static RunAtScriptExecution := OnMessage(0x201, Func("%%;Function_Name%%"))	;"0x201" left mouse down

   Static count := 1

   tooltip, % A_ThisFunc "() - " count++, 0, 225, 2
   }



	;Function_Name := "Last"

	%%;Function_Name%%()		;__________________________________________________
	{
	Static RunAtScriptExecution := OnMessage(0x201, Func("%%;Function_Name%%"))	;"0x201" left mouse down

	Static count := 2

	tooltip, % A_ThisFunc "() - " count++, 0, 250, 3
	}

Another.ahkpe

Code: Select all

Anonymous_Function%%++UniqueId%%()
Anonymous_Function%%UniqueId%%()
{
msgbox, % A_ThisFunc " - from ''Another.ahkpe''!"
}
User avatar
Thoughtfu1Tux
Posts: 125
Joined: 31 May 2018, 23:26

Re: [Tool] - Pre-Editor - Pre-Edit AHK Scripts Before Execution!

09 Jan 2019, 04:38

This is a really interesting concept, what kind of scenarios do you use this for?
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Tool] - Pre-Editor - Pre-Edit AHK Scripts Before Execution!

09 Jan 2019, 08:43

Thoughtfu1Tux wrote:
09 Jan 2019, 04:38
This is a really interesting concept, what kind of scenarios do you use this for?
As already mentioned above, "Pre-Editor.exe" can be used to create anonymous functions, avoid code repetitions, associate "Unique_Ids" to different things, and may be useful for many other things that I myself don't even know!

For example, each label can execute its own Anonymous_function, and you don't have to worry about enumerating them because "%%++Unique_Id%%" or %%++Any_Variable%% will do it for you automatically:

Anonymous_Functions are useful because they allow that each thread to be executed in a "Local" environment!

Do_This:
Anonymous_Function%%++Unique_Id%%()
Anonymous_Function%%Unique_Id%%(){
msgbox, Do_This
}
return

Do_That:
Anonymous_Function%%++Unique_Id%%()
Anonymous_Function%%Unique_Id%%(){
msgbox, Do_That
}
return

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: MrHue, trouble003 and 265 guests