Running a Script Segment as a Separate Program

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Running a Script Segment as a Separate Program

Post by Alexander2 » 11 Mar 2022, 13:08

Does anyone know how to run a portion of a script as a separate program rather than as part of the script in which it is contained? (A new icon should appear in system tray.) I need to assign a hotkey to run a specified code segment within a script as a separate program.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Running a Script Segment as a Separate Program

Post by mikeyww » 11 Mar 2022, 13:11

:arrow: #Include

When you include one script in another one, you can also run the included script separately if you like.
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Running a Script Segment as a Separate Program

Post by Alexander2 » 11 Mar 2022, 14:07

mikeyww wrote:
11 Mar 2022, 13:11
:arrow: #Include
When you include one script in another one, you can also run the included script separately if you like.
Thank you. If I understand correctly, this command reads the contents of a specified file and treats them as code. But I need a script to run a certain segment within itself as a separate program (with a separate icon in system tray). This separate program should then run independently of the script from which it has been launched, which means that if the original program is closed, the second program should continue running.
JustinNL
Posts: 28
Joined: 01 Sep 2020, 13:22

Re: Running a Script Segment as a Separate Program

Post by JustinNL » 11 Mar 2022, 14:39

Maybe I'm overlooking something, but you could just use Run to open the second script?

You could also use OnMessage to communicate between two AHK scripts. (i.e. to use your first script to send a message to your second script to start the relevant section of the script)
https://www.autohotkey.com/docs/commands/OnMessage.htm Example 4 is an example of this.
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Running a Script Segment as a Separate Program

Post by Alexander2 » 12 Mar 2022, 11:37

What I wanted is to “split” a currently running script, that is, to automatically run a certain segment within a currently running script as a separate script. As an example, do you know how to launch the second segment in the following script as a separate program when the program code is executed? The second segment should be run as a new program (with its own icon in system tray).

Code: Select all

SoundBeep
MsgBox Example 1

SoundBeep
MsgBox Example 2
Maybe I'm overlooking something, but you could just use Run to open the second script?
Yes, but I want to keep all the scripts within a single file (rather than in different files) and to run them as separate programs when this single .ahk file is run.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Running a Script Segment as a Separate Program

Post by mikeyww » 12 Mar 2022, 12:16

If it's a separate and different program, then it's a separate file. Although a script can parse itself and create a new script, using #Include would be easier.
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Running a Script Segment as a Separate Program

Post by flyingDman » 12 Mar 2022, 14:27

Code: Select all

fileappend,msgbox World,%a_scriptdir%\script2.ahk
run, %a_scriptdir%\script2.ahk
sleep,100
filedelete, %a_scriptdir%\script2.ahk
msgbox Hello
14.3 & 1.3.7
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Running a Script Segment as a Separate Program

Post by Alexander2 » 13 Mar 2022, 09:57

Thank you. If I understand correctly, there is no option to run a portion of a script as a separate program without first copying it into a separate .ahk file. So apparently I will have to create multiple .ahk files instead of trying to run different portions of a script as separate programs.
Rohwedder
Posts: 7770
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Running a Script Segment as a Separate Program

Post by Rohwedder » 13 Mar 2022, 10:16

Hallo,
it works even without .ahk file:

Code: Select all

Script =
(
q::SoundBeep, 1000, 20
w::ToolTip,`% A_TickCount
)
ExecScript(Script,0)
e::SoundBeep, 4000, 20
ExecScript(Script, Wait:=true)
{ ;https://www.autohotkey.com/docs/commands/Run.htm#ExecScript
	shell := ComObjCreate("WScript.Shell")
	exec := shell.Exec("AutoHotkey.exe /ErrorStdOut *")
	exec.StdIn.Write(script), exec.StdIn.Close()
	Return, Wait?exec.StdOut.ReadAll():
}
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Running a Script Segment as a Separate Program

Post by mikeyww » 13 Mar 2022, 10:39

Cool! Even in the AHK documentation-- I never saw it. :bravo:
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Running a Script Segment as a Separate Program

Post by flyingDman » 13 Mar 2022, 14:31

Yes cool, but I have a hard time imagining a practical implementation... and whether this is the OP's solution. To be honest, why would one want a portion of the script to be executed as a separate script? Hope this is only to be used with good intentions...
14.3 & 1.3.7
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Running a Script Segment as a Separate Program

Post by Alexander2 » 14 Mar 2022, 10:58

Rohwedder wrote:
13 Mar 2022, 10:16
Hallo,
it works even without .ahk file:

Code: Select all

Script =
(
q::SoundBeep, 1000, 20
w::ToolTip,`% A_TickCount
)
ExecScript(Script,0)
e::SoundBeep, 4000, 20
ExecScript(Script, Wait:=true)
{ ;https://www.autohotkey.com/docs/commands/Run.htm#ExecScript
	shell := ComObjCreate("WScript.Shell")
	exec := shell.Exec("AutoHotkey.exe /ErrorStdOut *")
	exec.StdIn.Write(script), exec.StdIn.Close()
	Return, Wait?exec.StdOut.ReadAll():
}
Thank you. I can see that this script creates a separate icon in system tray. I did not know that it requires some special knowledge of coding to do this.
I have a hard time imagining a practical implementation... and whether this is the OP's solution. To be honest, why would one want a portion of the script to be executed as a separate script?
I wanted to keep some scripts in one file in order not to have too many files in a folder. The script was to check at what time it is run by the Task Scheduler, and if the time matched the specified value, to run a certain segment of the code as a separate program.
User avatar
labrint
Posts: 383
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Running a Script Segment as a Separate Program

Post by labrint » 15 Mar 2022, 02:56

Rohwedder wrote:
13 Mar 2022, 10:16
Hallo,
it works even without .ahk file:

Code: Select all

Script =
(
q::SoundBeep, 1000, 20
w::ToolTip,`% A_TickCount
)
ExecScript(Script,0)
e::SoundBeep, 4000, 20
ExecScript(Script, Wait:=true)
{ ;https://www.autohotkey.com/docs/commands/Run.htm#ExecScript
	shell := ComObjCreate("WScript.Shell")
	exec := shell.Exec("AutoHotkey.exe /ErrorStdOut *")
	exec.StdIn.Write(script), exec.StdIn.Close()
	Return, Wait?exec.StdOut.ReadAll():
}
This is very interesting @Rohwedder. So basically I have a script that was required to create an independent script.
The problem with this approach was that if you try to append a very long code for the new script, I still had to encapsulate it as a variable as such:

Code: Select all

CodeSegment =
(
;write code here
)
Then I would FileAppend the codesegment variable. Otherwise I would get "error message: "Continuation section too long."

Have you tested this method with a very long script as a secondary script?

Also is it possible to name the script other than *?
Rohwedder
Posts: 7770
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Running a Script Segment as a Separate Program

Post by Rohwedder » 15 Mar 2022, 05:11

The only plausible reason for me to use ExecScript() for larger simultaneous scripts is to package everything in an .ahk file and sell it compiled to game cheats.
Not my métier!
I prefer the method shown here by flyingDman, but I always position FileDelete before FileAppend for easier debugging of the generated script file.
ExecScript() I use only for snippets like mathematical expressions and nonsense e.g.:
viewtopic.php?p=449373#p449373
The displayed script name * probably has something to do with this:
https://www.autohotkey.com/docs/commands/FileAppend.htm#Parameters
Standard Output (stdout): Specifying an asterisk (*) for Filename causes Text to be sent to standard output (stdout).
Since several different scripts can work with this name at the same time, this has not interested me yet.
Post Reply

Return to “Ask for Help (v1)”