• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Tachytelic.net

  • Get in Touch
  • About Me

Scripts & Utilities

Emailing from Turbosoft TTwin 4 Terminal Emulator with VBA Script

November 12, 2019 by Paulie Leave a Comment

I’ve got a lot of customers with legacy systems, by chance most of them use Century TinyTerm. One, who uses TTwin 4 by Turbosoft asked if it would be possible to initiate an email from the contents of the emulation screen. Take this screen for example:

Image of typical text based legacy system using TTWin 4 to connect.

I investigated and thought it should be possible. TTwin uses VBA engine provided by WinWrap . It is easy to capture the contents of a fixed portion of the screen, but the email address could be anywhere in the display. A slight complication.

So I write a script which could be activated by a mouse shortcut (which is quite a neat feature of this emulator). The script scans the screen for email addresses and then initiates the default mail client on the PC to send an email. Here is the code:

Option Explicit

Sub Main
	Dim screenContents() As String, emailAddress As String
	ScreenContents = getScreenContents(24, 80)
	emailAddress = findEmailOnScreen()
	If emailAddress <> "notFound" Then
		Shell ("rundll32.exe url.dll,FileProtocolHandler mailto:" & emailAddress, 1)
	End If
End Sub

'Function to search for an email address on screen
Function findEmailOnScreen() As String
	Dim ScreenContents() As String, Screenline() As String
	Dim regExp As New RegExp, i As Integer, j As Integer
	ScreenContents = getScreenContents(24,80)

	With regExp
		.Global = True
		.Multiline = True
		.IgnoreCase = True
		.Pattern = "((?:[A-Z0-9_%+-]+\.?)+)@((?:[A-Z0-9-]+\.)+[A-Z]{2,4})$"
	End With

	For i = 0 To UBound(ScreenContents)
		Screenline() = Split(ScreenContents(i), " ")
		For j = 0 To UBound(Screenline)
			If(regExp.Test(Screenline(j))) Then
				findEmailOnScreen=Screenline(j)
				Exit Function
			End If
		Next		
	Next

	findEmailOnScreen = "notFound"
End Function

'Function to get the entire contents of the screen as an array
Function getScreenContents(Height As Integer, width As Integer)
	Dim screenLines() As String
	ReDim screenLines(Height)
	Dim lineText As String, count As Integer

	Do
		TTWin.DispReadText  count,0,lineText,width
		screenLines(count) = lineText
    	count=count+1
	Loop Until count = Height
	getScreenContents=screenLines()
End Function

It works like this:

  • Loads each line of the display into an array
  • Splits each line of the array by the space character into another array.
  • Uses regex to see if any element of the array looks like an email address
  • Opens the default mail client if a match is found using rundll32.exe

The script could be improved but it works well and it is a decent start.

Image showing Email being initiated from a VBA Script within Powersoft TTWin 4
Mouse Shortcut of Right-Click Shift initiates the script and a new email is created.

TinyTerm has the same scripting engine as TTWin 4, I tried and failed to create the same function with TinyTerm. I am sure it is possible, but the programmers documentation wasn’t clear to me.

This is the first time I have used TTWin, it seems like a really good product, things I like about it:

  • Multiple sessions support is good with thumbnails in a dedicated panel.
  • Scripting engine is easy to get to grips with.
  • Hot spots, keyboard and mouse events are easy to setup to provide quick automation.
  • The macro recorder is easy to use and works well.

The programmers documentation could be better, more example code would help a lot.

Do you have a text based legacy system? If so I offer lots of ways to enhance and modernise, just get in touch to discuss your requirements.

Filed Under: Scripts & Utilities Tagged With: VBA

How to add Thousands separators to a number with Powershell

October 8, 2019 by Paulie Leave a Comment

When working with long numbers, they are much easier to read with the thousand separators included. The Powershell format operator makes it simple.

$Number = 2109999.86
'{0:N0}' -f $Number
'{0:N2}' -f $Number

Produces the following output:

As you can see from the above, the first example produces a whole number with no decimal places, and the second one includes two decimal places.

In practice, when I needed to do this, the number provided to me was actually a string read from a CSV File, so although you can use the format operator, it won’t do anything:

Image showing Powershell trying to add thousand separators to a number stored as a string.

The string needs to be cast as a number before you can use the format operator in this way:

$StringNumber = "10456.21" 
$($StringNumber -as [decimal]).ToString('N2')

