Search for selection "inside quotes" on Google

Get help with using AutoHotkey and its commands and hotkeys
User avatar
michaelbeijer
Posts: 40
Joined: 04 Oct 2014, 08:01
Google: https://plus.google.com/+MichaelBeijer/
Location: Hastings, UK.
Contact:

Search for selection "inside quotes" on Google

14 Jan 2015, 10:09

OK, so I have a script that automatically starts a Google search in Chrome on any selection. I just can't figure out how to get it to search for the selection in quotes.

I don't want to search for:
ho ho ho
but:
"ho ho ho"
Here is my script:

Code: Select all

;-----------------------------------------------------------***
; Search Google for selection
{
!#g:: ; GoogleSearch or Show Link with CTRL+G
  prevClipboard := ClipboardAll
  SendInput, ^c 
  ClipWait, 1
  if !(ErrorLevel)  { 
    Clipboard := RegExReplace(RegExReplace(Clipboard, "\r?\n"," "), "(^\s+|\s+$)")
    If SubStr(ClipBoard,1,7)="http://"
      Run, %Clipboard%
    else 
      Run, % "http://www.google.co.uk/search?hl=en&q=" Clipboard
  } 
  Clipboard := prevClipboard
return
}
;-----------------------------------------------------------***
Any pointers would be much appreciated!

Michael
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Search for selection "inside quotes" on Google

14 Jan 2015, 10:14

this:

Code: Select all

 Run, % "http://www.google.co.uk/search?hl=en&q=" Clipboard
should be:

Code: Select all

 Run, % "http://www.google.co.uk/search?hl=en&q=" chr(34) Clipboard chr(34)
or

