Problem with quotes Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
SyntaxTerror
Posts: 53
Joined: 23 May 2017, 12:55

Problem with quotes

13 Apr 2024, 08:20

Hello

I have a piece of code I found quite a long time ago (unfortunately, I cannot remember where) that allows me to select some text and search for it on Google in Firefox using a hotkey :

Code: Select all

^²::
	Send, ^c
	Sleep 50
	parameter = C:\Program Files\Mozilla Firefox\firefox.exe https://www.google.com/search?channel=crow5&client=firefox-b-d&q="%clipboard%"
	Run %parameter%
Return
(note that I have an AZERTY keyboard, so the ² is easily accessible as it is between Esc and Tab)

The only problem is when I search for text in quotes or containing quotes, as this causes a problem with the URL (i.e. the "%clipboard%" part).
Is there a way to avoid this, or is the only solution to avoid quotes in my selections?

Thank you for your help.
Last edited by SyntaxTerror on 13 Apr 2024, 09:41, edited 1 time in total.
User avatar
mikeyww
Posts: 27132
Joined: 09 Sep 2014, 18:38

Re:

13 Apr 2024, 09:29

Code: Select all

#Requires AutoHotkey v1.1.33.11
ff := A_ProgramFiles "\Mozilla Firefox\firefox.exe"

F3::
; ^²::
Clipboard := ""
Send ^c
ClipWait 0
If ErrorLevel
 MsgBox 48, Error, An error occurred while waiting for the clipboard.
Else Run % ff " https://www.google.com/search?q=" encodeDecodeURI(Clipboard)
Return

encodeDecodeURI(str, encode := True, component := True) {
 ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=84825
 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)
}
Please create an informative subject for your post.
User avatar
SyntaxTerror
Posts: 53
Joined: 23 May 2017, 12:55

Re: Problem with quotes  Topic is solved

13 Apr 2024, 10:09

Hello @mikeyww and thank you for your quick answer.

I updated my AHK version from 1.1.33.10 to 1.1.37.02 and removed the F3:: line and the ; before ^²::, and your script seems to work, but I also had to remove the space before https in Else Run % ff " https://www.google.com/search?q=" encodeDecodeURI(Clipboard) to avoid an error message.

With ² as the hotkey, the working script is :

Code: Select all

#Requires AutoHotkey v1.1.33.11
ff := A_ProgramFiles "\Mozilla Firefox\firefox.exe"

^²::
Clipboard := ""
Send ^c
ClipWait 0
If ErrorLevel
 MsgBox 48, Error, An error occurred while waiting for the clipboard.
Else Run % ff "https://www.google.com/search?q=" encodeDecodeURI(Clipboard)
Return

encodeDecodeURI(str, encode := True, component := True) {
 ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=84825
 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)
}
Thanks again @mikeyww :thumbup:
User avatar
mikeyww
Posts: 27132
Joined: 09 Sep 2014, 18:38

Re: Problem with quotes

13 Apr 2024, 10:29

Hmm. That makes no sense to me and does not work here, but it's great if you have it working!
User avatar
SyntaxTerror
Posts: 53
Joined: 23 May 2017, 12:55

Re: Problem with quotes

13 Apr 2024, 11:36

I had this error message (the part in red is my translation to English) : https://imgur.com/0P9yUiz
User avatar
mikeyww
Posts: 27132
Joined: 09 Sep 2014, 18:38

Re: Problem with quotes

13 Apr 2024, 17:10

I see. You were not running the script that I posted, but a different script. You can always just copy and paste if you wish. Syntax matters!
User avatar
SyntaxTerror
Posts: 53
Joined: 23 May 2017, 12:55

Re: Problem with quotes

13 Apr 2024, 17:59

