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 := ""
}