-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClipboardHistoryViewer.ps1
More file actions
215 lines (200 loc) · 8.42 KB
/
Copy pathClipboardHistoryViewer.ps1
File metadata and controls
215 lines (200 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<#
.SYNOPSIS
UI that will display the history of clipboard items
.DESCRIPTION
UI that will display the history of clipboard items. Options include filtering for text by
typing into the filter textbox, context menu for removing and copying text as well as a menu to
clear all entries in the clipboard and clipboard history viewer.
Use keyboard shortcuts to run common commands:
Ctrl + C -> Copy selected text from viewer
Ctrl + R -> Remove selected text from viewer
Ctrl + E -> Exit the clipboard viewer
.NOTES
Author: Boe Prox
Created: 10 July 2014
Version History:
1.0 - Boe Prox - 10 July 2014
-Initial Version
1.1 - Boe Prox - 24 July 2014
-Moved Filter from timer to TextChanged Event
-Add capability to select multiple items to remove or add to clipboard
-Able to now use mouse scroll wheel to scroll when over listbox
- Added Keyboard shortcuts for common operations (copy, remove and exit)
#>
#Requires -Version 3.0
$Runspacehash = [hashtable]::Synchronized(@{})
$Runspacehash.Host = $Host
$Runspacehash.runspace = [RunspaceFactory]::CreateRunspace()
$Runspacehash.runspace.ApartmentState = "STA"
$Runspacehash.runspace.Open()
$Runspacehash.runspace.SessionStateProxy.SetVariable("Runspacehash",$Runspacehash)
$Runspacehash.PowerShell = {Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase}.GetPowerShell()
$Runspacehash.PowerShell.Runspace = $Runspacehash.runspace
$Runspacehash.Handle = $Runspacehash.PowerShell.AddScript({
Function Get-ClipBoard {
[Windows.Clipboard]::GetText()
}
Function Set-ClipBoard {
$Script:CopiedText = @"
$($listbox.SelectedItems | Out-String)
"@
[Windows.Clipboard]::SetText($Script:CopiedText)
}
Function Clear-Viewer {
[void]$Script:ObservableCollection.Clear()
[Windows.Clipboard]::Clear()
}
#Build the GUI
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window" Title="Powershell Clipboard History Viewer" WindowStartupLocation = "CenterScreen"
Width = "350" Height = "425" ShowInTaskbar = "True" Background = "White">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.Resources>
<Style x:Key="AlternatingRowStyle" TargetType="{x:Type Control}" >
<Setter Property="Background" Value="LightGray"/>
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<Menu Width = 'Auto' HorizontalAlignment = 'Stretch' Grid.Row = '0'>
<Menu.Background>
<LinearGradientBrush StartPoint='0,0' EndPoint='0,1'>
<LinearGradientBrush.GradientStops>
<GradientStop Color='#C4CBD8' Offset='0' />
<GradientStop Color='#E6EAF5' Offset='0.2' />
<GradientStop Color='#CFD7E2' Offset='0.9' />
<GradientStop Color='#C4CBD8' Offset='1' />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Menu.Background>
<MenuItem x:Name = 'FileMenu' Header = '_File'>
<MenuItem x:Name = 'Clear_Menu' Header = '_Clear' />
</MenuItem>
</Menu>
<GroupBox Header = "Filter" Grid.Row = '2' Background = "White">
<TextBox x:Name="InputBox" Height = "25" Grid.Row="2" />
</GroupBox>
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
Grid.Row="3" Height = "Auto">
<ListBox x:Name="listbox" AlternationCount="2" ItemContainerStyle="{StaticResource AlternatingRowStyle}"
SelectionMode='Extended'>
<ListBox.Template>
<ControlTemplate TargetType="ListBox">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderBrush}">
<ItemsPresenter/>
</Border>
</ControlTemplate>
</ListBox.Template>
<ListBox.ContextMenu>
<ContextMenu x:Name = 'ClipboardMenu'>
<MenuItem x:Name = 'Copy_Menu' Header = 'Copy'/>
<MenuItem x:Name = 'Remove_Menu' Header = 'Remove'/>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
</ScrollViewer >
</Grid>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )
#Connect to Controls
$listbox = $Window.FindName('listbox')
$InputBox = $Window.FindName('InputBox')
$Copy_Menu = $Window.FindName('Copy_Menu')
$Remove_Menu = $Window.FindName('Remove_Menu')
$Clear_Menu = $Window.FindName('Clear_Menu')
#Events
$Clear_Menu.Add_Click({
Clear-Viewer
})
$Remove_Menu.Add_Click({
@($listbox.SelectedItems) | ForEach {
[void]$Script:ObservableCollection.Remove($_)
}
})
$Copy_Menu.Add_Click({
Set-ClipBoard
})
$Window.Add_Activated({
$InputBox.Focus()
})
$Window.Add_SourceInitialized({
#Create observable collection
$Script:ObservableCollection = New-Object System.Collections.ObjectModel.ObservableCollection[string]
$Listbox.ItemsSource = $Script:ObservableCollection
#Create Timer object
$Script:timer = new-object System.Windows.Threading.DispatcherTimer
$timer.Interval = [TimeSpan]"0:0:.1"
#Add event per tick
$timer.Add_Tick({
$text = Get-Clipboard
If (($Script:Previous -ne $Text -AND $Script:CopiedText -ne $Text) -AND $text.length -gt 0) {
#Add to collection
[void]$Script:ObservableCollection.Add($text)
$Script:Previous = $text
}
})
$timer.Start()
If (-NOT $timer.IsEnabled) {
$Window.Close()
}
})
$Window.Add_Closed({
$Script:timer.Stop()
$Script:ObservableCollection.Clear()
$Runspacehash.PowerShell.Dispose()
})
$InputBox.Add_TextChanged({
[System.Windows.Data.CollectionViewSource]::GetDefaultView($Listbox.ItemsSource).Filter = [Predicate[Object]]{
Try {
$args[0] -match [regex]::Escape($InputBox.Text)
} Catch {
$True
}
}
})
$listbox.Add_MouseRightButtonUp({
If ($Script:ObservableCollection.Count -gt 0) {
$Remove_Menu.IsEnabled = $True
$Copy_Menu.IsEnabled = $True
} Else {
$Remove_Menu.IsEnabled = $False
$Copy_Menu.IsEnabled = $False
}
})
$Window.Add_KeyDown({
$key = $_.Key
If ([System.Windows.Input.Keyboard]::IsKeyDown("RightCtrl") -OR [System.Windows.Input.Keyboard]::IsKeyDown("LeftCtrl")) {
Switch ($Key) {
"C" {
Set-ClipBoard
}
"R" {
@($listbox.SelectedItems) | ForEach {
[void]$Script:ObservableCollection.Remove($_)
}
}
"E" {
$This.Close()
}
Default {$Null}
}
}
})
[void]$Window.ShowDialog()
}).BeginInvoke()