hjf

Friday, January 1, 2016

[Obsolete] - Web search query with AutoHotKey (InputBox)


Web search query with AutoHotKey (InputBox)



A few days ago I wrote about doing web searches from the command line in both Windows and Linux, but today I am posting about doing web searches with AutoHotKey (Windows only).
While my other scripts are just fine for some people, some people prefer to either stick to one system or hate bat/cmd and since I love AHK I figured I'd write a simple search tool for it using hotkeys. For those who aren't aware, AutoHotKey is a scripting language aimed at; as its name implies, using hotkeys to automate tasks. AHK was forked from a project called AutoIT which has some hotkey accessibility, but more aimed at GUIs (plan to do a AU3 project in the near future).
The script will be posted below with in depth comments to help you better understand the script or if you prefer I will also provide 32 and 64 bit compiled executables for the Google versions only.
Note: If you do not enter any search terms and exit the gui in anyway it will automatically open Google.com in your browser, but if you do enter something it will open your browser with the search engine you have set in your browser.

Main AutoHotKey Script

Download google.ahk script file
Or read and copy this to a text file and save as anything_you_want.ahk (google.ahk):
; AHK script created by Lateralus138
; Google search gui
; Key Control+Shift+g (^+g)

#SingleInstance, Force

; Tray icon function (just for fun),
; provide resource library name and 
; icon number and tray message.
; This changes icons in the system tray 
; calling system icons from imageres.dll 
; or the library of your choice.
; E.g. TrayIcon("imageres.dll", 21, "Google Search") - where 
; imageres.dll is the resource library, 21 is the resource 
; icon number, and Google Search is the text to display in
; your system tray.

TrayIcon(ResNam, ResNum, TrayMsg )
 {
  Menu, Tray, Icon, %ResNam%, %ResNum%
  Menu, TRAY, Tip, %TrayMsg%
 }


; Tooltip w/timer function, provide
; text and time.
tttimer2(tttext2, tttime2)
 {
  ToolTip, %tttext2%
  Sleep, %tttime2%
  ToolTip
 }

; Function to activate your web browser if it's already
; running change browser to your preference.
; I use chrome.exe in this script
ChromeActive()
 {
  WinActivate , ahk_exe chrome.exe
  Sleep , 500
  Send , {Ctrl Down}t{Ctrl Up}
  Sleep , 500
 }

; Function to browser if not already running.
StartChrome()
 {
  Run , chrome.exe
  Sleep , 5000
  ChromeActive()
 }


; Function to type just your web engines main url without
; a query in your web browser.
SndE()
 {
  Send , https://www.google.com/{Enter}
 }

; SetFormat for converting decimals to whole numbers then
; WinGetPos to get your screen resolution.
; SWD and SHT variables for positioning the search bar in
; the center of the screen.
SetFormat, FloatFast, 1
WinGetPos,,, desk_width, desk_height, Program Manager  
SWD := (desk_width / 2) - 400    
SHT := (desk_height / 2) - 72


; Initially call main tray icon
Gosub, TrayIconSub

; Start hotkey, main program funtion.

; Change hotkey in the line below.
; Currently ^+g (CTRL+SHIFT+g, g for Google, but this is
; your choice)
^+g::

; Call tray icon function when hotkeys are pushed, calls
; icon for main GUI window in the system tray.
TrayIcon("imageres.dll", 95, "What would you like to search for?")

; Call to GUI window. Waits for your query, if left empty and the gui is exited
; in any way it loads your default search engine with no query; if your query is
; provided it then opens your query in your browser and search engine of choice.
InputBox, GoogleQuery , Google Search, `Leave empty and hit [Enter] to open Google.com.`n`Your search query`:,,800, 144, %SWD%, %SHT%

; Process actions after GUI closes. Tests if your query is provided
; and calls the approriate functions.
if ! GoogleQuery
 {
  IfWinExist, ahk_exe chrome.exe
   {
    ChromeActive()
    SndE()
   }
  Else
   {
    StartChrome()
    SndE()
   }
 }
Else
 {
  IfWinExist, ahk_exe chrome.exe
   {
    ChromeActive()
    Send , %GoogleQuery%{Enter}
   }
  Else
   {
    StartChrome()
    Send , %GoogleQuery%{Enter}
   }
 }

Gosub, TrayIconSub
Return

; Resets tray icon.
TrayIconSub:
TrayIcon("imageres.dll", 21, "Google Search")
Return

Executables

Double click to run or you can run from a command line. These executables work with Chrome browser and Google.com only.
Rename these as you like:

No comments: