how to wake up with powershell
Introduction
I was recently hired for a new job which meant I had to start waking up at 6 am again. I absolutely hate normal beeping alarms. I went to a few malls searching for a less irritating alarm clock -- either one of those natural alarms or a zen alarm. I wasn't able to find either, so I decided to make my own.
This will assume the following:
- You use iTunes
- You can hear music on your computer from your bed
- You have the iTunes / Powershell script written by Lars Brandt
The Music
First, I created a playlist in iTunes called wakeup. For those interested, here's the list and here's how I made it:
$itunes = new -com iTunes.Application
$sources = $itunes.sources
$library = $sources.ItemByName("Library")
$wakeup = $library.Playlists | where { $_.name -eq "wakeup" }
$tracks = $wakeup.Tracks
$tracks | format-table -prop artist, name, album | Out-File "Desktop\wakeup-list.txt"
Making the Alarm
I wanted my alarm to do two things:
- Set the volume to 0 and gradually raise to 50%
- Play 5 random songs from the
wakeupplaylist while doing this
Here's what I came up with:
$itunes = new -com itunes.application
$random = new random
$wakeuplist = itgetlist | where {$_.name -eq "wakeup"}
$songCount = 5
$itunes.soundvolume = 0
$i = 0
while ($i -lt $songCount) {
if ($itunes.playerstate) {
if ($itunes.soundvolume -lt 50) {
$itunes.soundvolume += 5
sleep 20
}
} else {
$i += 1
$song = $wakeuplist.tracks.item($random.next($wakeuplist.tracks.count))
$itunes.playfile($song.location)
sleep 5
}
if ($song.name -ne $itunes.currenttrack.name) {
$itunes.stop()
}
}
The hardest part was getting the next random song in line to play. When iTunes finishes a song, it will start playing the next song in your Library. To fix, I added a clause that checks if the name of the song playing is the same as the current random song. If it's not, iTunes will stop and the script will trigger the next song.
Scheduling the Alarm
The final step was to schedule the alarm to go of at 5:55am every week day. To do this, I made a Scheduled Task that ran the following command:
C:\\WINDOWS\\system32\\WINDOW~2\\v1.0\\powershell.exe -command "C:\wakeup.ps1"