Running a Script Segment as a Separate Program
-
- Posts: 348
- Joined: 27 Apr 2019, 17:38
Running a Script Segment as a Separate Program
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.
Re: Running a Script Segment as a Separate Program
#Include
When you include one script in another one, you can also run the included script separately if you like.
When you include one script in another one, you can also run the included script separately if you like.
-
- Posts: 348
- Joined: 27 Apr 2019, 17:38
Re: Running a Script Segment as a Separate Program
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.
Re: Running a Script Segment as a Separate Program
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.
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.
-
- Posts: 348
- Joined: 27 Apr 2019, 17:38
Re: Running a Script Segment as a Separate Program
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
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.Maybe I'm overlooking something, but you could just use Run to open the second script?
Re: Running a Script Segment as a Separate Program
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.
- flyingDman
- Posts: 2848
- Joined: 29 Sep 2013, 19:01
Re: Running a Script Segment as a Separate Program
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
-
- Posts: 348
- Joined: 27 Apr 2019, 17:38
Re: Running a Script Segment as a Separate Program
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.
Re: Running a Script Segment as a Separate Program
Hallo,
it works even without .ahk file:
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():
}
Re: Running a Script Segment as a Separate Program
Cool! Even in the AHK documentation-- I never saw it.
- flyingDman
- Posts: 2848
- Joined: 29 Sep 2013, 19:01
Re: Running a Script Segment as a Separate Program
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
-
- Posts: 348
- Joined: 27 Apr 2019, 17:38
Re: Running a Script Segment as a Separate Program
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.Rohwedder wrote: ↑13 Mar 2022, 10:16Hallo,
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(): }
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.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?
Re: Running a Script Segment as a Separate Program
This is very interesting @Rohwedder. So basically I have a script that was required to create an independent script.Rohwedder wrote: ↑13 Mar 2022, 10:16Hallo,
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(): }
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
)
Have you tested this method with a very long script as a secondary script?
Also is it possible to name the script other than *?
Re: Running a Script Segment as a Separate Program
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.
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.