Produces a number with separators:

You can use the format operator for all sorts of things. You might like this post on how to format leading zeros with the format operator.

Filed Under: Scripts & Utilities Tagged With: Powershell

VBScript lpad and rpad functions

May 23, 2019 by Paulie 1 Comment

VBScript does not include any functions to left pad or right pad a string, so these two functions will enable you to do just that.

Function LPad(StringToPad, Length, CharacterToPad)
  Dim x : x = 0
  If Length > Len(StringToPad) Then x = Length - len(StringToPad)
  LPad = String(x, CharacterToPad) & StringToPad
End Function

Function RPad(StringToPad, Length, CharacterToPad)
  Dim x : x = 0
  If Length > Len(StringToPad) Then x = Length - len(StringToPad)
  RPad = StringToPad & String(x, CharacterToPad)
End Function

Add a Leading Zero

Left pads the number four to a length of two with a zero:
LPad("4", 2, "0")

Add a Trailing Zero

Right pads the number four to a length of two with a zero:
RPad("4", 2, "0")

Left Pad with spaces

Left pads a string with spaces to a length of ten:
LPad("Hello ", 10, " ")

Right Pad with spaces

Right pads a string with spaces to a length of ten:
RPad("hello", 10, " ")

It is a shame there isn’t a native way to pad with VBScript. But these small functions make it simple. I’ve also written up how to do padding with Powershell, which makes things much easier.

Filed Under: Scripts & Utilities Tagged With: VBScript

How to Format a variable with leading zeros in Powershell

May 23, 2019 by Paulie 1 Comment

If you need to format a number in Powershell to include leading zeros, so that the length is always a fixed width, it is easy to do with Powershell format operator.

I was working with some data that stored dates as single digits for those days and months that only contained a single digit. For example I needed “1/1/2019” to be “01/01/2019”.

Using the powershell “-f” operator it is easy to add the leading zeros. For example:

PS C:\> "{0:d2}/{1:d2}/{2:d4}" -f 1,1,2019
01/01/2019

You can easily wrap this in a loop like this:

foreach ($months in @(1..12)) 
{
    $Month = "{0:d2}" -f $months
    foreach ($days in @(1..31))
    {
        $Day = "{0:d2}" -f $days
        $ArchiveFolder =  "D:\Archive\PDF\2019\$month\$day"
        Write-Host $ArchiveFolder
    }
    
}

Image of PowerShell loop padding a single digit number with leading zeros

Filed Under: Scripts & Utilities Tagged With: Powershell

Use PowerShell to Set Environment Variables

March 11, 2019 by Paulie 5 Comments

Setting environment variables in PowerShell is easy. This post will show you how to create a PowerShell environment variable which is scoped:

  • Locally to your current PowerShell session.
  • To your user profile and available to all other processes that you execute
  • To the machine, and accessible to all processes that run on that system.

Set a locally scoped Environment Variable

To create an environment variable that is local to your current PowerShell session, simply use:

$env:SiteName = 'tachytelic.net'

Then you can check the value of the environment variable with this code:

Get-ChildItem Env:SiteName

Image showing how to set a local Environment Variable in Powershell

This method is fine, but the variable will vanish when the PowerShell process ends.

Set an Environment Variable scoped to the User

To set an environment variable which will be available to all processes that your account runs, use the following:

[System.Environment]::SetEnvironmentVariable('siteName','tachytelic.net',[System.EnvironmentVariableTarget]::User)

Now you can see the variable is set from the Environment variables section of the system properties:

Image showing currently set User Environment Variables

Set an Environment Variable scoped to the Machine

To create an environment variable visible to every process running on the machine:

[System.Environment]::SetEnvironmentVariable('siteName','tachytelic.net',[System.EnvironmentVariableTarget]::Machine)

Note: This command will probably fail unless you run PowerShell as an administrator.

The new PowerShell System environment variable is visible:

Image showing system environment variable added by PowerShell

 

It’s quite interesting that the PowerShell environment variables are stored in a drive, which you can access using:

Set-Location Env:
Get-ChildItem

For more information on how PowerShell environment variable work, read the Microsoft documentation here.

Filed Under: Scripts & Utilities Tagged With: Powershell

  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Interim pages omitted …
  • Go to page 5
  • Go to Next Page »

Primary Sidebar

Link to my LinkedIn Profile
Buy me a coffee

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 246 other subscribers.

Go to mobile version