Page 1 of 1

help required for a script that should open a browser and search using the values stored in the clipboard

Posted: 02 May 2024, 17:42
by slyfox1186
This script's purpose is to do the following:

If a URL or string is in the clipboard when the hotkey is triggered the open the chrome browser and search google if it is a plain non-url string and open a website directly if it is a website URL.

The script should always work and open the chrome browser UNLESS there is nothing in the clipboard by the time the code reaches the part where the browser is to open the strings stored.

My issue is that this script only works when there is not highlighted text and the clipboard contains acceptable strings or URLS.
It should be able to read highlighted text and also use what is in the clipboard when no text is highlighted as long as the clipboard is not blank.

I am starting to this this is a bug in the clipwait command for version 2.12 and 2.13.

Can anyone help me fix this script?

Code: Select all

/*____________________________________________________________________________________

    Open your browser and search the clipboard or the currently highlighted text using
    Google or if it is a website then directly load it.

^!c:: {
    browser := A_ProgramFiles . "\Google\Chrome Beta\Application\chrome.exe" ; Update this path as necessary
    win := "ahk_class Chrome_WidgetWin_1 ahk_exe chrome.exe"

    ClipSaved := ClipboardAll() ; Save the current Clipboard contents

    Send "^c"
    if !ClipWait(1)
        A_Clipboard := ClipSaved

    ; Trim whitespace from the clipboard text for accurate URL detection
    searchText := Trim(A_Clipboard, " `t`r`n")

    ; Determine if the searchText looks like a URL. If not, prepare a Google search.
    isUrl := RegExMatch(searchText, "i)^(https?:\/\/)?[\w.-]+(\.[a-zA-Z]{2,})+(\/\S*)?$")
    searchOrUrl := isUrl ? searchText : "https://www.google.com/search?q=" . searchText

    ; Open the browser with the URL or search query
    ff_cmd := '"' . browser . '"' . ' --new-tab ' . '"' . searchOrUrl . '"'
    Run(ff_cmd,, "Max")

    ; Attempt to bring the browser window to the foreground
    if WinWait(win,, 2) {
        WinActivate(win)
    } else {
        reload
    }

    ; Restore the original clipboard content
    A_Clipboard := ""
}

Re: help required for a script that should open a browser and search using the values stored in the clipboard

Posted: 02 May 2024, 18:13
by mikeyww
:arrow: How to use ClipWait

Code: Select all

#Requires AutoHotkey v2.0
find  := 'https://www.google.com/search?q='
nav   := (str) => Run(isURL(str) ? str : find encodeDecodeURI(str))
isURL := (str) => RegExMatch(str, '^(https?://)?[\w/?=%.-]+\.[\w/&?=%.-]+$')

^F5:: {
 clip := A_Clipboard
 A_Clipboard := ''
 Send '^c'
 (ClipWait(1)) ? nav(A_Clipboard) : clip = '' ? SoundBeep(1500) : nav(clip)
}

encodeDecodeURI(str, encode := true, component := true) {
 ; Adapted from teadrinker: https://www.autohotkey.com/boards/viewtopic.php?p=372134#p372134
 ; https://www.autohotkey.com/boards/viewtopic.php?p=516899#p516899
 Static Doc, JS
 If !IsSet(Doc) {
  Doc := ComObject('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)
}

Re: help required for a script that should open a browser and search using the values stored in the clipboard

Posted: 02 May 2024, 18:21
by divanebaba
Hello.
Here is a similar topic from v1.
viewtopic.php?t=93891
Maybe you can transform it into v2. :D

Re: help required for a script that should open a browser and search using the values stored in the clipboard

Posted: 02 May 2024, 21:39
by slyfox1186
@mikeyww
This did not work well. It only opened links not highlighted and were already in the clipboard. Any links that were highlighted did not work.

Regardless, thank you for showing me new ways to go about things. If you have any other ideas please let me know because this is still not solved.

I must say it again: I am getting to be convinced that this is a software-related bug.