This article introduces how to create a simple timer app with WSL Bash and PowerShell.
It’s a timer that notifies you with voice. It displays elapsed time when the timer expires.
e.g. ttstimer "15m" "cooking" will notify you with voice saying cooking timer fired. 15 m passed. after 15 minutes.
This program sets a timer for a specified time interval, and when the timer ends, it reads the specified text aloud using speech synthesis. It also notifies the elapsed time. Below I’ll add comments to each line and then explain the program’s operation step by step.
ttstimer(){
# Start of function to set timer
: e.g. ttstimer "15m" "cooking"
# Example function call: call this function with arguments like ttstimer "15m" "cooking"
json=
ttstimer_d=
ttstimer_w=
# Initialize json variable, ttstimer_d variable, ttstimer_w variable
[ -z "${1}" ] && return 1
# If the first argument is empty, exit function as error
json="$(python3 - $@ <<EOF
# -*- coding: utf-8 -*-
import sys
args = sys.argv
import re
import json
text = """${1}"""
result = re.match(r'(\d+)([a-z]{1})', text, re.M)
print (json.dumps(list(result.groups())))
EOF
)"
# Execute Python script and store result in json variable
[ -z "${json}" ] && return 1
# If json variable is empty, exit function as error
ttstimer_d="$( echo $json | parsejson '[0]' )"
ttstimer_w="$( echo $json | parsejson '[1]' )"
# Parse json content and store in ttstimer_d and ttstimer_w
# sleep ${1} && tts "${2} timer fired. ${ttstimer_d} ${ttstimer_w} passed." && termdown
sleep ${1} && ( tts "${2} timer fired. ${ttstimer_d} ${ttstimer_w} passed." & termdown )
# Wait for specified time, read specified text with speech synthesis, and notify elapsed time
# Execute in background using &
}
The main operation steps of this program are as follows:
-
The
ttstimerfunction starts. -
The function receives a time interval (e.g., “15m”) and text (e.g., “cooking”) as arguments.
-
Initialize the
json,ttstimer_d, andttstimer_wvariables. -
If the first argument is empty, exit the function as an error.
-
A Python script is executed, the specified time interval (e.g., “15m”) is broken down using regular expressions, and the result is stored in the
jsonvariable in JSON format. -
If the
jsonvariable is empty, exit the function as an error. -
The content of the
jsonvariable is parsed and stored in thettstimer_dandttstimer_wvariables. These are values that split the time interval into number and unit. -
Sleep for the specified time interval (e.g., “15m”), then read the specified text (e.g., “cooking”) aloud using speech synthesis. It also notifies the elapsed time. This processing is executed in the background.
It started as just 3 lines but grew before I knew it.
Other Code
The purpose of the following scripts is to parse JSON data using the parsejson function in .bashrc and read the parsed data aloud using speech synthesis with the tts function. The tts function in the PowerShell script is also used for the same purpose, but performs speech synthesis within PowerShell.
// .bashrc
parsejson(){
: e.g. parsejson '[0]["title"]'
python3 -c "import json,sys;print(json.load(sys.stdin)${1})"
}
tts(){
powershell.exe - <<EOF
tts "${1}"
EOF
}
// Microsoft.PowerShell_profile.ps1
function tts(){
Add-Type -AssemblyName System.speech
$tts = New-Object System.Speech.Synthesis.SpeechSynthesizer
$Phrase = @"
<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis"
xml:lang="en-US">
<voice xml:lang="en-US">
<prosody rate="1">
<p>$($args[0])</p>
</prosody>
</voice>
</speak>
"@
$tts.SpeakSsml($Phrase)
}
.bashrc file:
-
parsejsonfunction: A function to extract specific values from JSON data. For example, callingparsejson '[0]["title"]'extracts the value at the specified location in the JSON data. Uses Python to parse the JSON data. -
ttsfunction: Calls PowerShell to read the specified text aloud using speech synthesis.
Microsoft.PowerShell_profile.ps1 file:
ttsfunction: A function to execute speech synthesis within PowerShell. Uses theSystem.Speech.Synthesis.SpeechSynthesizerclass to read the specified text aloud using speech synthesis. The text is provided in SSML (Speech Synthesis Markup Language) format, and speech properties (rate, etc.) can be configured.