Show-UI Weather Widget and a Non Show-UI widget

image

Ok, this weather widget created by Joel “Jaykul” Bennett (Blog | Twitter) is just amazingly cool. This widget auto-updates the temperature and even updates the picture to reflect the current conditions outside. The requirement to run this is by having the Show-UI module downloaded on your computer. If you like to build UI’s or are curious about what it takes to build one, I would highly recommend you download this and give it a run.

Now while I have Show-UI available at home and have put a couple small UIs together for my own learning, having it at work is another story all together. The change management process is pretty brutal and I am not all that confident that it will get approval (long story and not one that I will go into here). So, being that I wanted this widget at work, I went on the task of creating a widget just like the one that was created with Show-UI so I can use it at work. While working on this, I found that the amount of code required to make this happen was almost 3 1/2 times the code required in Show-UI (110 vs. 31). That and just the look of the code to me on the Show-UI just seems more organized and easier to work with (once I got past the initial learning curve and started writing some small UI’s to get a feel for it).

This difference in the amount of code is, to me, more than enough reason to build UI’s using Show-UI. While there is a requirement for downloading and running the module in order to take advantage of this, if you have the option of doing so, then I would recommend it! I am currently in the process of taking my existing UI’s and making them using Show-UI (it is taking a little while as I am working on other projects as well both work and personal).

Some great sites showing various examples of using Show-UI

http://www.dougfinke.com/blog/index.php/category/showui/

http://blog.startautomating.com/ShowUI.posts.html

http://www.ravichaganti.com/blog/?cat=279

So what are you waiting for? Download Show-UI and put together some cool UIs to show off!

So if you’re in the same boat as me at your workplace where you waiting to get Show-UI approved for work, you can use the code below to have your own weather widget. But I would recommend you use Joel’s code if you do have Show-UI available at your office.

Non Show-UI Weather Widget download

Remove .doc extension to view code in notepad. Change extension to .ps1 to run with PowerShell.

Weather.ps1

Weather Widget Code (Non Show-UI)

Param ($ZipCode = 68123)
$Global:rs = [RunspaceFactory]::CreateRunspace()
$rs.ApartmentState = “STA”
$rs.ThreadOptions = “ReuseThread”
$rs.Open()
$psCmd = {Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase}.GetPowerShell()
$rs.SessionStateProxy.SetVariable('ZipCode',$ZipCode)
$psCmd.Runspace = $rs
$psCmd.Invoke()
$psCmd.Commands.Clear()
$psCmd.AddScript({ 

#Load Required Assemblies
Add-Type –assemblyName PresentationFramework
Add-Type –assemblyName PresentationCore
Add-Type –assemblyName WindowsBase
$update = {
    $h = (Get-Date).Hour
    $channel = ([xml](New-Object Net.WebClient).DownloadString("http://weather.yahooapis.com/forecastrss?p=$ZipCode")).rss.channel
    $temp.text = $channel.item.condition.temp + [char]176
    $HiLow.text = "Hi: {0}`tLow: {1}" -f $channel.item.forecast[0].High,$channel.item.forecast[0].low
    if($h -gt ([DateTime]$channel.astronomy.sunrise).Hour -and $h -lt ([DateTime]$channel.astronomy.sunset).Hour) {
    $dayOrNight = 'd'
    } else {
    $dayOrNight = 'n'
    }
    $Image.source = "http`://l.yimg.com/a/i/us/nws/weather/gr/{0}{1}.png" -f $channel.item.condition.code, $dayOrNight
    }
[xml]$xaml = @"
<Window
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    x:Name='Window' WindowStyle = 'None' WindowStartupLocation = 'CenterScreen' SizeToContent = "WidthAndHeight" ShowInTaskbar = 'True'
    ResizeMode = 'NoResize' Title = 'Weather' AllowsTransparency = 'True' Background = 'Transparent' Opacity = '1' Topmost = 'True'>
        <Grid x:Name = 'Grid' Background = 'Transparent'>
            <Rectangle RadiusX = '10' RadiusY = '10' StrokeThickness = '0' Width = '170' Height = '80'
             HorizontalAlignment = 'Left' VerticalAlignment = 'Top' Margin = '60,40,0,0'>
                <Rectangle.Fill>
                    <LinearGradientBrush StartPoint = '0.5,0' EndPoint = '0.5,1'>
                        <LinearGradientBrush.GradientStops>
                            <GradientStop Color='#FF007bff' Offset='0' />
                            <GradientStop Color='#FF40d6ff' Offset='1' />
                        </LinearGradientBrush.GradientStops>
                    </LinearGradientBrush>
                </Rectangle.Fill>
            </Rectangle>
        <TextBlock x:Name = 'temp' FontSize=  '80' Foreground = 'White' Margin  = '130,0,0,0'>
            <TextBlock.Effect>
                <DropShadowEffect Color = 'Black' ShadowDepth = '0' BlurRadius = '8' />
            </TextBlock.Effect>
        </TextBlock>
        <TextBlock x:Name = 'HiLow' FontSize=  '13' Foreground = 'White' Margin  = '90,95,0,0'>
            <TextBlock.Effect>
                <DropShadowEffect Color = 'Black' ShadowDepth = '0' BlurRadius = '8' />
            </TextBlock.Effect>
        </TextBlock>
        <Image x:Name = 'Image' Stretch = 'Uniform' Width = '250.0' Height = '180.0' />
        </Grid>
</Window>
"@

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Global:Window=[Windows.Markup.XamlReader]::Load( $reader )

$Image = $Window.Content.Children | Where {$_.Name -eq 'Image'}
$temp = $Window.FindName("temp")
$location = $Window.FindName("location")
$HiLow = $Window.FindName("HiLow")

$Window.Add_MouseLeftButtonDown({
    $This.DragMove()
    })

#Timer Event
$Window.Add_SourceInitialized({
    #Create Timer object
    Write-Verbose "Creating timer object"
    $Global:timer = new-object System.Windows.Threading.DispatcherTimer
    #Fire off every 1 minutes
    Write-Verbose "Adding 1 minute interval to timer object"
    $timer.Interval = [TimeSpan]"0:1:0.00"
    #Add event per tick
    Write-Verbose "Adding Tick Event to timer object"
    $timer.Add_Tick({
        Try {
            &$Update
            }
        Catch {
            $temp.text = 'N/A'
            }
        [Windows.Input.InputEventHandler]{ $Global:Window.UpdateLayout() }
        Write-Verbose "Updating Window"
        })
    #Start timer
    Write-Verbose "Starting Timer"
    $timer.Start()
    If (-NOT $timer.IsEnabled) {
        $Window.Close()
        }
    })
$Window.Add_Loaded({
    Try {
        &$Update
        }
    Catch {
        $temp.text = 'N/A'
        }
    })
$Window.ShowDialog() | Out-Null
}).BeginInvoke() | out-null
This entry was posted in powershell, scripts and tagged , , , , . Bookmark the permalink.

2 Responses to Show-UI Weather Widget and a Non Show-UI widget

  1. I suggest adding a download link for your code sample as an ASCII text file so readers don’t have to rely on trying to copy and paste.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s