https://autohotkey.com/board/topic/7331 ... ry-script/
Could anyone advise on a working update, variants, or newer, better methods that supercede this please?
Specific error is in the UnHTM function.: "The following variable name contains an illegal character: '<lots of unicode characters>'"
Code: Select all
; --------------------
;
; Google(SearchQuery, StrTitle, StrDescriptionStr), function to return URL of first google search result
;
; Input the google query term or phrase in the first parameter. The function returns the url of the first google result.
; Optionally, you may also enter a variable in the 2nd or 3rd paramters:
; Title - stores the page title of the first result
; Description - stores the google description text for that result.
;
; Url := Google("AutoHotkey", Title, Description)
; MsgBox, % "Url:`t`t" Url "`nTitle:`t`t" Title "`nDescription:`t" Description
;
; Returns:
; Url: http://www.autohotkey.com/
; Title: AutoHotkey - Free Mouse and Keyboard Macro Program with ...
; Description: Free keyboard macro program. Supports hotkeys for keyboard, mouse, and joystick. Can expand abbreviations as you type them (AutoText).
;
; https://autohotkey.com/board/topic/73311-quick-dirty-google-query-script/
;
; --------------------
#F1:
Url := Google("AutoHotkey", Title, Description)
MsgBox, % "Url:`t`t" Url "`nTitle:`t`t" Title "`nDescription:`t" Description
return
Google(Phrase, ByRef Title="", ByRef Text="") ; http://www.autohotkey.com/forum/topic78570.html
{
If DllCall("Wininet.dll\InternetGetConnectedState", "Str", 0x40, "Int", 0) {
URLDownloadToFile, % "http://www.google.com/search?q=" EncodeURL(Phrase), %A_Temp%\GoogleSearch
FileRead, Result, %A_Temp%\GoogleSearch
FileDelete, %A_Temp%\GoogleSearch
If (Title := RegExMatch(Result, "i)<h3 class=""r""><a href=""(?:/url\?q=)?\K[^/][^""& ]*", Link)) {
StringTrimLeft, Result, Result, %Title%
Title := UnHTM(SubStr(Result, @ := InStr(Result, ">", True, 25) + 1, InStr(Result, "</a>") - @)), RegExMatch(Result, "si)(?:<div class=""?s""?>|<span class=""?st""?>)\K.*?(?=(?:<br>)*\s*</?(?:span|div|table))", Text), Text := RegExReplace(UnHTM(Text), "\s+", " ")
Return EncodeURL(Link, True)
} Else
MsgBox, 262160, %A_ScriptName% - %A_ThisFunc%(): Error, Failed to find a result!
} Else
MsgBox, 262160, %A_ScriptName% - %A_ThisFunc%(): Error, No internet connectivity!
}
EncodeURL(Text, FromURL=False) ; Uberi
{
FormatInteger := A_FormatInteger, FoundPos := 0, SearchFor := FromURL ? "%.." : "[^\w-.~% ]"
SetFormat, IntegerFast, Hex
If !FromURL
StringReplace, Text, Text, `%, `%25, All
While (FoundPos := RegExMatch(Text, SearchFor, Char, FoundPos + 1))
StringReplace, Text, Text, %Char%, % FromURL ? Chr("0x" SubStr(Char, 2)) : "%" SubStr(0 SubStr(Asc(Char), 3), -1), All
If !FromURL {
StringReplace, Text, Text, %A_Space%, +, All
Text := RegExReplace(Text, "%..", "$U0")
}
SetFormat, IntegerFast, %FormatInteger%
Return Text
}
UnHTM(HTM) ; www.autohotkey.com/forum/topic51342.html
{
Static HT
If (HT = "")
HT := "ááââ´´ææàà&ååããää&bdquo„¦¦&bull•ç縸¢¢&circˆ©©¤¤&dagger†&dagger‡°°÷÷ééêêèèððëë&euro€&fnofƒ½½¼¼¾¾>>&hellip…ííîî¡¡ìì¿¿ïï««&ldquo“&lsaquo‹&lsquo‘<<¯¯&mdash—µµ·· &ndash–¬¬ññóóôô&oeligœòòªªººøøõõöö¶¶&permil‰±±££"""»»&rdquo”®®&rsaquo›&rsquo’&sbquo‚&scaronš§§ ¹¹²²³³ßßþþ&tilde˜××&trade™úúûûùù¨¨üüýý¥¥ÿÿ"
HTM := RegExReplace(HTM, "<[^>]+>")
Loop, Parse, HTM, &`;
If !((A_Index & 1) or InStr(R, "&" A_LoopField ";", True))
R .= "&" A_LoopField ";"
StringTrimRight, R, R, 1
Loop, Parse, R, `;
If InStr(HT, A_LoopField)
StringReplace, HTM, HTM, %A_LoopField%;, % SubStr(HT, InStr(HT, A_LoopField) + StrLen(A_LoopField), 1), All
Else If (SubStr(A_LoopField, 2, 1) = "#")
StringReplace, HTM, HTM, %A_LoopField%;, % Chr(((SubStr(A_LoopField, 3, 1) = "x") ? 0 : "") SubStr(A_LoopField, 3)), All
Return RegExMatch(HTM, "\S") ? RegExReplace(HTM, "^\s*([\s\S]*\S)\s*$", "$1") : ""
}