I have tested it, and now Chrome window is activated in 100% of cases. If anyone is interested, here is the full code (in the URL, replace underscores '_' with slashes '/'). This script launches Google search with the text you selected (in Word, Notepad, anywhere, even from the Chrome itself):
Code: Select all
#NoEnv
;#NoTrayIcon
SetBatchLines, -1
#Persistent
SetWorkingDir, %A_ScriptDir%
Menu, Tray, Icon, google.png
UriEncode( uri, enc="UTF-8" )
{
StrPutVar( uri, var, enc )
f := A_FormatInteger
SetFormat, IntegerFast, H
Loop
{
code := NumGet( var, A_Index - 1, "UChar" )
if( ! code )
{
break
}
if( code >= 0x30 && code <= 0x39 ; 0-9
|| Code >= 0x41 && code <= 0x5A ; A-Z
|| code >= 0x61 && code <= 0x7A ) ; a-z
{
res .= Chr( code )
}
else
{
res .= "%" . SubStr( code + 0x100, -1 )
}
}
SetFormat, IntegerFast, %f%
return res
}
StrPutVar( str, ByRef var, enc="" )
{
len := StrPut( str, enc ) * ( enc = "UTF-16" || enc = "CP1200" ? 2 : 1 )
VarSetCapacity( var, len, 0 )
return StrPut( str, &var, enc )
}
trim2( str )
{
return RegExReplace( str, "^[\s ]+|[\s ]+$", "" )
}
clean_spaces( str )
{
return RegExReplace( RegExReplace( str, " ", " " ), " {2,}", " " )
}
^g::
Clipboard := ""
SendInput, ^c
ClipWait, 5
if( ErrorLevel )
{
return
}
;MsgBox, % "https:__www.google.ru_search?q=" . UriEncode( clean_spaces( trim2( Clipboard ) ) )
Run, % "https:__www.google.ru_search?q=" . UriEncode( clean_spaces( trim2( Clipboard ) ) ),,, pid
;WinWait, % "ahk_pid ", pid,, 0.5 ; sometimes causes script to hang :(
WinActivate, % "ahk_pid ", pid