Script error search google

Share your favorite FastKeys commands
Post Reply
Elermino
Posts: 11
Joined: Nov 12th, ’21, 19:20

Post by Elermino » Dec 10th, ’21, 02:25

I wrote this script and when selecting the urls to search for a page it works correctly, but if I select a word it shows an error in regEx and then it searches for it. This is the code:

Code: Select all

Clipboard =
Send ^c
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else Run, % Clipboard ~= "\b(?:https?://)?(?:(?i:[a-z]+\.)+)[^\s,]+\b"
          ? Clipboard : fix("https://www.google.com/search?q=" Clipboard)
Return

fix(url) {
 RegExMatch(url, "^(https?://)?(.+?)(/)?$", part)
 Loop, Parse, part2
  text .= A_LoopField ~= "[$&+,:;@""<>`%{}|\\^~[\] `]" ? Format("%{:X}", Asc(A_LoopField)) : A_LoopField
 Return part1 text part3
}
User avatar
Marko
Posts: 1551
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Dec 10th, ’21, 20:46

Sorry, what is the purpose of the script?
Elermino
Posts: 11
Joined: Nov 12th, ’21, 19:20

Post by Elermino » Dec 10th, ’21, 22:30

Marko wrote:
Dec 10th, ’21, 20:46
Sorry, what is the purpose of the script?
It is used to search for the selected text in Google, depending on whether it is a url or a normal text. Validate if it is a url with regEx and it goes directly to the page, if not, it searches the text in google as a normal query.
Elermino
Posts: 11
Joined: Nov 12th, ’21, 19:20

Post by Elermino » Dec 10th, ’21, 22:34

Elermino wrote:
Dec 10th, ’21, 22:30
Marko wrote:
Dec 10th, ’21, 20:46
Sorry, what is the purpose of the script?
It is used to search for the selected text in Google, depending on whether it is a url or a normal text. Validate if it is a url with regEx and it goes directly to the page, if not, it searches the text in google as a normal query.
I already managed to get the code to work. Apparently FastKeys does not work the same as an active Autohotkey command, since it requires variable initialization.
Here the code:

Code: Select all

Clipboard =
Send ^c
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else Run, % Clipboard ~= "\b(?:https?://)?(?:(?i:[a-z]+\.)+)[^\s,]+\b"
          ? Clipboard : fix("https://www.google.com/search?q=" Clipboard)
Return

fix(url) {
 text := "", RegExMatch(url, "^(https?://)?(.+?)(/)?$", part)
 Loop, Parse, part2
  text .= A_LoopField ~= "[$&+,:;@""<>`%{}|\\^~[\] `]" ? Format("%{:X}", Asc(A_LoopField)) : A_LoopField
 Return part1 text part3
}
Post Reply