学印尼语遇到一个问题,即需要大量复制pdf文本中的个别单词到谷歌翻译中,这个过程需要重复点击和按键,及其耗费时间,为此写了一个脚本 只需要
双击生词
或自定义键盘按键
就可以自动复制、翻译(需要提前在翻译的网页中设置翻译的语言类别)、并将结果以附注形式添加到pdf中,方便重复查看。
本文教程的视频为 https://www.bilibili.com/video/BV1hS4y1f7yy
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.14.5
Author: GDP
Script Function:
Template AutoIt script.
功能: 使用autoit脚本,通过双击pdf文件中不知道的单词(通过识别被选中文字的背景色),
然后自动在翻译网页中进行翻译,并将翻译结果作为单词的 文本附注。
特点:
1- 快速翻译单词
2-可将翻译结果作为附注
3-方便 双击单词即可
比如有道词典一类的软件也可以识别鼠标动作进行翻译,但不会将翻译的结果记录到pdf文件中
本脚本可以 快速(比通过手动地复制单词 粘贴到浏览器网页中搜索快)地将翻译结果 以 附注 形式添加到
pdf文件中,方便之后查看单词的意思
限制:
1-需要了解下autoit 的基础,b站有教程,一个小时可入门
2-因为pdf和pdf阅读器原因,可能无法准确识别已选择文本的背景色,通过识别鼠标双击比较准确
-------------------------------------------------------------------------
需要提前设置及注意的的内容包括:
1-浏览器位置-本脚本中浏览器在任务栏第一的位置
2-pdf文件的位置-本脚本中pdf文件在任务栏第二的位置
3-浏览器标签的位置-本脚本中第一个标签为google翻译网页
4-双击单词后单词的背景色-本脚本中使用的acrobat软件中双击后出现的背景色
5-翻译的对象及结果-需要提前在翻译网页中选择正确选项
6-尽量保证pdf文件中没有与选择文本出现的背景色相同的颜色,可提前删除pdf文件中的图像或 其他可能相似的颜色
7-双击单词后会自动识别鼠标的位置,之后在这个位置进行右击并添加附注,所以双击后的几秒内且在标注添加完成前不要移动鼠标
8-识别颜色区域需要提前设置即 PixelSearch 函数
10-翻译网页中 播放语音 按钮的位置
9-其他具体参数可以自己调整,以适应自己的电脑
10-脚本无限循环,不使用时及时关闭,并尽量不使用其他的软件,防止在其他window搜索到对应颜色
#ce
#include <Date.au3>
$starttime = _NowCalc()
Sleep(2000)
While 1
$coord = PixelSearch(114, 160,1454, 732,0x99C1DA);识别单词是否被双击,双击后单词会有颜色背景,根据不同的pdf阅读器调整颜色
;PixelSearch 里的坐标指的是 区域内左上角 和 右下角两点各自的的 x和 y 坐标
If Not @error Then
ToolTip("单词被选中 ")
$pos = MouseGetPos() ;first get the pos,use ctrl + tab for open script and get the mouse positon
Send("^c") ;复制选中的单词
Sleep(500)
MouseClick("left",222,851,1,2) ;打开浏览器
MouseClick("left",80,17,1,2) ;open google translate
MouseClick("left",300,322,1,2) ;点击输入框
Send("^a"); choose all the words
Send("{BACKSPACE 20}") ;delet the contentt in the frame
Send("^v"); input the word
sleep(1000) ;wait one second for loading
MouseClick("left",987,326,2) ;choose the translation result
Send("^c") ;复制选中的翻译结果
MouseClick("left",202,426,1,2) ;tap pronounction button
MouseClick("left",402,844,1,2) ;点击pdf
MouseMove($pos[0],$pos[1])
MouseClick("right") ;右键点击,只移动鼠标为自己操作
Send("n")
Send("^v") ; paste the world
Sleep(500)
$starttime = _NowCalc()
Else
$currenttime = _NowCalc()
$iDateCalc = _DateDiff( 's',$starttime,$currenttime);计算有多少时间没有选择新的单词
If $iDateCalc<>0 And mod($iDateCalc, 30) = 0 Then ;提醒的时间最少30s起步
ToolTip("已经: " & $iDateCalc & "S没有选择新的单词" );提醒有多久没有选择新的单词
Else
ToolTip("")
EndIf
Sleep(500)
EndIf
WEnd
除了识别双击后的文字背景色, 也可以识别键盘操作,先将光标移动到我们的目标单词,然后 让脚本识别键盘特定按键,然后进行后续相同的操作。即使用另一个函数即, 可自定义按键
If _IsPressed("11",$hDLL) And _IsPressed("10",$hDLL) Then
autoitscript.com/autoit3/docs/ 网页介绍了如何识别双击即下面的脚本
<pre class="geshi geshiprinted lang-autoit ipsCode" style="box-sizing: border-box; font-family: Consolas, "Courier New", "Andale Mono", monospace; font-size: 12px; overflow: auto; tab-size: 4; white-space: pre-wrap; line-height: 13.2px; text-rendering: optimizespeed; background: rgb(248, 248, 248); clear: both; direction: ltr; overflow-wrap: normal; margin: 0px 0px 0px 10px; border-radius: var(--radius-1); padding: 5px; border-color: rgb(201, 201, 201); border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 4px !important; border-image: initial; max-height: 500px; color: rgb(53, 60, 65); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">[#Include](https://www.autoitscript.com/autoit3/docs/keywords/include.htm) <Misc.au3> ; Used for the _IsPressed
[HotKeySet](https://www.autoitscript.com/autoit3/docs/functions/HotKeySet.htm)("{ESC}", "_ExitScript") ;Press Ecs key to Exit script
[Local](https://www.autoitscript.com/autoit3/docs/keywords.htm) $hDLL = [DllOpen](https://www.autoitscript.com/autoit3/docs/functions/DllOpen.htm)("user32.dll") ; Dll as calling _IsPressed function repeatedly
[While](https://www.autoitscript.com/autoit3/docs/keywords.htm) 1
$nLeftclicks = 0 ;Initialize left click count on each iteration
[If](https://www.autoitscript.com/autoit3/docs/keywords.htm) [_IsPressed](https://www.autoitscript.com/autoit3/docs/libfunctions/_IsPressed.htm)("01", $hDLL) [Then](https://www.autoitscript.com/autoit3/docs/keywords.htm) ;If left mouse button pressed
$Pos = [MouseGetPos](https://www.autoitscript.com/autoit3/docs/functions/MouseGetPos.htm)() ;Get mouse position
[Do](https://www.autoitscript.com/autoit3/docs/keywords.htm)
[While](https://www.autoitscript.com/autoit3/docs/keywords.htm) ([_IsPressed](https://www.autoitscript.com/autoit3/docs/libfunctions/_IsPressed.htm)("01", $hDLL)) ;Wait until user releases button
[Sleep](https://www.autoitscript.com/autoit3/docs/functions/Sleep.htm)(10)
[WEnd](https://www.autoitscript.com/autoit3/docs/keywords.htm)
$nLeftclicks += 1 ;Increment click count
[sleep](https://www.autoitscript.com/autoit3/docs/functions/Sleep.htm)(100) ;Wait for 100ms
$aTemp = [MouseGetPos](https://www.autoitscript.com/autoit3/docs/functions/MouseGetPos.htm)() ;Get new mouse position again
[Until](https://www.autoitscript.com/autoit3/docs/keywords.htm)([Not](https://www.autoitscript.com/autoit3/docs/keywords.htm) ($aTemp[0] = $Pos[0] [and](https://www.autoitscript.com/autoit3/docs/keywords.htm) $aTemp[1] = $Pos[1] [and](https://www.autoitscript.com/autoit3/docs/keywords.htm) [_IsPressed](https://www.autoitscript.com/autoit3/docs/libfunctions/_IsPressed.htm)("01", $hDLL))) ;Loop until mouse is on a new position or it is not pushed again
[ToolTip](https://www.autoitscript.com/autoit3/docs/functions/ToolTip.htm)("X: " & $Pos[0] & "Y: " & $Pos[1] & "Number of continous clicks: " & $nLeftclicks)
[ConsoleWrite](https://www.autoitscript.com/autoit3/docs/functions/ConsoleWrite.htm)("X: " & $Pos[0] & "Y: " & $Pos[1] & "Number of continous clicks: " & $nLeftclicks & [@crlf](https://www.autoitscript.com/autoit3/docs/macros.htm#%40CRLF))
[EndIf](https://www.autoitscript.com/autoit3/docs/keywords.htm)
[Wend](https://www.autoitscript.com/autoit3/docs/keywords.htm)
[Func](https://www.autoitscript.com/autoit3/docs/keywords.htm) _ExitScript()
[DllClose](https://www.autoitscript.com/autoit3/docs/functions/DllClose.htm)($hDLL)
[Exit](https://www.autoitscript.com/autoit3/docs/keywords.htm)
[EndFunc](https://www.autoitscript.com/autoit3/docs/keywords.htm) ;=> _ExitScript</pre>