Code: Select all

 Run, % "http://www.google.co.uk/search?hl=en&q=""" Clipboard """"
or :

Code: Select all

 Run, http://www.google.co.uk/search?hl=en&q="%Clipboard%"
User avatar
michaelbeijer
Posts: 40
Joined: 04 Oct 2014, 08:01
Google: https://plus.google.com/+MichaelBeijer/
Location: Hastings, UK.
Contact:

Re: Search for selection "inside quotes" on Google

14 Jan 2015, 10:28

Thanks MJs, but these three all get me the Google search page and insert only the first word of any multi-word selection. Not sure what's going wrong.

Michael
GEV
Posts: 844
Joined: 25 Feb 2014, 00:50

Re: Search for selection "inside quotes" on Google

14 Jan 2015, 11:13

Try

Code: Select all

Run, http://www.google.com/search?source=ig&hl=en&rlz=&q=%Clipboard%&btnG=Google+Search&aq=f
User avatar
michaelbeijer
Posts: 40
Joined: 04 Oct 2014, 08:01
Google: https://plus.google.com/+MichaelBeijer/
Location: Hastings, UK.
Contact:

Re: Search for selection "inside quotes" on Google

14 Jan 2015, 11:32

Hi GEV,

Yours inserts all selected terms, but without the quotes :-(
User avatar
LinearSpoon
Posts: 156
Joined: 29 Sep 2013, 22:55

Re: Search for selection "inside quotes" on Google

14 Jan 2015, 14:42

Your problem is most likely that characters like quote and space need to be escaped in urls. This is sort of a brute force solution that just escapes the entire search term.

Code: Select all

SetFormat, Integer, H

!#g:: ;Requires integer format set to hex (See SetFormat command)
  selection := getSelection()
  str := ""
  Loop, Parse, selection
    str .= "%" SubStr(asc(A_LoopField), 3)
  Run, http://www.google.com/search?source=ig&hl=en&rlz=&q=`%22%str%`%22&btnG=Google+Search&aq=f
return

getSelection()
{
  clip_backup := ClipboardAll ;Save the old clipboard
  clipboard := ""
  Send, ^c 
  Clipwait, 3
  selection := clipboard ;Get selection text - may be blank if clipwait timed out/nothing was copied
  clipboard := clip_backup ;Restore clipboard
  return RegexReplace(selection, "`r") 
}
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Search for selection "inside quotes" on Google

14 Jan 2015, 22:50

michaelbeijer wrote:Thanks MJs, but these three all get me the Google search page and insert only the first word of any multi-word selection. Not sure what's going wrong.

Michael
to be sure don't use the Run command, get the variable value directly then see if it's as you desire, url+"Clipboard" if so then look at the other parties involved, that's how you make sure what's wrong
you may need to encode the content of thr clipboard
or send the text via commands
get the url that you want to send, and see
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Search for selection "inside quotes" on Google

14 Jan 2015, 22:54

GEV wrote:Try

Code: Select all

Run, http://www.google.com/search?source=ig&hl=en&rlz=&q=%Clipboard%&btnG=Google+Search&aq=f
then use this:

Code: Select all

Run, http://www.google.com/search?source=ig&hl=en&rlz=&q="%Clipboard%"&btnG=Google+Search&aq=f
User avatar
michaelbeijer
Posts: 40
Joined: 04 Oct 2014, 08:01
Google: https://plus.google.com/+MichaelBeijer/
Location: Hastings, UK.
Contact:

Re: Search for selection "inside quotes" on Google

15 Jan 2015, 06:36

LinearSpoon wrote:Your problem is most likely that characters like quote and space need to be escaped in urls. This is sort of a brute force solution that just escapes the entire search term.

Code: Select all

SetFormat, Integer, H

!#g:: ;Requires integer format set to hex (See SetFormat command)
  selection := getSelection()
  str := ""
  Loop, Parse, selection
    str .= "%" SubStr(asc(A_LoopField), 3)
  Run, http://www.google.com/search?source=ig&hl=en&rlz=&q=`%22%str%`%22&btnG=Google+Search&aq=f
return

getSelection()
{
  clip_backup := ClipboardAll ;Save the old clipboard
  clipboard := ""
  Send, ^c 
  Clipwait, 3
  selection := clipboard ;Get selection text - may be blank if clipwait timed out/nothing was copied
  clipboard := clip_backup ;Restore clipboard
  return RegexReplace(selection, "`r") 
}
Thanks LinearSpoon!

So far, yours is the only one that works!
User avatar
michaelbeijer
Posts: 40
Joined: 04 Oct 2014, 08:01
Google: https://plus.google.com/+MichaelBeijer/
Location: Hastings, UK.
Contact:

Re: Search for selection "inside quotes" on Google

15 Jan 2015, 06:37

MJs wrote:
GEV wrote:Try

Code: Select all

Run, http://www.google.com/search?source=ig&hl=en&rlz=&q=%Clipboard%&btnG=Google+Search&aq=f
then use this:

Code: Select all

Run, http://www.google.com/search?source=ig&hl=en&rlz=&q="%Clipboard%"&btnG=Google+Search&aq=f
Thanks, but can't get this to work either.

I did manage to get it working with LinearSpoon's version though!
User avatar
michaelbeijer
Posts: 40
Joined: 04 Oct 2014, 08:01
Google: https://plus.google.com/+MichaelBeijer/
Location: Hastings, UK.
Contact:

Re: Search for selection "inside quotes" on Google

15 Jan 2015, 06:50

LinearSpoon wrote:Your problem is most likely that characters like quote and space need to be escaped in urls. This is sort of a brute force solution that just escapes the entire search term.

Code: Select all

SetFormat, Integer, H

!#g:: ;Requires integer format set to hex (See SetFormat command)
  selection := getSelection()
  str := ""
  Loop, Parse, selection
    str .= "%" SubStr(asc(A_LoopField), 3)
  Run, http://www.google.com/search?source=ig&hl=en&rlz=&q=`%22%str%`%22&btnG=Google+Search&aq=f
return

getSelection()
{
  clip_backup := ClipboardAll ;Save the old clipboard
  clipboard := ""
  Send, ^c 
  Clipwait, 3
  selection := clipboard ;Get selection text - may be blank if clipwait timed out/nothing was copied
  clipboard := clip_backup ;Restore clipboard
  return RegexReplace(selection, "`r") 
}
Hey, I noticed that the line:
in your code gets to the final search page via a redirect:
If I use this instead, it goes straight to the search:
Any idea what the difference is?
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Search for selection "inside quotes" on Google

15 Jan 2015, 15:00

I was under the impression that the problem was because you didn't include the quotes in your script, but the problem was the way Chrome handles urls and command line params since the same work in IE, that why LinearSpoon way works because it encodes the search query.
it redirects to the secure http server,

Return to “Ask For Help”

Who is online

Users browsing this forum: Bar_Dres, Bing [Bot], Google [Bot], Spawnova, XMCQCX, yh0099 and 43 guests