mikeyww wrote:
13 Apr 2024, 17:10
I see. You were not running the script that I posted, but a different script. You can always just copy and paste if you wish. Syntax matters!
I posted the error message from your first version of the script I tried just a couple minutes after you posted it, but the second one had the same problem : https://i.imgur.com/O768pFi.png
(the line numbers may have changed a bit as I edited this script that is much bigger and has many lines above).
User avatar
mikeyww
Posts: 27132
Joined: 09 Sep 2014, 18:38

Re: Problem with quotes

13 Apr 2024, 18:17

But your error message shows a line that is not actually in my script. Are you spotting the difference? Instead of editing the script, why not test it the way it is?
User avatar
SyntaxTerror
Posts: 53
Joined: 23 May 2017, 12:55

Re: Problem with quotes

13 Apr 2024, 18:37

mikeyww wrote:
13 Apr 2024, 18:17
But your error message shows a line that is not actually in my script. Are you spotting the difference? Instead of editing the script, why not test it the way it is?
I copied the script from your answer above and tested it (again) just now.
I agree that there is a difference between your script above and the error message, but it's only the lines that are cut differently, for example, line 198, which is split into two lines.
Or we're really struggling to understand each other. :wtf:
User avatar
mikeyww
Posts: 27132
Joined: 09 Sep 2014, 18:38

Re: Problem with quotes

13 Apr 2024, 18:40

I am seeing the following on line 10.

Code: Select all

Else Run % ff "https://www.google.com/search?q=" encodeDecodeURI(Clipboard)
  1. Are you seeing that line, too?
  2. Did you copy and paste the script so that you could run it without changing it and without adding anything to it?
  3. My script has 20 lines, not 198. Why not test it?
User avatar
SyntaxTerror
Posts: 53
Joined: 23 May 2017, 12:55

Re: Problem with quotes

13 Apr 2024, 19:27

mikeyww wrote:
13 Apr 2024, 18:40
I am seeing the following on line 10.

Code: Select all

Else Run % ff "https://www.google.com/search?q=" encodeDecodeURI(Clipboard)
  1. Are you seeing that line, too?
  2. Did you copy and paste the script so that you could run it without changing it and without adding anything to it?
  3. My script has 20 lines, not 198. Why not test it?
  1. No, I'm seeing this : https://i.imgur.com/3ZcgZ4I.png (even after clearing the cache of this page)
  2. Yea, I tried it with F3 as a hotkey too
  3. I just made a new .ahk file and it works (the script below), and if I put this script without changes before all my other scripts in my main AHK script, it works also... :crazy: There is probably some syntax error before the line 180 of my main AHK script, but I'm not able to find it...
    mikeyww wrote:
    13 Apr 2024, 09:29

    Code: Select all

    #Requires AutoHotkey v1.1.33.11
    ff := A_ProgramFiles "\Mozilla Firefox\firefox.exe"
    
    F3::
    ; ^²::
    Clipboard := ""
    Send ^c
    ClipWait 0
    If ErrorLevel
     MsgBox 48, Error, An error occurred while waiting for the clipboard.
    Else Run % ff " https://www.google.com/search?q=" encodeDecodeURI(Clipboard)
    Return
    
    encodeDecodeURI(str, encode := True, component := True) {
     ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=84825
     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)
    }
Anyway, it works now, and it's a bit late here...
I'll have a closer look at my main AHK script tomorrow to try and find the problem, but my main issue with my Google search is solved.
Thank you again for helping me with it and for your patience, given that I can be a horrible noob at times.
User avatar
mikeyww
Posts: 27132
Joined: 09 Sep 2014, 18:38

Re: Problem with quotes

13 Apr 2024, 19:41

You are not able to find it, but....

Code: Select all

Run,ff " https.....
vs.
Run % ff " https....
Explained: Expressions

How to see this?

Code: Select all

#Requires AutoHotkey v1.1.33.11
ff := "firefox.exe"
MsgBox,ff " https....."
MsgBox % ff " https...."
Another way to find the problem: look for "--->" in the error message that you saw. This arrow points to the problem!

AHK v2 never suffers from this legacy syntax problem, because it uses expressions throughout all scripts.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 115 guests