Powershell Reader and translator
#
#File name: English Read and Translate words tool.ps1
#Developer: Lingli Zeng (MSN:becomegreat@126.com)
#Create time: September 29,2009 1:00 PM
#Last Modify time: September 29,2009
#Version: V1.0
powershell -sta
#Load assembly
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
####################################################################################
#####################################create UI######################################
#Object Form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "English Reader and Translator"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
#Set BorderStyle to FixedSingle
$objForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
#Can not maximize box.
$objForm.MaximizeBox = $false
#Label for Select option
$SelectOption = New-Object System.Windows.Forms.Label
$SelectOption.Location = New-Object System.Drawing.Size(10,10)
$SelectOption.Size = New-Object System.Drawing.Size(135,30)
$SelectOption.Text = "Select an Option:"
#Stop
$RadioButton = New-Object Windows.Forms.radiobutton
$RadioButton.Location = New-Object System.Drawing.Size(60,40)
$RadioButton.Size = New-Object System.Drawing.Size(150,20)
$RadioButton.Text = "Stop"
$objForm.Controls.Add($RadioButton)
#Read text
$RadioButton1 = New-Object Windows.Forms.radiobutton
$RadioButton1.Location = New-Object System.Drawing.Size(60,70)
$RadioButton1.Size = New-Object System.Drawing.Size(150,20)
$RadioButton1.Text = "Read text"
#Translate word
$RadioButton2 = New-Object Windows.Forms.radiobutton
$RadioButton2.Location = New-Object System.Drawing.Size(60,100)
$RadioButton2.Size = New-Object System.Drawing.Size(150,20)
$RadioButton2.Text = "Translate word"
#The default option is "read".
$Radiobutton1.checked = "true"
#Ok button
$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Size(180,125)
$StartButton.Size = New-Object System.Drawing.Size(100,30)
$StartButton.Text = "OK"
####################################################################################
########################################create Event################################
#Create a SAPI.SpVoice object.
$Voice = new-object -com SAPI.SpVoice;
#Set the default rate
$Voice.Rate = -1
#Clickboard translator
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("about:blank")
$ie.visible = $false
#########################################For Translator###############################
#WShell
$wshell = New-Object -comObject WScript.Shell
#wget
function wget([string]$url)
{
$client = new-object System.Net.WebClient
$client.Encoding = [System.Text.Encoding]::GetEncoding("utf-8")
return $client.DownloadString($url)
}
function ReplaceYinbiao([string]$StringYinbiao)
{
$StringYinbiao = $StringYinbiao.Replace("ə","ə")
$StringYinbiao = $StringYinbiao.Replace("ɔ","ɔ")
$StringYinbiao = $StringYinbiao.Replace("ʌ","ʌ")
$StringYinbiao = $StringYinbiao.Replace("æ","æ")
$StringYinbiao = $StringYinbiao.Replace("ɑ","ɑ")
$StringYinbiao = $StringYinbiao.Replace("ɛ","ɛ")
$StringYinbiao = $StringYinbiao.Replace("θ","θ")
$StringYinbiao = $StringYinbiao.Replace("ŋ","ŋ")
$StringYinbiao = $StringYinbiao.Replace("ʃ","ʃ")
$StringYinbiao = $StringYinbiao.Replace("ð","ð")
$StringYinbiao = $StringYinbiao.Replace("ʒ","ʒ")
$StringYinbiao = $StringYinbiao.Replace("ʊ","ʊ")
return $StringYinbiao
}
$url = "http://dict.youdao.com/search?tab=chn&keyfrom=dict.top&q="
function TranslateWord([string]$ContentItem)
{
$result = wget($url + $ContentItem)
$TranslateResults = ""
$Yinbiao = ""
$bYinbiao = $result -match ">.*]"
if($bYinbiao -eq "True")
{
$Yinbiao = "" + $matches.values
$Yinbiao = $Yinbiao.Substring(1,($Yinbiao.Length - 1))
$Yinbiao = ReplaceYinbiao($Yinbiao)
}
$index = 0
$regexPattern = "attributem1web.*</td"
$Fanyi = ""
$regex = New-Object System.Text.RegularExpressions.Regex $regexPattern
while($index -lt $result.Length)
{
$match = $regex.Match($result, $index)
if($match.Success -and $match.Length -gt 0)
{
$Fanyi += $match.Value.ToString() + "`t"
$index = $match.Index + $match.Length
}
else
{
$index = $result.Length
}
}
$Fanyi = $Fanyi.Replace("attributem1web`">","`n").Replace("</td","").Replace("<font class=graynolinktext>","`n")
$Fanyi = $Fanyi.Replace(" `; ","").Replace("</font>","")
$TranslateResults = $ContentItem + "`t" + $Yinbiao + "`n" + $Fanyi + "`n"
#$TranslateResults= $TranslateResults.Replace("<a href=`"`/help`/dic`/features`/001`/`"target=`"_blank`">","")
$TranslateResults= $TranslateResults.Replace("<a href=","").Replace("`"","").Replace("/help/dict/features/001/target=_blank","")
return $TranslateResults
}
#The event when click OK button
$event = {
#
$objForm.Visible = $false
#clear clipboard
$ie.document.parentwindow.clipboarddata.getdata("text")
$ie.document.parentwindow.clipboarddata.setdata("text","")
$StringOption = ""
if($Radiobutton.checked){$StringOption = "stop"}
if($Radiobutton1.checked){$StringOption = "read"}
if($Radiobutton2.checked){$StringOption = "translate"}
if($StringOption -eq "stop")
{
#Do nothing
}
if($StringOption -eq "read")
{
$str1 = ""
while($Radiobutton1.checked -eq "true")
{
$str2 = $ie.document.parentwindow.clipboarddata.getdata("text")
if($str1 -ne $str2)
{
if($str2.Length -gt 1)
{
$str1 = $str2
$Voice.Speak($str2,1)
$flag = $wshell.Popup("Do you want to continue?",5,"English Reader",1)
if($flag -eq 2){$Radiobutton.checked = "true"}
}
}
Start-Sleep 2
}
}
if($StringOption -eq "translate")
{
$str1 = ""
while($Radiobutton2.checked -eq "true")
{
$str2 = $ie.document.parentwindow.clipboarddata.getdata("text")
if($str1 -ne $str2)
{
if($str2.Length -gt 1 -and $str2.Length -lt 50)
{
$str1 = $str2
$ShowResults = TranslateWord $str2
$flag = $wshell.Popup(($ShowResults + "`nClick Cancel to stop translator"),30,$str2,1)
if($flag -eq 2){$Radiobutton.checked = "true"}
}
}
Start-Sleep 2
}
}
$objForm.Visible = $true
}
####################################################################################
##############################################Show UI###############################
#Add Controls to object Form
$objForm.Controls.Add($SelectOption)
$objForm.Controls.Add($StartButton)
$objForm.Controls.Add($RadioButton1)
$objForm.Controls.Add($RadioButton2)
#Add click event
$StartButton.Add_Click($event)
#Show GUI dialog
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()