PowerShell, ShowUI and the Twitter API–Part 2

July 9, 2011

in Microsoft,PowerBoots,PowerShell,twitter,Video Tutorial,WPF,WPK

Part 2 builds on the first 30 lines from PowerShell, ShowUI and the Twitter API–Part 1 (Check out the video tutorial for building it) and positions the script for the next set of steps.

More updates coming soon.

image

Updates

This version lets you view multiple Twitter search feeds. Type in a search and press enter and another column is displayed. You can delete a column too.

PowerShell Code

Download it HERE from my repository. Download ShowUI.

Import-Module showui            
            
function Search-Twitter {            
    param (             
        [Parameter(ValueFromPipeline=$true)]            
        $query            
    )            
                
    Begin { $wc = New-Object Net.Webclient }            
                
    Process {            
        $url = "http://search.twitter.com/search.rss?q=$query"            
        ([xml]$wc.downloadstring($url)).rss.channel.item |            
            Select *            
    }            
}            
            
function New-TwitterFeed ($search) {            
            
    $guid = "g" + [guid]::NewGuid() -replace "-",""            
                
    $buttonAttributes = @{            
        Margin = 5            
        HorizontalAlignment = "Right"            
        Height = 25             
        Tag = $guid             
    }            
                
    $labelAttributes = @{            
        FontSize = 20             
        Margin = 5             
        Foreground = "White"            
    }            
                
    Grid -Name $guid -Rows 45, 100* -Tag $search {            
        Grid -Row 0 -Columns 2 {                        
            Label -Column 0 $Search @labelAttributes            
            Button " X " -Column 1 @buttonAttributes -On_Click {            
                $name = "`$$($this.tag)"            
                $targetLayout.Children.Remove(($name|iex))                            
            }            
        }            
                    
        ListBox -Background Black -Row 1 -Margin 5 -DataContext {            
            Search-Twitter $search            
        } -DataBinding @{            
            ItemsSource="."            
        } -ItemTemplate {            
            $tweetTbAttributes = @{            
                FontSize = 12            
                Margin = 5             
                TextWrapping = "wrap"            
                Foreground = "White"            
            }            
                                  
            Grid -Columns 55, 300 {            
                Image     -Name Image  -Column 0 -Margin 6            
                TextBlock -Name Title @tweetTbAttributes -Column 1             
            } | ConvertTo-DataTemplate -binding @{            
                "Image.Source" = "image_link"            
                "Title.Text" = "title"            
            }            
        }            
    }            
}            
            
function ql {$args}            
            
$windowAttributes = @{            
    WindowStartupLocation = "CenterScreen"            
    Width = 1200            
    Height = 600            
    Background = "Black"            
    Title = "PowerShell, ShowUI and the Twitter API"            
}            
            
$scrollViewerAttributes = @{            
    VerticalScrollBarVisibility = "Disabled"            
    HorizontalScrollBarVisibility = "Auto"            
}            
            
$textBboxAttributes = @{            
    Margin = 5             
    Background = "DarkGray "            
    Foreground = "#FFFFFF"             
    FontSize = 14            
}            
            
New-Window @windowAttributes -Show -On_Loaded {            
    $search.focus()            
    ql PowerShell ShowUI dfinke jamesbru  jaykul |             
        ForEach {             
            $targetLayout.Children.Add( (New-TwitterFeed $_ $tabcontrol) )             
        }            
} {            
    Grid -Columns 35*, 75 -Rows 35, 100* {            
            
        TextBox -Name Search -Row 0 -Column 0 @textBboxAttributes            
            
        Button -IsDefault -Row 0 -Column 1 -Margin 5 "_Search" -On_Click {                        
            $targetLayout.Children.Add( (New-TwitterFeed $Search.Text) )            
            $Search.Text = ""            
        }            
            
        ScrollViewer -Row 1 -Column 0 -ColumnSpan 2 @scrollViewerAttributes {            
            UniformGrid -Name targetLayout -Rows 1            
        }            
    }            
}

{ 3 comments… read them below or add one }

Ravi 07.09.11 at 10:37 am

I am getting this error when I run it in ISE
New-Window : Exception setting “TaskbarItemInfo”: “Cannot convert the “System.Windows.Controls.Grid” value of type “System.Windo
ws.Controls.Grid” to type “System.Windows.Shell.TaskbarItemInfo”.”
At line:89 char:11
+ New-Window <<<< @ws -Show -On_Loaded {
+ CategoryInfo : NotSpecified: (:) [New-Window], RuntimeException
+ FullyQualifiedErrorId : EmbeddedProcessRecordError,AutoGenerateCmdlets131633447.NewWindowCommand

Ravi 07.09.11 at 10:42 am

Okay, the above error could be due to .Net 4. I removed that from ISE config and now, I see a different error:

New-Window : Cannot convert ‘System.Management.Automation.ScriptBlock’ to the t
ype ‘System.Management.Automation.SwitchParameter’ required by parameter ‘Allow
sTransparency’.
At line:89 char:11
+ New-Window <<<< @ws -Show -On_Loaded {
+ CategoryInfo : InvalidArgument: (:) [New-Window], ParameterBind
ingException
+ FullyQualifiedErrorId : CannotConvertArgument,AutoGenerateCmdlets1386855
856.NewWindowCommand

Doug Finke 07.09.11 at 10:44 am

Thanks Ravi. Fixed, changed @ws to @windowAttributes

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Contrat Creative Commons

© 2007-2013, Doug Finke