How to open Windows Terminal and send text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
erbanku
Posts: 7
Joined: 07 Oct 2021, 23:04

How to open Windows Terminal and send text

16 Oct 2021, 08:02

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
User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: How to open Windows Terminal and send text

16 Oct 2021, 08:08

Code: Select all

Run, %ComSpec% /k ping 8.8.8.8
erbanku
Posts: 7
Joined: 07 Oct 2021, 23:04

Re: How to open Windows Terminal and send text

16 Oct 2021, 08:26

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
User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: How to open Windows Terminal and send text

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.
erbanku
Posts: 7
Joined: 07 Oct 2021, 23:04

Re: How to open Windows Terminal and send text

16 Oct 2021, 09:11

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.
User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: How to open Windows Terminal and send text

16 Oct 2021, 16:15

Feel free to post your test script.
garry
Posts: 3758
Joined: 22 Dec 2013, 12:50

Re: How to open Windows Terminal and send text

17 Oct 2021, 11:08

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/

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: dipahk, mikeyww and 250 guests