Page 1 of 1

How to open Windows Terminal and send text

Posted: 16 Oct 2021, 08:02
by erbanku
I tried the code below, this can run normally on legacy Windows cmd, but doesn't work on Windows Terminal (before run this code I already set Windows Terminal as default command prompt app, and 'RUn cmd'will open Windows Terminal).

Any and all ideas would be deeply appreciated.

Code: Select all

SetTitleMatchMode 2
Run cmd
WinWait, ahk_class ConsoleWindowClass,
IfWinNotActive, ahk_class ConsoleWindowClass, , WinActivate, ahk_class ConsoleWindowClass,
WinWaitActive, ahk_class ConsoleWindowClass,
ControlSend, , ping 8.8.8.8 -t {Enter}, cmd.exe

Re: How to open Windows Terminal and send text

Posted: 16 Oct 2021, 08:08
by mikeyww

Code: Select all

Run, %ComSpec% /k ping 8.8.8.8

Re: How to open Windows Terminal and send text

Posted: 16 Oct 2021, 08:26
by erbanku
mikeyww wrote:
16 Oct 2021, 08:08

Code: Select all

Run, %ComSpec% /k ping 8.8.8.8
Thank you, Sir.
I have another issue with this.
I'd like to open the terminal and then redirect to my GitHub local repo directory and then run the commands below.

Code: Select all

gh workflow run
gh run watch
I would be deeply appreciative if you can help.
My wrong codes which have some issues:

Code: Select all

ControlSend, , pushd C:\Users\Erban Ku\Documents\GitHub\awesome {Enter}, cmd.exe
ControlSend, , gh workflow run {Enter}, cmd.exe
ControlSend, , {Enter}, cmd.exe
sleep, 2000
ControlSend, , gh run watch {Enter}, cmd.exe
ControlSend, , {Enter}, cmd.exe
sleep, 60000
ControlSend, , exit {Enter}, cmd.exe

Re: How to open Windows Terminal and send text

Posted: 16 Oct 2021, 08:29
by mikeyww
Although I do not use this program, my advice as a first step is to run cmd.exe, and then run a script that uses Send instead of ControlSend to send to your active CMD window. See if you can get that working first. Try just one or two commands first, instead of the entire set. This sort of incremental troubleshooting will get you to your final script more quickly.

Re: How to open Windows Terminal and send text

Posted: 16 Oct 2021, 09:11
by erbanku
mikeyww wrote:
16 Oct 2021, 08:29
Although I do not use this program, my advice as a first step is to run cmd.exe, and then run a script that uses Send instead of ControlSend to send to your active CMD window. See if you can get that working first. Try just one or two commands first, instead of the entire set. This sort of incremental troubleshooting will get you to your final script more quickly.
Tried these for about 30 minutes, ended up with failing.

After opening the Terminal and redirect to the repo path, I need to insert "gh workflow run" command and press {Enter}
and then
wait for about 2 seconds
insert "gh run watch" command and press {Enter}
done.

Re: How to open Windows Terminal and send text

Posted: 16 Oct 2021, 16:15
by mikeyww
Feel free to post your test script.

Re: How to open Windows Terminal and send text

Posted: 17 Oct 2021, 11:08
by garry
this was an example with GWBASIC.exe ( which no more works with WIN10 64-bit )
start DOS and then GWBASIC.exe , load xy.bas , run xy.bas, list xy.bas, exit
( can run this script , see DOS for 7 seconds )

Code: Select all

;-- start GWBASIC
;-- start DOS with big black screen , but GWBASIC allows only width=80 lines=25

#warn
#NoEnv                       ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input               ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
;----------------------------
PR=c:\gwbasic.exe            ;-- here is gwbasic.exe
;----------------------------
SetKeyDelay, 80, 10
wa:=a_screenwidth,ha:=a_screenheight,W:=(wa*50)/100,H:=(ha*92)/100
e4x=
(Ltrim join&
@echo off
echo date=
date /t
echo time=
time /t
)
title2=DOS_TEST
run, %comspec% /T:0A /k "title %title2%&mode con lines=4000 cols=120&%e4x%,,,pid2
WinWait, ahk_pid %pid2%
WinMove, ahk_pid %pid2%, ,1,1,%w%,%h%       ;- move DOS window to the defined position
WinWaitactive, ahk_pid %pid2%
SendInput {text}ver`n
sleep,1000
controlsend,ahk_parent,{text}cd\`n,ahk_pid %pid2%
ifexist,%pr%
  {
  sleep,500
  ControlSend,ahk_parent,{text}gwbasic`r,ahk_pid %PID2%           ;-- run GWBASIC
  sleep,500
  ControlSend,ahk_parent,{text}Load "Sunset"`r,ahk_pid %PID2%    ;-- a GWBASIC command opens 'Sunset.bas'
  sleep,500
  ControlSend,ahk_parent,{text}run`r,ahk_pid %PID2%               ;-  run loaded GWBASIC program 
  sleep,5000
  ControlSend,ahk_parent,{text}list`r,ahk_pid %PID2%              ;-  list GWBASIC script
  sleep,5000                                                      ;-- wait 5 seconds
  ControlSend,ahk_parent,{text}system`r,ahk_pid %PID2%            ;-- quit GWBASIC
  }
ControlSend,ahk_parent,{text}@echo Quit this DOS in 6 seconds`r,ahk_pid %PID2%
sleep,6000
ControlSend,ahk_parent,{text}exit`r,ahk_pid %PID2%        ;-- quit DOS after 6 seconds
return
https://github.blog/2021-04-15-work-with-github-actions-in-your-terminal-with-github-cli/