I use this in a tooltip-like GUI, but here's an example of using it with a Tooltip
Update: here's a slightly modified version using CFlyout:
Spoiler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;; Lookup definition of selected text ;;;;;;;;;;;;;; Made into function so that variables are local ^+Space::QuickDef() QuickDef() { sOldClipboard := clipboard Send ^c Clipwait, 1 if (clipboard == A_Blank) clipboard := sOldClipboard aTmp := ["Searching...!"] vFlyout := new CFlyout(0, aTmp, true, false, -99999, -99999, 800, 9, -99999, true, 0, "Consolas, s20", "c0xFD6CFD", "Center") sSel := Trim(clipboard, " `t`r`n") asResponse := DictLookup(sSel) if (!asResponse[4]) { aTmp := ["No data found!"] vFlyout.UpdateFlyout(aTmp) Sleep 500 return } CoordMode, Mouse, Screen vFlyout.UpdateFlyout(asResponse) while (GetKeyState("Ctrl", "D") && GetKeyState("Shift", "D") && (GetKeyState("Space", "D") || GetKeyState("1", "D"))) continue vFlyout:= CoordMode, Mouse, Relative ; Default clipboard := sOldClipboard return } ;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;; Lookup a word and its synonyms via Thesaurus.com ;;;;;;;;;;;;;; returns an 2-element array as follows: ;;;;;;;;;;;;;; 1. Definition ;;;;;;;;;;;;;; 2. List of synonyms DictLookup(sWord, bRetry=true, sOrig="") { sLoopStartAt := "<div class=""heading-row synonims-heading"">" sSearchSynonym := "<div class=""relevancy-list"">" sEndOfSynonymList := "<div class=""synonyms-horizontal-divider""></div>" rogue sEndOfSynonymList := "<div id=""filter-0""></div>" sBeg := "<span class=""text"">" sEnd := "</span>" sResponse := UrlGet("http://thesaurus.com/browse/" sWord "?s=t") if (!InStr(sResponse, sEndOfSynonymList)) sEndOfSynonymList := "<div class=""synonyms-horizontal-divider""></div>" Loop, Parse, sResponse, `n, `r { if (InStr(A_LoopField, "Did you mean")) { bMisspelled := true continue } if (bMisspelled) { ; <a href="http://thesaurus.com/browse/systemization" data-cts='{"linkId":"h7k7u4","infix":"","domain":"dictionary.com","clkpage":"the-spell","clkmod":"scpmean","clksite":"thes","clkld":0}'>systemization </a> iStartPos := InStr(A_LoopField, ">", false, 1, 1) + 1 sRetryWord := SubStr(A_LoopField, iStartPos) StringReplace, sRetryWord, sRetryWord, `</a`>,, All sRetryWord := Trim(sRetryWord) break } if (Trim(A_LoopField) = sLoopStartAt) { iDefLine := A_Index + 2 continue } if (A_Index == iDefLine) { sDef := SubStr(A_LoopField, InStr(A_LoopField, ">") + 1, InStr(A_LoopField, "</") - InStr(A_LoopField, ">") - 1) "." bFoundDef := true continue } if (Trim(A_LoopField) == sSearchSynonym) { bLoopingThroughSynonymList := true continue } bFoundAllSynonyms := (Trim(A_LoopField) = sEndOfSynonymList && bLoopingThroughSynonymList) if (bFoundAllSynonyms && bFoundDef) break if (bLoopingThroughSynonymList || InStr(LoopField, "See more synonyms")) { if (!InStr(A_LoopField, "href=")) continue ; Trim what we know we can. ; Example HTML for "word": ; <a class="theColor" rel="nofollow" href="http://thesaurus.com/browse/chat">chat</a>, chitchat, iHyperlinkPos := InStr(A_LoopField, ">") + 1 if (iHyperlinkPos == 0) iHyperlinkPos = 1 LoopField := Trim(SubStr(A_LoopField, iHyperlinkPos)) ; chat</a>, chitchat, StringReplace, sSynonymPart, LoopField, %sBeg%,, All sSynonymList .= sSynonymList ? ", " sSynonymPart : sSynonymPart } } StringReplace, sSynonymList, sSynonymList, %sEnd%,, All StringReplace, sSynonymList, sSynonymList, `<b`>,, All StringReplace, sSynonymList, sSynonymList, `</b`>,, All StringReplace, sSynonymList, sSynonymList, *,, All StringReplace, sSynonymList, sSynonymList, `t,, All StringReplace, sSynonymList, sSynonymList, `,`,,`,, All if (bMisspelled && bRetry) return DictLookup(sRetryWord, false, sWord) aDef := ["-----------------------------------------------------"] if (sOrig) { aDef.Insert("No results found for, """ sOrig """") if (sSynonymList == A_Blank) { if (sOrig = sWord) aDef.Insert("No suggested words found") else aDef.Insert("Also, no results found for, """ sWord """") aDef.Insert("-----------------------------------------------------") return aDef } aDef.Insert("Showing definition of """ sWord """") } else aDef.Insert("Definition of """ sWord """") aDef.Insert("-----------------------------------------------------") aDef.Insert(sDef) aDef.Insert("-----------------------------------------------------") aDef.Insert("Synonyms") aDef.Insert("-----------------------------------------------------") aDef.Insert(sSynonymList) aDef.Insert("-----------------------------------------------------") return aDef }
Example.ahk
Spoiler
;;;;;;;;;;;;;; Lookup definition of selected text. ;;;;;;;;;;;;;; Made into function so that variables are local. ^+Space::QuickDef() QuickDef() { Tooltip Searching... clipboard = SendInput ^{c} clipwait,2 if (ErrorLevel) { Tooltip No data found! Sleep 500 Tooltip return } SelText := Trim(clipboard) sResponse := DictLookup(SelText) CoordMode, Mouse, Screen while (GetKeyState("Ctrl", "D") and GetKeyState("Shift", "D") and GetKeyState("Space", "D")) { MouseGetPos, MouseX, MouseY if (MouseX != PrevMouseX || MouseY != PrevMouseY) ; To reduce blinking, only call Tooltip when the mouse has moved. Tooltip % sResponse PrevMouseX := MouseX PrevMouseY := MouseY } CoordMode, Mouse, Relative ; Default Tooltip return } ;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Spoiler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;; Lookup a word and its synonyms via Thesaurus.com ;;;;;;;;;;;;;; returns an 2-element array as follows: ;;;;;;;;;;;;;; 1. Definition ;;;;;;;;;;;;;; 2. List of synonyms DictLookup(sWord, bRetry=true, sOrig="") { sLoopStartAt := "<div class=""heading-row synonims-heading"">" sSearchSynonym := "<div class=""relevancy-list"">" sEndOfSynonymList := "<div class=""synonyms-horizontal-divider""></div>" rogue sEndOfSynonymList := "<div id=""filter-0""></div>" sBeg := "<span class=""text"">" sEnd := "</span>" sResponse := UrlGet("http://thesaurus.com/browse/" sWord "?s=t") if (!InStr(sResponse, sEndOfSynonymList)) sEndOfSynonymList := "<div class=""synonyms-horizontal-divider""></div>" Loop, Parse, sResponse, `n, `r { if (InStr(A_LoopField, "Did you mean")) { bMisspelled := true continue } if (bMisspelled) { ; <a href="http://thesaurus.com/browse/systemization" data-cts='{"linkId":"h7k7u4","infix":"","domain":"dictionary.com","clkpage":"the-spell","clkmod":"scpmean","clksite":"thes","clkld":0}'>systemization </a> iStartPos := InStr(A_LoopField, ">", false, 1, 1) + 1 sRetryWord := SubStr(A_LoopField, iStartPos) StringReplace, sRetryWord, sRetryWord, `</a`>,, All sRetryWord := Trim(sRetryWord) break } if (Trim(A_LoopField) = sLoopStartAt) { iDefLine := A_Index + 2 continue } if (A_Index == iDefLine) { sDef := SubStr(A_LoopField, InStr(A_LoopField, ">") + 1, InStr(A_LoopField, "</") - InStr(A_LoopField, ">") - 1) "." bFoundDef := true continue } if (Trim(A_LoopField) == sSearchSynonym) { bLoopingThroughSynonymList := true continue } bFoundAllSynonyms := (Trim(A_LoopField) = sEndOfSynonymList && bLoopingThroughSynonymList) if (bFoundAllSynonyms && bFoundDef) break if (bLoopingThroughSynonymList || InStr(LoopField, "See more synonyms")) { if (!InStr(A_LoopField, "href=")) continue ; Trim what we know we can. ; Example HTML for "word": ; <a class="theColor" rel="nofollow" href="http://thesaurus.com/browse/chat">chat</a>, chitchat, iHyperlinkPos := InStr(A_LoopField, ">") + 1 if (iHyperlinkPos == 0) iHyperlinkPos = 1 LoopField := Trim(SubStr(A_LoopField, iHyperlinkPos)) ; chat</a>, chitchat, StringReplace, sSynonymPart, LoopField, %sBeg%,, All sSynonymList .= sSynonymList ? ", " sSynonymPart : sSynonymPart } } StringReplace, sSynonymList, sSynonymList, %sEnd%,, All StringReplace, sSynonymList, sSynonymList, `<b`>,, All StringReplace, sSynonymList, sSynonymList, `</b`>,, All StringReplace, sSynonymList, sSynonymList, *,, All StringReplace, sSynonymList, sSynonymList, `t,, All StringReplace, sSynonymList, sSynonymList, `,`,,`,, All if (bMisspelled && bRetry) return DictLookup(sRetryWord, false, sWord) sDef := "-----------------------------------------------------" if (sOrig) { sDef .= "`nNo results found for, """ sOrig """" sDef .= "`nShowing definition of """ sWord """" } else sDef .= "`nDefinition of """ sWord """" sDef .= "`n-----------------------------------------------------" sDef .= "`n" sDef sDef .= "`n-----------------------------------------------------" sDef .= "`nSynonyms" sDef .= "`n-----------------------------------------------------" sDef .= "`n" st_wordwrap(sSynonymList) sDef .= "`n-----------------------------------------------------" return sDef } ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;; Using rbrtryn's UrlGet wrapper UrlGet(url) { FileEncoding UTF-8 TempFile := A_Temp "/" A_Now ".tmp" URLDownloadToFile % url, % TempFile FileRead out, % TempFile FileDelete % TempFile return Trim(out) } ;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;; using Tidbit's st_wordwrap from his StringThings library st_wordWrap(string, column=56, indentChar="") { indentLength := StrLen(indentChar) Loop, Parse, string, `n, `r { If (StrLen(A_LoopField) > column) { pos := 1 Loop, Parse, A_LoopField, %A_Space% If (pos + (loopLength := StrLen(A_LoopField)) <= column) out .= (A_Index = 1 ? "" : " ") A_LoopField , pos += loopLength + 1 Else pos := loopLength + 1 + indentLength , out .= "`n" indentChar A_LoopField out .= "`n" } Else out .= A_LoopField "`n" } Return SubStr(out, 1, -1) } ;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Edited by Verdlin, 09 September 2013 - 02:07 PM.