I wrote a previous entry about the ability to generate speech from text. This time, I provide a semi-practical use for doing so.
In this example, we are scanning our outlook 2010 inbox folder for new emails. If one is found, we use the SAPI.SPVoice object to playback the FROM and SUBJECT fields.
A simple but effect way to use text to speech from powershell. Here is the entire script:
###### ### ### Powershell Script to review Outlook 2010 Email and provide TTS ### alerts when a new message is delivered - just for fun! ### ### Version 1.2 ### Patrick McKibbins ### 8/21/13 ### ###### Clear-Host $ScriptVersion = "Version 1.2" $ScriptFileName = $MyInvocation.MyCommand.Definition Write-Host ("`n{0}`n" -f " [ Program: ] [ $ScriptFileName ] [ $ScriptVersion ]") -ForegroundColor Yellow #Function to process text to speech function mySay { param( [Parameter(Mandatory=$true)] $Text, [switch] $Slow ) $object = New-Object -ComObject SAPI.SpVoice if ($Slow) { $object.Rate = -10 } $object.Speak($text) | Out-Null } # Verify Volume is loud enough mySay "Testing Volume." # Setup MAPI connection to local Outlook client $OutLookFolderInbox = 6 $outlook = new-object -com outlook.application; $NameSpace = $outlook.GetNameSpace("MAPI"); # Create an Endless Loop that simply scans the inbox for new emails # If a new email is found, it marks it as read and notifies you via TTS alert $a=0 #Loop Count $b=0 #Message Count while($true) { # Gather inbox details $inbox = $NameSpace.GetDefaultFolder($OutlookFolderInbox) #checks 10 newest messages / $found = $false unless new message is found $Found = $false $inbox.items | select -first 10 | foreach { if($_.unread -eq $True) { $Found = $true $b++ # build strings of details for TTS # Also replacing RE: with 'Reply' for TTS clarity # Replace FW: with 'Forwarded Message' $s1 = "You have a new email message from: " + $_.SenderName + ("`n") $s2 = "Subject: " + $_.Subject + ("`n") -replace "RE: ", "Reply, " $s2 = $s2 -replace "FW: ", "Forwarded Message, " # Output details to screen for review $d0 = " Date: " + (get-date).tostring() $d1 = " From: " + $_.SenderName + ("`n") $d2 = " Subject: " + $_.Subject + ("`n") $d3 = " MSG : [" + $b +"] LC: (" +$a +")" Write-Host ("`n{0}`n" -f " ----- ") -ForegroundColor blue Write-Host ("{0}" -f $d3) -ForegroundColor cyan Write-Host ("{0}" -f $d0) -ForegroundColor green Write-Host ("{0}" -f $d1) -ForegroundColor green Write-Host ("{0}" -f $d2) -ForegroundColor green Write-Host ("{0}" -f " ----- ") -ForegroundColor blue # Mark current new message as read $_.UnRead = $false # TTS Output of details mySay $s1 mySay $s2 } } # Sleep for 5 seconds then rinse and repeat start-sleep 5 # $a is just being used to let us know how many times the loop has been processed - it serves no other purpose $a++ # write-host $a " " -nonewline } ###### ### ### The End... ### ######
I’ve included some minor remarks in the script to help understand the flow. If needed, I can provide a PART 3 that breaks it down further at a later date.
So there you have it! A practical use for Text to Speech via Powershell that is quick and easy!
— PFM