Advertisement
slyfox1186

pass-clipboard-to-google-v4.1.ahk

Oct 20th, 2022 (edited)
2,145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*____________________________________________________________________________________
  2.  
  3.     Creator:       SlyFox1186
  4.     Pastebin:      https://pastebin.com/u/slyfox1186
  5.  
  6.     Purpose:       Copy any highlighted text to the clipboard or use the
  7.                    current clipboard contents and search google's engine.
  8.  
  9.     Instructions:  Replace the _browser variable below with the full path of your browser's exe.
  10.  
  11.  
  12.     Update:        2.0 (12.16.21)
  13.                    Added ability to parse strings that start with http or https
  14.  
  15.     Update:        3.0 (10.20.22)
  16.                    Added RegEx filtering to only identify primary domains with matching endings.
  17.                    See below to customize the inputs as needed.
  18.  
  19.     Update:        v4.0 (11.23.22)
  20.                    Fixed RegEx false negative issues among other problems.
  21.  
  22.     Update:        v4.1 (12.02.22)
  23.                    Removed an unnecessary duplicate step.
  24.  
  25.     Test:          google.com
  26.                    battle.net
  27.                    https://test.xyz   ; this should not work as it is not included in the list of acceptable names below.
  28.                    www.yahoo.com
  29.                    https://stackoverflow.com/users/10572786/slyfox1186
  30. */
  31.  
  32. ^!c::
  33. {
  34.     _browser := "C:\Program Files\Google\Chrome Beta\Application\chrome.exe"
  35.     setcontroldelay, -1 ; make sure the script is running as fast as possible
  36.     _clipsaved := clipboardAll ; save current clipboard contents to its own variable
  37.     clipboard := "" ; empty the clipboard
  38.     sendinput, ^c ; make the clipboard ready to receive any possible input
  39.     clipwait, 2.5 ; wait for that input to come through for no more than 2.5 second
  40.  
  41.     if (errorlevel)
  42.         clipboard := _clipsaved ; if nothing is saved in the clipboard from the clipwait command then use the current clipboard's contents
  43.  
  44.     clipboard := trim(clipboard) ; trim both ends of the string found in clipboard
  45.  
  46.     ; task: list the types of primary domains you want regex to attempt to match.
  47.     ; info: change the below domains as needed.
  48.     _domains := [   .com
  49.                 ,   .de
  50.                 ,   .gov
  51.                 ,   .io
  52.                 ,   .jp
  53.                 ,   .net
  54.                 ,   .org
  55.                 ,   .to
  56.                 ,   .tv
  57.                 ,   .uk   ]
  58.  
  59.     ; test each of the primary domains above with regex expressions for any matches.
  60.     for each, _domain in _domains
  61.     {
  62.         RegExMatch(clipboard, "\.[a-z]{2,3}$|.*$", _isMatch)
  63.         RegExMatch(clipboard, "\.[a-z]{2,3}", _isMatchEnd)
  64.         while (_ismatchend = _domain)
  65.         {
  66.             run, "%_browser%" --new-tab "%_isMatch%",, max, _wpid
  67.             WinWait, ahk_pid %_wpid%,, 2
  68.             WinSet, Top,, ahk_pid %_wpid%
  69.             WinActivate, ahk_pid %_wpid%
  70.             return
  71.         }
  72.     }
  73.  
  74.     /*
  75.         task: modify the current string stored in the clipboard.
  76.  
  77.         info: search engines use uri-type encoding which means
  78.               you need must replace certain characters with others
  79.               for the engine to understand your query correctly.
  80.     */
  81.  
  82.     stringreplace, clipboard, clipboard, `r`n, `%20, All
  83.     stringreplace, clipboard, clipboard, !, `%21, All
  84.     stringreplace, clipboard, clipboard, #, `%23, All
  85.     stringreplace, clipboard, clipboard, $, `%24, All
  86.     stringreplace, clipboard, clipboard, `%`%, `%25, All
  87.     stringreplace, clipboard, clipboard, &, `%26, All
  88.     stringreplace, clipboard, clipboard, ', `%27, All
  89.     stringreplace, clipboard, clipboard, (, `%28, All
  90.     stringreplace, clipboard, clipboard, ), `%29, All
  91.     stringreplace, clipboard, clipboard, -, `%2D, All
  92.     stringreplace, clipboard, clipboard, ., `%2E, All
  93.     stringreplace, clipboard, clipboard, *, `%30, All
  94.     stringreplace, clipboard, clipboard, :, `%3A, All
  95.     stringreplace, clipboard, clipboard, ?, `%3F, All
  96.     stringreplace, clipboard, clipboard, @, `%40, All
  97.     stringreplace, clipboard, clipboard, [, `%5B, All
  98.     stringreplace, clipboard, clipboard, ], `%5D, All
  99.     stringreplace, clipboard, clipboard, ^, `%5E, All
  100.     stringreplace, clipboard, clipboard, _, `%5F, All
  101.     stringreplace, clipboard, clipboard, ``, `%60, All
  102.     stringreplace, clipboard, clipboard, {, `%7B, All
  103.     stringreplace, clipboard, clipboard, |, `%7C, All
  104.     stringreplace, clipboard, clipboard, }, `%7D, All
  105.     stringreplace, clipboard, clipboard, ~, `%7E, All
  106.     stringreplace, clipboard, clipboard, \, /, All
  107.  
  108.     clipboard := trim(clipboard) ; trim the ends of the string again
  109.  
  110.     run, "%_browser%" --new-tab "https://google.com/search?q=%clipboard%",, max, _wpid
  111.     WinWait, ahk_pid %_wpid%,, 2
  112.     WinSet, Top,, ahk_pid %_wpid%
  113.     WinActivate, ahk_pid %_wpid%
  114. }
  115. return
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement