✪ Speak Word - 30 Dec 2018

我们常常遇到一个陌生的英文单词却拿不准发音, 在 Mac 上我们有内建的工具和快捷键可以解决, 而在 Windows 并没有内建的工具, 为此我写了这个脚本 speak.vbs. 使用时首先复制需要朗读的单词, 然后运行它, 即可朗读当前剪贴板里的第一个单词.

我在使用时配合了 WinHotKey, 并关联了快捷键 Ctrl + Alt + S. 记得要 Uncheck Windows 的权限提醒.

✪ Source Code - 30 Dec 2018


' 1. Get clipboard text
Set objHtml = CreateObject("htmlfile")
clipBoard = objHtml.ParentWindow.ClipboardData.GetData("text")
If clipBoard <> "" Then ' Do nothing if empty

Dim clipBoardText
clipBoardText = CStr(clipBoard)
clipBoardText = Trim(clipBoardText)
clipBoardText = LCase(clipBoardText)
splitText = Split(clipBoardText, " ")
textToSpeak = splitText(0) ' get only first word

' Test if it has chinese character to avoid error
Set objRegExp = New RegExp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "[^a-zA-Z0-9\s]"
doesWriteLog = True
If objRegExp.Test(clipBoardText) Then 
    textToSpeak = "character mistake"
    doesWriteLog = false
End If

' 2. Speak it
Set sapi = CreateObject("sapi.spvoice")
If Len(textToSpeak) >= 30 Then 
    sapi.Speak "text too long"
    doesWriteLog = false
ElseIf Len(textToSpeak) < 30 Then 
    sapi.Speak textToSpeak
End If

' 3. Save a log
Set fso = CreateObject("Scripting.FileSystemObject")
strFile = "words.txt"

' Read old
strLine = ""
If (fso.FileExists(strFile)) Then
    Set objFileRead = fso.OpenTextFile(strFile)
    Do Until objFileRead.AtEndOfStream
        strLine = objFileRead.ReadLine
    Loop
    objFileRead.Close
End If

' Write new
Set objFileWrite = fso.CreateTextFile(strFile, True)
If doesWriteLog Then
    objFileWrite.Write strLine + textToSpeak + " " & vbCrLf
Else
    objFileWrite.Write strLine + " " & vbCrLf
End If
objFileWrite.Close
End If ' If clipboard empty   
      

✪ Download - 30 Dec 2018

speak.vbs

✪ Update History - 2017

2018-12-30 08:26:17
发布

2019-01-03 20:24:07
解决了剪贴板有汉字会报错的 Bug。

2019-01-27 14:09:19
修复了剪贴板为空的 Bug。



  About | Email | Twitter | Facebook
- by 318yang -

kopimi