![Microsoft Windows]()
In this article, I'll give you the code to create voice interactions in your scripts for Microsoft Windows.
The source code is available on this page; download it and follow these steps.
- Unzip the downloaded file.
- Open in Visual Studio the Solution.
![Visual Studio]()
- Check the code on the Program. cs and check the code.
using System.Speech.Synthesis;
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main(string[] args)
{
PromptSpeech(args.Length == 0 ? ["Please pass the command to speech!"] : args);
}
private static void PromptSpeech(string[] args)
{
var voice = new SpeechSynthesizer();
voice.SetOutputToDefaultAudioDevice();
voice.Rate = 1;
voice.Speak(string.Join(" ", args));
}
}
- On the Solution Explorer, right-click on SpeechPrompt and select Publish.
![Solution Explorer]()
- Publish option.
![Publish option]()
- Click the Publish button.
![Publish]()
- After the publish, click on the icon to copy the path to the clipboard.
![Clipboard]()
- Copy the .exe file.
![.exe fileĀ]()
- to C:\SpeechCmd.
![SpeechCmd]()
How do I use this command?
Call from your bat file.
C:\SpeechCmd\SpeechPrompt.exe WITH YOUR MESSAGE TO SPEECH
or from PowerShell.
Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "MESSAGE TO SPEECH"
Now you can try this batch script sample.bat attached to the project.
@echo off
C:\SpeechCmd\SpeechPrompt.exe Please confirm the drivers backup pressing enter
echo Please confirm the drivers backup pressing enter
pause
REM Check if the temporary directory exists and create it if it doesn't
if not exist C:\temp\driversWindow (
C:\SpeechCmd\SpeechPrompt.exe Creating temporary directory
mkdir C:\temp\driversWindow
)
C:\SpeechCmd\SpeechPrompt.exe Copying files from the drivers directory to the temporary directory
REM Copy files from the drivers directory to the temporary directory
xcopy C:\Windows\System32\drivers\* C:\temp\driversWindow\ /E /H /C /I
REM Change to the temporary directory
cd /d C:\temp
REM Compress the files into a ZIP archive using PowerShell
C:\SpeechCmd\SpeechPrompt.exe Compressing files into a ZIP archive
powershell Compress-Archive -Path C:\temp\driversWindow\* -DestinationPath C:\temp\driversWindow.zip
REM Check if the ZIP file was created successfully
if exist C:\temp\driversWindow.zip (
REM Delete the temporary directory and its contents
rd /s /q C:\temp\driversWindow
) else (
C:\SpeechCmd\SpeechPrompt.exe Zip file creation failed. Temporary files not deleted.
echo Zip file creation failed. Temporary files not deleted.
)
REM Notify that the process is complete
echo Process complete. The ZIP file is located at C:\temp\driversWindow.zip
C:\SpeechCmd\SpeechPrompt.exe Process complete. The ZIP file is located at C:\temp\driversWindow.zip
C:\SpeechCmd\SpeechPrompt.exe Thanks for using this script
pause
If you start the sample.bat, it will complete smoothly, but the sample.ps1 in PowerShell will not complete successfully because PowerShell can't access the System32\Drivers subfolders.
And try the sample.ps1.
# PowerShell script equivalent of the provided batch script
# Notify the user to confirm the drivers backup
Write-Host "Please confirm the drivers backup pressing enter"
Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Please confirm the drivers backup pressing enter"
Read-Host -Prompt "Press Enter to continue..."
# Check if the temporary directory exists and create it if it doesn't
$temporaryDir = "C:\temp\driversWindow"
if (-Not (Test-Path $temporaryDir)) {
Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Creating temporary directory"
New-Item -ItemType Directory -Path $temporaryDir
}
# Notify the user that files are being copied
Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Copying files from the drivers directory to the temporary directory"
# Copy files from the drivers directory to the temporary directory
$sourceDir = "C:\Windows\System32\drivers\*"
Copy-Item -Path $sourceDir -Destination $temporaryDir -Recurse -Force
# Notify the user that files are being compressed
Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Compressing files into a ZIP archive"
# Compress the files into a ZIP archive
$zipFilePath = "C:\temp\driversWindow.zip"
Compress-Archive -Path "$temporaryDir\*" -DestinationPath $zipFilePath -Force
# Check if the ZIP file was created successfully
if (Test-Path $zipFilePath) {
# Delete the temporary directory and its contents
Remove-Item -Path $temporaryDir -Recurse -Force
# Notify the user that the process is complete
$completionMessage = "Process complete. The ZIP file is located at $zipFilePath"
Write-Host $completionMessage
Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList $completionMessage
Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Thanks for using this script"
} else {
# Notify the user of the failure
Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Zip file creation failed. Temporary files not deleted."
Write-Host "Zip file creation failed. Temporary files not deleted."
Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Sorry for the inconvenience"
}
Read-Host -Prompt "Press Enter to exit..."
Q&A
1. How can you incorporate the provided code for voice interactions into your scripts?
You can choose a folder other than C:\SpeechCmd\ and distribute the SpeechPrompt.exe with your solution and point to this new path the call C:\SpeechCmd\SpeechPrompt.exe, any argument passed to it will be speeches.
2. What are the specific functionalities and features of the SpeechPrompt tool?
The SpeechPrompt will speech anything passed as arguments.
3. Are any dependencies or prerequisites required to use the provided code for voice interactions in scripts?
It runs on Windows, and this sample requires that .NET 8 binaries be installed to work.
Conclusion
Incorporating voice interactions into your scripts for Microsoft Windows can enhance user experience and accessibility. By following the provided code and steps, you can enable voice prompts and notifications within your scripts, making them more interactive and user-friendly. This can be particularly useful in scenarios where visual interaction may not be feasible or convenient. Integrating voice interactions can add a valuable dimension to your scripts and improve user experience.