I wanted a script which made my most frequent operations more convenient, so I created this one by combining the
copy on select and the
quick info lookup scripts.
I don't think I'll do major changes on it, so I put it here for backup.
Features:
- When something is selected with the mouse the selection is immediately copied to the clipboard. No need for pressing Ctrl+C. It is recommended to use a clipboard manager (like clipx) together with this feature. Pasting can be done the usual way or with middle click.
- After selection a green box appears beside the mouse cursor. If you pull the mouse into it then a menu appears where you can select from various operations (search, etc) to be performed on the selected text.
- If you don't use the green box, but later you want to do something with clipboard content then you can press pause and the menu appears with a tooltip indicating the current clipboad contents. I recommend using the item shortcut keys in the menu for speed and convenience.
- If you press Shift+pause then an input box appears in the center of the screen. You can type something in it, press enter and the usual menu appears. The input box can be dismissed with esc. The input overwrites the clipboard, but for me it's not a big deal, because I use clipx mentioned above.
Here's the script. The default menu operations are those which I use. You may want to customize the menu according to your own needs.
Code:
; background color of the little indicator box which appears
; when something is selected
cos_selection_indicator_background_color = 66FF00
; background color of the input box
cos_gui_background_color = 66CCFF
; size of the selection indicator box in pixels
cos_selection_indicator_size = 20
; distance of selection indicator box from the mouse cursor in pixels
cos_selection_indicator_distance = 15
; if the distance of mouse cursor from the selection indicator box is
; larger than this value (in pixels) then the indicator is hidden
cos_selection_indicator_hide_distance := 20
ibc_input_check_delay = 100
ibc_completion_file := A_Temp . "\ibc_ompletions"
if (cos_selection_indicator_distance > cos_selection_indicator_hide_distance)
{
msgbox if the indicator distance is greater than the hide distance then the indicator will be hidden immediately. fix it!
exitapp
}
CoordMode, Mouse, Screen
CoordMode, Menu, Screen
Gui 22: +Owner -Caption +AlwaysOnTop
Gui 22: Color, % cos_selection_indicator_background_color
Gui 23: +Owner -Caption +AlwaysOnTop
Gui 23: font, s20
Gui 23: Add, Edit, w500 vQuickSearchInput gibc_input_changed
Gui 23: Color, % cos_gui_background_color
Gui 23: +LastFound
ibc_input_gui_id := winexist()
Gui 24: +Owner -Caption +AlwaysOnTop
Gui 24: font, s14
Gui 24: Add, Listbox, x0 y0 w600 r10 vCompletions AltSubmit HwndIbc_Listbox
Gui 24: Color, EEAA99
Gui 24: +LastFound
WinSet, TransColor, EEAA99
Hotkey escape, QuickSearchInputHide, off
Hotkey enter, QuickSearchInputSubmit, off
Hotkey down, ibc_next_completion, off
Hotkey up, ibc_previous_completion, off
Menu QuickSearchMenu, add, &Google, QuickSearchGoogle
Menu QuickSearchMenu, add, &Wikipedia, QuickSearchWikipedia
Menu QuickSearchMenu, add, &Youtube, QuickSearchYoutube
Menu QuickSearchMenu, add, I&MDb, QuickSearchIMDb
Menu QuickSearchMenu, add, Google M&aps, QuickSearchGoogleMaps
Menu QuickSearchMenu, add, Google &Define, QuickSearchGoogleDefine
Menu QuickSearchMenu, add, Google &Images, QuickSearchGoogleImage
Menu QuickSearchMenu, add, Google &News, QuickSearchGoogleNews
Menu QuickSearchMenu, add, Google &Video, QuickSearchGoogleVideo
Menu QuickSearchMenu, add, Go to &URL, QuickSearchGoToUrl
Menu QuickSearchMenu, add, &Translate Any to English, QuickSearchTranslate
; Hungarian-German dictionary
Menu QuickSearchMenu, add, &Sztaki, QuickSearchSztaki
; German dictionary
Menu QuickSearchMenu, add, &Leo, QuickSearchLeo
#IfWinNotActive ahk_class ConsoleWindowClass
~lButton::
cos_mousedrag_treshold := 20 ; pixels
MouseGetPos, cos_mousedrag_x, cos_mousedrag_y
keywait lbutton
mousegetpos, cos_mousedrag_x2, cos_mousedrag_y2
if (abs(cos_mousedrag_x2 - cos_mousedrag_x) > cos_mousedrag_treshold
or abs(cos_mousedrag_y2 - cos_mousedrag_y) > cos_mousedrag_treshold)
{
wingetclass cos_class, A
if (cos_class == "Emacs")
sendinput !w
else
sendinput ^c
MouseGetPos, x, y
cos_selection_indicator_x := x + cos_selection_indicator_distance
cos_selection_indicator_y := y + 5
Gui 22:Show, x%cos_selection_indicator_x% y%cos_selection_indicator_y% w%cos_selection_indicator_size% h%cos_selection_indicator_size% NoActivate
settimer cos_monitor_mouse_cursor_for_selection_indicator, 10
}
return
#IfWinNotActive
; firefox does pasting on middleclick by itself
#IfWinNotActive ahk_class MozillaUIWindowClass
~mbutton::
WinGetClass cos_class, A
;; emacs does pasting on middleclick by itself
if (cos_class <> "Emacs")
SendInput ^v
return
#IfWinNotActive
;rcontrol::
Media_Play_Pause::
pause::
SysGet, Monitor, Monitor
x := (MonitorRight - MonitorLeft) / 2
y := (MonitorBottom - MonitorTop) / 2
ToolTip %clipboard%, x, 0
SetTimer, QuickSearchRemoveToolTip, 5000
Menu QuickSearchMenu, show, %x%, 30
Return
+pause::
Gui 23: Show
hotkey escape, on
hotkey enter, on
sendinput {home}+{end}
WinGetPos ibc_input_gui_x, ibc_input_gui_y, w , ibc_input_gui_height, A
ibc_completion_gui_x := ibc_input_gui_x
ibc_completion_gui_y := ibc_input_gui_y + ibc_input_gui_height + 5
ibc_previous_input =
gosub ibc_input_changed
return
QuickSearchInputSubmit:
Gui 23: submit
clipboard := QuickSearchInput
SysGet, Monitor, Monitor
x := (MonitorRight - MonitorLeft) / 2
y := (MonitorBottom - MonitorTop) / 2
SetTimer, QuickSearchRemoveToolTip, 5000
Gosub QuickSearchInputHide
Menu QuickSearchMenu, show, %x%, %y%
return
QuickSearchInputHide:
Gui 23: Hide
settimer ibc_check_input, off
gosub ibc_hide_completions
hotkey enter, off
hotkey escape, off
return
QuickSearchRemoveToolTip:
SetTimer, QuickSearchRemoveToolTip, Off
ToolTip
Return
cos_monitor_mouse_cursor_for_selection_indicator:
MouseGetPos x, y
if (x >= cos_selection_indicator_x
and x < cos_selection_indicator_x + cos_selection_indicator_size
and y >= cos_selection_indicator_y
and y < cos_selection_indicator_y + cos_selection_indicator_size)
{
gosub cos_hide_selection_indicator
MouseGetPos x, y
Menu QuickSearchMenu, show, %x%, %y%
}
if (x < (cos_selection_indicator_x - cos_selection_indicator_hide_distance)
or x > (cos_selection_indicator_x + cos_selection_indicator_size + cos_selection_indicator_hide_distance)
or y < (cos_selection_indicator_y - cos_selection_indicator_hide_distance)
or y > (cos_selection_indicator_y + cos_selection_indicator_size + cos_selection_indicator_hide_distance))
gosub cos_hide_selection_indicator
return
cos_hide_selection_indicator:
Gui 22:Hide
SetTimer cos_monitor_mouse_cursor_for_selection_indicator, off
Return
QuickSearchGoogle:
quicksearch_lookup("http://www.google.com/search?ie=utf-8&oe=utf-8&hl=en&q=")
return
QuickSearchIMDb:
quicksearch_lookup("http://www.imdb.com/find?s=all&q=")
return
QuickSearchWikipedia:
quicksearch_lookup("http://en.wikipedia.org/wiki/Special:Search?search=")
return
QuickSearchGoogleMaps:
quicksearch_lookup("http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=")
return
QuickSearchYoutube:
quicksearch_lookup("http://www.youtube.com/results?aq=f&search_query=")
return
QuickSearchGoogleImage:
quicksearch_lookup("http://www.google.hu/images?hl=en&um=1&ie=UTF-8&source=og&sa=N&tab=wi&q=")
return
QuickSearchGoogleNews:
quicksearch_lookup("http://www.google.com/search?hl=en&safe=off&prmd=nvlifd&source=lnms&tbs=nws:1&q=")
return
QuickSearchGoogleVideo:
quicksearch_lookup("http://www.google.com/search?tbo=p&tbs=vid%3A1&safe=off&q=")
return
QuickSearchGoogleDefine:
quicksearch_lookup("http://www.google.com/search?ie=utf-8&oe=utf-8&hl=en&q=define:")
return
QuickSearchGoToUrl:
if (substr(clipboard, 1, 4) <> "http")
clipboard := "http://" . clipboard
quicksearch_lookup("")
return
QuickSearchTranslate:
quicksearch_lookup("http://translate.google.com/translate_t?&hl=en&langpair=auto|en&tbb=1&text=")
return
QuickSearchSztaki:
quicksearch_lookup("http://szotar.sztaki.hu/dict_search.php?L=GER%3AHUN%3AGerHunDict&O=HUN&W=")
return
QuickSearchLeo:
quicksearch_lookup("http://dict.leo.org/ende?lp=ende&lang=de&searchLoc=0&cmpType=relaxed&search=")
return
quicksearch_lookup(url)
{
Run, %url%%clipboard%
}
ibc_input_changed:
GuiControlGet ibc_input, 23:, QuickSearchInput
settimer ibc_check_input, %ibc_input_check_delay%
return
ibc_check_input:
if (A_TimeIdlePhysical < ibc_input_check_delay)
return
SetTimer ibc_check_input, off
if (ibc_input == ibc_previous_input)
return
ibc_previous_input := ibc_input
ibc_input_len := strlen(ibc_input)
if (ibc_input_len == 0)
{
gosub ibc_hide_completions
return
}
ibc_matches =
; fixme: encode query
URLDownloadToFile http://en.wikipedia.org/w/api.php?action=opensearch&search=%ibc_input%, %ibc_completion_file%
if (ErrorLevel == 0)
{
fileread ibc_completions, %ibc_completion_file%
; thanks to SKAN for this simple approach of parsing the results
ibc_completions := Deref_Umlauts(RegExReplace(ibc_completions, "\[|\]"))
Loop, Parse, ibc_completions, CSV
{
if (A_Index == 1)
ibc_user_input := A_Loopfield
else
ibc_matches := ibc_matches . "|" . A_Loopfield
}
}
if (ibc_matches <> "|")
{
GuiControl 24:,Completions, %ibc_matches%
Gui 24: Show, x%ibc_completion_gui_x% y%ibc_completion_gui_y%
WinActivate ahk_id %ibc_input_gui_id%
hotkey down, on
hotkey up, on
}
Else
gosub ibc_hide_completions
return
ibc_hide_completions:
Gui 24: Hide
Hotkey down, off
Hotkey up, off
return
ibc_next_completion:
GuiControlGet ibc_current_completion, 24:, Completions
if (ibc_current_completion == "")
ibc_current_completion = 0
ibc_current_completion := ibc_current_completion + 1
GuiControl 24:Choose ,Completions, %ibc_current_completion%
gosub ibc_copy_completion_to_input
return
ibc_previous_completion:
GuiControlGet ibc_current_completion, 24:, Completions
if (ibc_current_completion > 0)
{
ibc_current_completion := ibc_current_completion - 1
if (ibc_current_completion == 0)
{
PostMessage, 0x186, -1, 0,, ahk_id %Ibc_Listbox%
copy_string_to_input(ibc_user_input)
}
else
{
GuiControl 24:Choose ,Completions, %ibc_current_completion%
gosub ibc_copy_completion_to_input
}
}
return
ibc_copy_completion_to_input:
ibc_wanted_completion := ibc_current_completion + 1
Loop, Parse, ibc_matches, |
{
if (A_Index == ibc_wanted_completion)
{
copy_string_to_input(A_Loopfield)
Break
}
}
return
copy_string_to_input(str)
{
global
GuiControl 23:, QuickSearchInput, %str%
ibc_input := str
ibc_previous_input := ibc_input
SendInput {end}
SetTimer ibc_check_input, off
}
ibc_cleanup:
Gosub ibc_hide_completions
Hotkey down, off
Hotkey up, off
return
; code by SKAN
Deref_Umlauts( w, n=1 ) {
stringreplace, w, w, \u0171, % chr("0xfc")
stringreplace, w, w, \u0151, % chr("0xf6")
While n := instr( w, "\u",1,n )
StringReplace, w, w, % ww := substr( w,n,6 ), % chr( "0x" substr( ww,3 ) ), all
Return w
}