循环访问AHK中所选单词的同义词

时间:2018-06-13 22:27:47

标签: windows automation autohotkey

这是我想要做的:

假设我在任意文本编辑器中有一个句子:

  蒂姆是一个快乐的人。

如果我的光标位于单词的末尾 - e。 G。 “happy”,然后我按下某个热键,脚本应该查看是否有包含“happy”这个词的同义词池,如果有,请替换它通过列表中的下一个同义词。

因此,您可以通过反复按热键循环浏览所有同义词。

同义词列表应该在脚本中,例如:

(
happy
cheerful
jolly
merry
lively
)

我发现this post 已经完成了类似的事情。 (在那里,一个特定的词总是被一个随机的同义词取代)

问题:

  • 是否有已经执行此操作的脚本?
  • 如果没有 - 你会怎么做?

(我还应该提到我对AHK很新。)

1 个答案:

答案 0 :(得分:1)

我会用这样的东西:

#NoEnv
#SingleInstance Force

synonyms=
(
happy,cheerful,jolly,merry,lively
unhappy,sad,down,depressed
calm,quiet,peaceful,still
; ...
)

$F1::
synonyms_found := "" ; empty this variable (erase its content)
Menu, Replace Synonym, Add
Menu, Replace Synonym, deleteAll ; empty this menu
ClipSaved := ClipboardAll ; save the entire clipboard to the variable ClipSaved
clipboard := "" ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
Send ^+{Left} ; select the word left to cursor
Sleep, 50
Send ^c ; copy the selected word
ClipWait 0.5 ; wait 0.5 seconds for the clipboard to contain data
if (ErrorLevel) ; If ErrorLevel, clipwait found no data on the clipboard within 0.5 seconds
{
    MsgBox, No word selected
    clipboard := ClipSaved ; restore original clipboard
    return ; don't go any further
}
; otherwise:
Loop, Parse, synonyms, `n,`r ; retrieve each line from the synonyms, one at a time
{   
    If InStr(A_LoopField, clipboard) ; if the retrieved line contains the word copied
    {       
        synonyms_found .= A_LoopField . "," ; concatenate (join) the retrieved lines into a single variable
        Loop, Parse, synonyms_found, `, ; retrieve each word from the retrieved lines
            Menu, Replace Synonym, Add, %A_LoopField%, Replace_Synonym  ; create a menu of the synonym words
    }
}
If (synonyms_found != "") ; if this variable isn't empty
    Menu, Replace Synonym, Show
else
    MsgBox, No synonyms found for "%clipboard%"
Sleep, 300
clipboard := ClipSaved ; restore original clipboard
return

; select a menu item to replace the selected word:
Replace_Synonym:
SendInput, %A_ThisMenuItem%
return