ExecScript() - Run AHK script (file, thru pipe, from stdin)

Post your working scripts, libraries and tools
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

ExecScript() - Run AHK script (file, thru pipe, from stdin)

07 Nov 2014, 09:27

ExecScript

Execute/run AutoHotkey script - .ahk file, through named pipe(s) - inspired by DynaRun, or from StdIn(requires latest test binaries)


Syntax: (usage and parameters are explained within the source)
exec := ExecScript( script [ , args, kwargs* ] )

Misc features:
  • Specify the AutoHotkey executable to use
  • Set working/starting directory
  • Pass command line arguments
  • Specify codepage - uses the /CPn switch
  • Option to execute/run script as a child process - the function uses either WshShell.Exec() or WshShell.Run() with the former as the default. When using Exec(), the script is run as a child process and a WshScriptExec object is returned. One advantage is that the parent script will have access to the script's stdin, stdout and stderror
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: ExecScript() - Run AHK script (file, thru pipe, from std

07 Nov 2014, 09:55

Edit: Simplified example

Code: Select all

#Include ExecScript.ahk

code := "
(Join`n
FileAppend [Child script] Hello!!``n, *
if (A_AhkVersion < 2)
{
	A_Args := []
	Loop `%0`%
		A_Args[A_Index] := `%A_Index`%
}
for i, arg in A_Args
{
	FileAppend [Child script] `%i`% = `%arg`%``n, *
	Sleep 750
}
working_dir := (A_AhkVersion < 2) ? A_WorkingDir : A_InitialWorkingDir
FileAppend [Child script] Working Directory: `%working_dir`%``n, *
Sleep 750
FileAppend [Child script] AutoHotkey.exe: `%A_AhkPath`%``n, *
Sleep 750
FileAppend [Child script] AutoHotkey version: `%A_AhkVersion`%``n, *
Sleep 750
FileAppend [Child script] Goodbye!!``n, *
return
)"

;// Exec child script
script := ExecScript(code, ["Param One", "Param Two", "Param Three"])

SetTimer Poll, -100 ;// start a separate thread

Gui New
Gui Font, s10, Consolas
Gui Margin, 0, 0
Gui Add, Edit, HwndhEdit w500 r15
Gui Show
EditAppendLine(hEdit, "[Main script] End of auto-execute section")
return
GuiClose:
	ExitApp

;// Read StdOut and/or StdErr
Poll:
	EditAppendLine(hEdit, "[Main script] Poll Thread Start")
	while (script.Status != 1) ;// http://goo.gl/g45DXO
		if ( (out := script.StdOut.ReadLine()) != "" )
			EditAppendLine(hEdit, out)
	if ( (err := script.StdErr.ReadAll()) != "" )
		EditAppendLine(hEdit, err)
	EditAppendLine(hEdit, "[Main script] Poll Thread Done")
	return

EditAppendLine(hEdit, line)
{
	pLine := &(line .= "`r`n")
	SendMessage, 0x000E, 0, 0,, ahk_id %hEdit% ; WM_GETTEXTLENGTH
	SendMessage, 0x00B1, %ErrorLevel%, %ErrorLevel%,, ahk_id %hEdit% ; EM_SETSEL
	SendMessage, 0x00C2, 0, %pLine%,, ahk_id %hEdit% ; EM_REPLACESEL
}
william_ahk
Posts: 40
Joined: 03 Dec 2018, 20:02

Re: ExecScript() - Run AHK script (file, thru pipe, from stdin)

30 Dec 2020, 09:23

Is there any way to terminate the generated script from master script?
User avatar
Xtra
Posts: 2048
Joined: 02 Oct 2015, 12:15

Re: ExecScript() - Run AHK script (file, thru pipe, from stdin)

30 Dec 2020, 13:59

william_ahk wrote:
30 Dec 2020, 09:23
Is there any way to terminate the generated script from master script?
* name - when running through named pipes, 'name' specifies the pipe name.
* If omitted, a random value is generated. Otherwise, specify an
* asterisk(*) to run from stdin. This option is ignored when a file
* is specified for the 'script' parameter.
When you specify a name you know the name of the script to close
william_ahk
Posts: 40
Joined: 03 Dec 2018, 20:02

Re: ExecScript() - Run AHK script (file, thru pipe, from stdin)

30 Dec 2020, 22:44

Xtra wrote:
30 Dec 2020, 13:59
william_ahk wrote:
30 Dec 2020, 09:23
Is there any way to terminate the generated script from master script?
* name - when running through named pipes, 'name' specifies the pipe name.
* If omitted, a random value is generated. Otherwise, specify an
* asterisk(*) to run from stdin. This option is ignored when a file
* is specified for the 'script' parameter.
When you specify a name you know the name of the script to close
Can you show an example? I can't find any code to terminate ahk scripts via named pipes.
User avatar
Xtra
Posts: 2048
Joined: 02 Oct 2015, 12:15

Re: ExecScript() - Run AHK script (file, thru pipe, from stdin)

31 Dec 2020, 05:41

Code: Select all

ExecScript(ScriptCode, ["arg"], "name=myscriptnamehere", "dir=" . A_WorkingDir)

DetectHiddenWindows, On
WinClose, myscriptnamehere
HotKeyIt
Posts: 2251
Joined: 29 Sep 2013, 18:35
Contact:

Re: ExecScript() - Run AHK script (file, thru pipe, from stdin)

31 Dec 2020, 09:09

It is better to use ProcessID

Code: Select all

script:=ExecScript("MsgBox")

DetectHiddenWindows, On
WinWait % "ahk_class AutoHotkey ahk_pid " script.ProcessID
WinClose % "ahk_class AutoHotkey ahk_pid " script.ProcessID
william_ahk
Posts: 40
Joined: 03 Dec 2018, 20:02

Re: ExecScript() - Run AHK script (file, thru pipe, from stdin)

31 Dec 2020, 19:15

@Xtra @HotKeyIt Thank you all!

Return to “Scripts and Functions”

Who is online

Users browsing this forum: daywalker, seddion and 21 guests