Google search tool - Help removing certain characters from clipboard. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Barnaby Ray
Posts: 45
Joined: 09 Nov 2021, 07:48

Google search tool - Help removing certain characters from clipboard.

Post by Barnaby Ray » 25 Apr 2022, 06:54

Hi everyone, I wondered if anyone can help me with a Google search tool I'm making to search for musical artists.

What I want to do, is select some text, then by using a hotkey it will perform a google search of the selected text inside quotation marks, followed by musical artist. So if I select Radiohead, the search will be "Radiohead" musical artist.

I've got it working for most searches, but if the band name includes an & symbol, the google search misses the & symbol and everything after it.. and if the band name also has quotation marks in it, ie DANIELA ALBARRAN "LA BRUJITA", the google search cuts off everything after the first quotation mark..

Is there a way to ignore the & symbol and quotation marks inside the clipboard?

Here is the script so far..

Code: Select all

^!m::
	Clipboard := ""
	send, ^c
	ClipWait, 2
	if ErrorLevel
	   {
	    MsgBox, The attempt to copy text onto the clipboard failed. 
	    return
	   }
	Clipboard=%Clipboard% 
	sleep, 200
	Run chrome.exe "http://www.google.com/search?q=`%22%clipboard%`%22 musical artist"
	sleep 200
	return


Barnaby Ray
Posts: 45
Joined: 09 Nov 2021, 07:48

Re: Google search tool - Help removing certain characters from clipboard.

Post by Barnaby Ray » 27 Apr 2022, 09:51

Thanks @mikeyww ... I will try to get my head around that :)

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Google search tool - Help removing certain characters from clipboard.

Post by mikeyww » 27 Apr 2022, 10:05

It would just be a call to teadrinker's function. The function would return a string that has appropriately transformed the special characters (&, ?, etc.) so that the URL still works. Of course, an alternative is to use StrReplace to delete those symbols, but the function may be convenient.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Google search tool - Help removing certain characters from clipboard.

Post by BoBo » 27 Apr 2022, 10:56

Coincidentally I've stumbled over this API a day ago… probably worth a look? :shh:
http://api.chartlyrics.com/apiv1.asmx

Barnaby Ray
Posts: 45
Joined: 09 Nov 2021, 07:48

Re: Google search tool - Help removing certain characters from clipboard.

Post by Barnaby Ray » 28 Jun 2022, 11:50

@mikeyww

Hi, sorry it's been a while, I've been trying to figure this out for a while, but it's still not working.. wondered if you can tell me what I'm doing wrong?

I want to be able to highlight some text on a webpage, and when I run the script, it will copy the selected text, then perform a google search with the search term inside quotation marks, and followed by musical artist

So if the text is;

Ben & Holly

I want it to search for;

"Ben & Holly" musical artist

With my current script, I've tried to include the function you suggested, but I don't think I've put it in the right place? When I run it I still just get a search for;

"Ben

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance Force
#Persistent

^F1::

	Clipboard := ""
	winactivate, ahk_exe chrome.exe
	sleep, 50
	send, ^c
	ClipWait, 2
	if ErrorLevel
	   {
	    MsgBox, The attempt to copy text onto the clipboard failed.
	    return
	   }
	Clipboard=%Clipboard%
	sleep, 200
    str := Clipboard
	decoded := EncodeDecodeURI(str, false)
		sleep 200
	MsgBox, % decoded
	Run chrome.exe "http://www.google.com/search?q=`%22%decoded%`%22 musical artist"
	sleep 200
	return

EncodeDecodeURI(str, encode := true, component := true) {
   static Doc, JS
   if !Doc {
      Doc := ComObjCreate("htmlfile")
      Doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
      JS := Doc.parentWindow
      ( Doc.documentMode < 9 && JS.execScript() )
   }
   Return JS[ (encode ? "en" : "de") . "codeURI" . (component ? "Component" : "") ](str)
}

ESC::ExitApp

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Google search tool - Help removing certain characters from clipboard.  Topic is solved

Post by mikeyww » 28 Jun 2022, 12:21

Code: Select all

^F1::
Clipboard =
Send ^c
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else Run, % "chrome.exe http://www.google.com/search?q=" EncodeDecodeURI(Trim(Clipboard))
Return

EncodeDecodeURI(str, encode := true, component := true) {
 Static Doc, JS
 If !Doc
   Doc := ComObjCreate("htmlfile"), Doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
  , JS := Doc.parentWindow        , (Doc.documentMode < 9 && JS.execScript())
 Return JS[(encode ? "en" : "de") "codeURI" (component ? "Component" : "")](str)
}

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Google search tool - Help removing certain characters from clipboard.

Post by flyingDman » 28 Jun 2022, 13:14

Perhaps:

Code: Select all

var = A&B musical artist
Run % "chrome.exe ""http://www.google.com/search?q=" strreplace(var,"&","`%26") ""
14.3 & 1.3.7

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Google search tool - Help removing certain characters from clipboard.

Post by mikeyww » 28 Jun 2022, 14:04

Certainly; and can add "musical artist" or whatever as noted, to the search string. The encoder handles various types of punctuation, spaces, etc. that may also require special handling.

Barnaby Ray
Posts: 45
Joined: 09 Nov 2021, 07:48

Re: Google search tool - Help removing certain characters from clipboard.

Post by Barnaby Ray » 29 Jun 2022, 05:10

Amazing, thank you so much!

Post Reply

Return to “Ask for Help (v1)”