Using Go language to automate the Windows Terminal launching

This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Tech Community.

 

 

 

 

 

Let’s start!

 

The first step is to download and install the Go language that is available at: https://golang.org/ .

 

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. 

 

 

 

Launching Windows Terminal from Windows Explorer Address Bar

 

I am pretty sure that you already know that typing CMD or PowerShell.exe on the Windows Explorer address bar will open the respective prompts in the same folder that it was displayed on Windows Explorer.

 

But how to achieve the same behavior with Windows Terminal?

 

If you type wt on the address bar, Windows Terminal will not be launched in the same folder that Windows Explorer. In fact, Windows Terminal will be opened in the folder defined in the default profile.

 

 

 

 

 

:triangular_flag: To open Windows Terminal in the same folder of Windows Explorer you need to run the following command:

 

 

wt -d .

 

 

 

 

In case you don't like to type the parameters for the wt command, you can use the language Go to reduce it to only a single command.

 

 

:folded_hands: I would like to thank my friend and teammate Alexandre Teoi that shared with me his implementation using Go to avoid having to pass parameters to wt command.

 

 

 

 

 

You can find Teoi's solution on GitHub: https://github.com/DevelopersCommunity/wtd .

 

The implementation is straightforward. The wtd.go file, available on GitHub, is used to run the wt -d . command:

 

 

 

 

Once you have installed Go, run the following code to compile and install packages:

 

go install github.com/DevelopersCommunity/wtd

 

 

:triangular_flag: It is also possible to clone the repository and install the App locally:

 

 

go install wtd.go

 

 

Now you can use wtd instead of wt to open Windows Terminal from Windows Explorer address bar without having to worry about pass the parameters.

 

 

 

 

 

 

 

 

Automating Windows Terminal launching

 

The idea of this session is to demonstrate how to open Windows Terminal with three panels opened running different commands.

 

 

 

 

The first thing that you need to do is to create the following three profiles on Windows Terminal to host each panel.

 

 

 

 

 

Follows the code that must be included in the session profiles of the settings.json file of Windows Terminal:

 

 

{
    "guid": "{0879968C-03C7-4B16-BF02-49CBF6046F59}",
    "fontFace" : "Cascadia Code PL",
    "name": "Pane1",
    "backgroundImage": "C:\\podcast\\Windows Terminal\\windows.png",
    "commandline": "powershell.exe -noexit -command c:\\users\\luisdem\\pane1.ps1"
},

{
    "guid": "{C7A929B8-F4F5-4B30-9AE4-761BD582B036}",
    "fontFace" : "Cascadia Code PL",
    "name": "Pane2",
    "backgroundImage": "C:\\podcast\\Windows Terminal\\linux.png",
    "commandline": "powershell.exe -noexit -command c:\\users\\luisdem\\pane2.ps1"
},

{
    "guid": "{F7F214CA-48DF-4CB2-8B21-54D195867B14}",
    "fontFace" : "Cascadia Code PL",
    "name": "Pane3",
    "backgroundImage": "C:\\podcast\\Windows Terminal\\linux.png",
    "commandline": "powershell.exe -noexit -command c:\\users\\luisdem\\pane3.ps1"
}


 

 

In my sample, I am running a PowerShell script for each profile (Pane1, Pane2 and Pane3).

The content of those scripts are:

 

 

Pane1.ps:

 

#First part
get-process

#Second part

[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[System.Windows.Forms.SendKeys]::SendWait("%{RIGHT}")

[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[System.Windows.Forms.SendKeys]::SendWait("%+{UP}")

[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[System.Windows.Forms.SendKeys]::SendWait("%+{UP}")

[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[System.Windows.Forms.SendKeys]::SendWait("%+{UP}")

 

 

The first part (#First part) is used to list all the processes running and the second part (#Second part) is used to invoke the short keys responsible to change the panel size.

 

%{RIGHT}: is used to invoke the Shift+Right Arrow to change the focus to the right pane.

%+{UP}: is used to invoke the ALT+Shift+Up Arrow to change the vertical size of the pane.

 

 

Pane2.ps:

 

wsl cowsay "Welcome to Windows Terminal"
wsl
wsl

 

 

Pane3.ps:

 

wsl cmatrix
wsl

 

 

Finally we can use Go to create the App that will open Windows Terminal with the three new profiles running in different panes.

 

 

The idea is to run the following command:

 

wt -p "Pane1" `; split-pane -p "Pane2" `; split-pane -H -p "Pane3"

 

 

All you have to do is to create a file, like wtp.go and implement the following code:

 

 

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    cmd := &exec.Cmd{
        Path: "cmd",
        Args: []string{"/c", "start", "wt", "-p", "Pane1", "`;", "split-pane", "-p", "Pane2", "`;", "split-pane", "-H", "-p", "Pane3"},
    }
    if err := cmd.Start(); err != nil {
        fmt.Printf("Error: %s\n", err)
    }
}

 

 

Follows the wtp.go file opened on Visual Studio Code:

 

 

 

 

 

Once we have the file, we need to compile and install the App through the following command:

 

 

go install wtp.go

 

Now, all you have to do is to execute wtp to launch this customized Windows Terminal:

 

 

 

 

This post is just to demonstrate how to use Go language to customize some actions to open Windows Terminal.

 

 

It is possible to achieve the same behavior using other languages. :beaming_face_with_smiling_eyes:

 

 

I hope you enjoyed this post. :thumbs_up:

 

REMEMBER: these articles are REPUBLISHED. Your best bet to get a reply is to follow the link at the top of the post to the ORIGINAL post! BUT you're more than welcome to start discussions here:

This site uses Akismet to reduce spam. Learn how your comment data is processed.