Using Go language to automate the Windows Terminal launching
Published Jun 17 2020 09:09 AM 9,748 Views
Microsoft

 

logo.png

 

 

go.jpg

 

 

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. 

 

go learning.png

 

 

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.

 

 

wt.gif

 

 

 

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

 

 

wt -d .

 

 

wt param.gif

 

 

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.

 

 

 

git.jpg

 

 

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:

 

 

wtd go.png

 

 

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.

 

 

wtd.gif

 

 

 

 

go learning.png

 

 

Automating Windows Terminal launching

 

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

 

 

wt split.gif

 

 

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

 

 

wt profiles.png

 

 

 

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:

 

 

wtp vscode.png

 

 

 

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:

 

 

wtp run.png

 

 

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:

 

3 Comments
Copper Contributor

For those looking to try this implementation,
There is a line in the Windows Terminal profile code: 

   "backgroundImage": "C:\\podcast\\Windows Terminal\\linux.png",

this line can be modified to point to another background picture of your choosing. 

I searched for this very thing and found myself here. Nicely done!
I think I will make a PowerShell function version of this. 
 

 
Thanks, @luisdem,


 

Microsoft

Thank you very much @ctmcisco !!!!!

Copper Contributor

Seems there have been some enhancements to wt.exe argument docs as of today 11/11/2020

https://docs.microsoft.com/en-us/windows/terminal/command-line-arguments?tabs=windows


Version history
Last update:
‎Jun 17 2020 09:14 AM
Updated by: