PoshCode Archive  Artifact Content

Artifact de1a736e3e2e8b057a3c0d13f74f4d1cdc800ee3fe3b4897638426462b3b9400:

  • File Start-Timer.ps1 — part of check-in [bc08d76b9d] at 2018-06-10 12:59:07 on branch trunk — The kitchen timer with a kitchen sink -full of features (user: Joel Bennett size: 2240)

# encoding: ascii
# api: powershell
# title: Start-Timer
# description: The kitchen timer with a kitchen sink -full of features
# version: 0.1
# type: script
# author: Joel Bennett
# license: CC0
# x-poshcode-id: 161
# x-archived: 2017-05-08T15:02:23
# x-published: 2008-03-17T21:05:00
#
#
## Start-Timer.ps1
## A kitchen timer script with visible countdown and customizable audio alert and messages
####################################################################################################
param( $seconds=0,  $reason="The Timer",  $SoundFile="$env:SystemRoot\Media\notify.wav",
      $minutes=0,  $hours=0,  $timeout=0,  [switch]$novoice, [string[]]$status="Cooking","Burning"
)

$start = [DateTime]::Now
write-progress -activity $reason -status "Running"

$seconds = [Math]::Abs($seconds) + [Math]::Abs($minutes*60) + [Math]::Abs($hours*60*60)
$end = $start.AddSeconds( $seconds )

## Take care of as much overhead as we can BEFORE we start counting
## So the sounds can play as cloase to the end as possible
$Voice = new-object -com SAPI.SpVoice
$Sound = new-Object System.Media.SoundPlayer

if(Test-Path $soundFile) {
   $Sound.SoundLocation=$SoundFile
} else {
   $soundFile = $Null
}

## The actual timing loop ... 
do {
   $remain  = ($end - [DateTime]::Now).TotalSeconds
   $percent = [Math]::Min(100, ((($seconds - $remain)/$seconds) * 100))
   $sec     = [Math]::Max(000, $remain)

   write-progress -activity $reason -status $status[0] -percent $percent -seconds $sec
   start-sleep -milli 100
} while( $remain -gt 0 )

## And a loop for beeping
$Host.UI.RawUI.FlushInputBuffer()
do {
   if($SoundFile) {
      $Sound.PlaySync()
   } else {
      [System.Media.SystemSounds]::Exclamation.Play()
   }
   if(!$novoice -and !$Host.UI.RawUI.KeyAvailable){
     $null = $Voice.Speak( "$reason is $($status[1])!!", 0 )
   }

   write-progress -activity $reason -status $status[1] -percent ([Math]::Min(100,((($seconds - $remain)/$seconds) * 100))) -seconds ([Math]::Max(0,$remain))
   $remain = ($end - [DateTime]::Now).TotalSeconds
} while( !$Host.UI.RawUI.KeyAvailable -and ($timeout -eq 0 -or $remain -gt -$timeout))

if($SoundFile) {
   $Sound.Stop() 
}