 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
kyf Guest
|
Posted: Sat Sep 12, 2009 6:20 pm Post subject: Get google "Did you mean" suggestions anywhere |
|
|
Hi,
i find "did you mean" is good for quickly spell checking a word, but also for grammer ....(useful if you a bit dyslexic!)
So I wrote this script so i could have the "Did you mean" suggestions at the press of a hotkey.
It currently works if you select a word or phrase and press win+d
...if there is a suggestion available, press 'enter' to replace original word, or any other key to continue
| Code: |
searchURL = http://www.google.com/search?hl=en&q= ;search string... change to local version if you wish, but may need to change FileReadLine line
tempfile = %temp%\DYM ;temp file to save search page
needle = class=spell>Did you mean ;did you mean search string
needle2 = class=spell>
needle3 = </a>
#d::
ClipSaved := ClipboardAll ; Save the entire clipboard to a variable, so it isn't affected by script
send, ^c
ClipWait
urldownloadtofile, %searchURL%%Clipboard%, %tempfile%
FileReadLine, LineIn, %tempfile%, 5 ; read the 5th line of the search page, where the "did you mean" bit is
IfInString, LineIn, %needle% ; check if it offers a suggestion
{
StringGetPos, length, LineIn, %needle%
length:= length + 24
StringTrimLeft, suggest, LineIn, %length% ;remove everything before "did you mean"
StringGetPos, length, suggest, %needle2%
length:= length + 12
StringTrimLeft, suggest, suggest, %length% ;remove everything before the DYM suggestion
StringGetPos, length, suggest, %needle3%
StringLeft, suggest, suggest, %length% ;remove everything after the DYM suggestion
;remove bold italic formating
StringReplace, suggest, suggest, <b><i>,, All
StringReplace, suggest, suggest, </i></b>,, All
toolText = Did you mean: `n %suggest%
tooltip, %toolText% , A_CaretX, A_CaretY + 20
Input, SingleKey, L1 T6, {enter} ;wait for input (for max 6 seconds)... if 'enter' pressed replace with suggestion
if ErrorLevel <> Max
{
if ErrorLevel <> Timeout
{
Clipboard := suggest
Send, ^v
}
}
} else {
toolText = No suggestion
tooltip, %toolText% , A_CaretX, A_CaretY +20
sleep, 2000
}
tooltip
FileDelete, %tempfile% ; delete the temporary file
Clipboard := ClipSaved ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
ClipSaved = ; Free the memory in case the clipboard was very large.
return
|
It might be interesting to add something like this into the intellisense code
 |
|
| Back to top |
|
 |
TchnclFl
Joined: 29 Jul 2009 Posts: 48
|
|
| Back to top |
|
 |
kyfs Guest
|
Posted: Tue Jul 13, 2010 6:46 pm Post subject: |
|
|
UPDATE
Just in case anyone is interested, thought i should upload an updated version of this as the one i posted previously doesn't work anymore (as google changed their code a little)
| Code: |
#c::
;---- set a few initial parameters......
searchURL = http://www.google.co.uk/search?hl=en&q= ;search string... change to local version if you wish, but may need to change FileReadLine line
tempfile = %temp%\DYM ;temp file to save search page
;----get word of interest..... (and save the current clipboard contents)
ClipSaved := ClipboardAll ; ----Save the entire clipboard to a variable, so it isn't affected by script
clipboard = zzzzzz ;use to test if anything copied
send, ^c
ClipWait
if clipboard = zzzzzz ;if clipboard empty, try selecting a nearby word
{
send, ^{left}
send, {ctrldown}{shiftdown}{right}{ctrlup}{shiftup}
send, ^c
ClipWait
}
;---do the search and save the webpage....
query = %Clipboard%
urldownloadtofile, %searchURL%%query%, %tempfile%
;msgbox, %searchURL%%query%
;---read through the file, to find the result amount and the did you mean suggestion
findCount:= 0
Loop, read, %tempfile%
{
if (findCount == 0) ;-----get # of results first....
{
NumString = resultStats>About
IfInString, A_LoopReadLine, %NumString%
{
StringGetPos, length, A_LoopReadLine, %NumString%
length:= length + 18
StringTrimLeft, NumResults, A_LoopReadLine, %length%
NumString = <nobr>
StringGetPos, length, NumResults, %NumString%
StringLeft, NumResults, NumResults, %length%
findcount:= 1 ;acknowledge that we have now found results number
}
}
if (findCount == 1) ;-----get # of results first....
{
;---then search for the did you mean bit...
needle = Did you mean: </span> ;did you mean search string
needle2 = class=spell>
needle3 = </a>
needle4 = Showing results for </span>
IfInString, A_LoopReadLine, %needle% ; check if it offers a suggestion
{
suggest:= A_LoopReadLine
StringGetPos, length, suggest, %needle%
length:= length + 21
StringTrimLeft, suggest, suggest, %length% ;remove everything before "did you mean"
StringGetPos, length, suggest, %needle2%
length:= length + 12
StringTrimLeft, suggest, suggest, %length% ;remove everything before the actual suggestion
StringGetPos, length, suggest, %needle3%
StringLeft, suggest, suggest, %length% ;remove everything after the actual suggestion
;remove bold italic formating
StringReplace, suggest, suggest, <b><i>,, All
StringReplace, suggest, suggest, </i></b>,, All
findcount:= 2 ;acknowledge that we have now found DYM suggestion
break
}
;---...or search for the "Showing results for" bit
IfInString, A_LoopReadLine, %needle4% ; check if it offers a suggestion
{
suggest:= A_LoopReadLine
StringGetPos, length, suggest, %needle%
length:= length + 27
StringTrimLeft, suggest, suggest, %length% ;remove everything before "did you mean"
StringGetPos, length, suggest, %needle2%
length:= length + 12
StringTrimLeft, suggest, suggest, %length% ;remove everything before the actual suggestion
StringGetPos, length, suggest, %needle3%
StringLeft, suggest, suggest, %length% ;remove everything after the actual suggestion
;remove bold italic formating
StringReplace, suggest, suggest, <b><i>,, All
StringReplace, suggest, suggest, </i></b>,, All
findcount:= 2 ;acknowledge that we have now found DYM suggestion
break
}
}
}
;---------------------------
if (findcount == 2)
{
toolText = Did you mean: `n %suggest%
tooltip, %toolText% , A_CaretX, A_CaretY + 20
Input, SingleKey, L1 T5, {enter} ;wait for input (for max 5 seconds)... if 'enter' pressed replace with suggestion
if ErrorLevel <> Max
{
if ErrorLevel <> Timeout
{
Clipboard := suggest
Send, ^v
}
}
} else {
toolText = Google doesn't offer alternative (%NumResults%)
;----------
found = 0
Loop, read, bigList.txt
{
if (found == 0)
{
if (query == A_LoopReadLine)
{
toolText = In dictionary `n Google doesn't offer alternative (%NumResults%)
found = 1
}
}
}
;---------
if (found == 0)
toolText = %toolText% `n Not in dictionary
tooltip, %toolText% , A_CaretX, A_CaretY +20
sleep, 4000
}
tooltip
sleep, 500
FileDelete, %tempfile% ; delete the temporary file
Clipboard := ClipSaved ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
ClipSaved = ; Free the memory in case the clipboard was very large.
return
|
|
|
| Back to top |
|
 |
kyfs Guest
|
Posted: Tue Jul 13, 2010 6:48 pm Post subject: |
|
|
| Oh... and i changed the hotkey to win+c ...incase you didn't spot that |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|