Skip to content

Instantly share code, notes, and snippets.

@dcai
Created February 28, 2017 02:46
Show Gist options
  • Save dcai/ec6f5a9a6d066fcf3c8f0e61f8d01e4f to your computer and use it in GitHub Desktop.
Save dcai/ec6f5a9a6d066fcf3c8f0e61f8d01e4f to your computer and use it in GitHub Desktop.
autohotkey grid
; https://autohotkey.com/board/topic/72918-my-ahk-stuff/
;
; AutoHotkey Version: 1.x
; Language: English
; Platform: windows
; Author: Andre Blanchard
;
;Windows Slide
; Script Function:
; This script allows you to move around windows by pressing the hotkey of your choice.
; Imagine each window is divided into a grid of 9 cells, pressing the hotkey will
; activate the action of the cell your mouse is currently over:
;
;-------------------------------------------------
;| | | |
;| 1 | 2 | 3 |
;| | | |
;| | | |
;------------------------------------------------- Hline1
;| | | |
;| 4 | 5 | 6 |
;| | | |
;| | | |
;------------------------------------------------- Hline2
;| | | |
;| 7 | 8 | 9 |
;| | | |
;| | | |
;-------------------------------------------------
; Vline1 Vline2
;
;Hline1, Hline2, Vline1, Vline2, all are adjustable paramaters
;
;CELL FUNCTIONS:
;1:resize window to top left corner of screen
;2:maximize window
;3:resize window to top right corner of screen
;4:resize window to left half of screen
;5:minimize window
;6:resize window to right half of screen
;7:resize window to bottom left corner of screen
;8:close window
;9:resize window to bottom right corner of screen
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; specify the hotkey of your choice here
HK = #f
;see preamble. adjust these to your liking (in percent of screen width)
Vline1 = 0.33
Vline2 = 0.67
Hline1 = 0.33
Hline2 = 0.67
;Delay for SetWinDelay
delay := 0
;get screen resolution and toolbar width
WinGetPos,,, RX, RY,Program Manager
WinGetPos,,,, T, ahk_class Shell_TrayWnd
;specify the hotkey
Hotkey,%HK%,script
script:
;get position and id of window mouse is over
MouseGetPos,X,Y,Win_ID,,
WinActivate,ahk_id %Win_ID%,,,
WinGetPos,,,W,H,ahk_id %Win_ID%,,,
;Here I figure out which cell the mouse is in and call the Act() function to perform action
;top left cell
if (X>0 and Y>0 and X<Vline1*W and Y<Hline1*H){
Act("tl")
}
;top mid cell
else if (X>Vline1*W and Y>0 and X<Vline2*W and Y<Hline1*H) {
act("max")
}
;top right cell
else if (X>Vline2*W and Y>0 and X<W and Y<Hline1*H) {
act("tr")
}
;mid left cell
else if (X>0 and Y>Hline1*H and X<Vline1*W and Y<Hline2*H) {
act("l")
}
;mid cell
else if (X>Vline1*W and Y>Hline1*H and X<Vline2*W and Y<Hline2*H) {
act("min")
}
;mid right cell
else if (X>Vline1*W and Y>Hline1*H and X<W and Y<Hline2*H) {
act("r")
}
;bottom left cell
else if (X>0 and Y>Hline2*H and X<Vline1*W and Y<H) {
act("bl")
}
;bottom mid cell
else if (X>Vline1*W and Y>Hline2*H and X<Vline2*W and Y<H) {
act("close")
}
;bottom right cell
else if(X>Vline2*W and Y>Hline2*H and X<W and Y<H) {
act("br")
}
return
;Here is the Act() function. its job is to essentially pass the appropriate
;parameters to the WinSlide() function
Act(loc)
{
global win_id, Rx, Ry, T
WinTitle = ahk_id %win_id%
if (loc = "tl") {
WinRestore, ahk_id %win_id%,,,
WinSlide(WinTitle,0,0,RX/2,(RY-T)/2)
}
else if (loc = "max") {
WinSlide(WinTitle,0,0,RX,RY-T)
WinMaximize,ahk_id %win_id%,,,
}
else if (loc = "tr") {
WinRestore, ahk_id %win_id%,,,
WinSlide(WinTitle,Rx/2,0,RX/2,(RY-T)/2)
}
else if (loc = "l") {
WinRestore, ahk_id %win_id%,,,
WinSlide(WinTitle,0,0,RX/2,(RY-T))
WinMove,ahk_id %Win_ID%,,0,0,RX/2,(RY-T),,
}
else if (loc = "min") {
WinMinimize,ahk_id %win_id%,,,
}
else if (loc = "r") {
WinRestore, ahk_id %win_id%,,,
WinSlide(WinTitle,RX/2,0,RX/2,(RY-T))
}
else if (loc = "bl") {
WinRestore, ahk_id %win_id%,,,
WinSlide(WinTitle,0,(RY-T)/2,RX/2,(RY-T)/2)
}
else if (loc = "close") {
WinClose, ahk_id %win_id%,,,,
}
else if (loc = "br") {
WinRestore, ahk_id %win_id%,,,
WinSlide(WinTitle,RX/2,(RY-T)/2,RX/2,(RY-T)/2)
}
return
}
;Here is the windslide function. It takes in the initial coordinates of the
;window specified by WinTitle, takes in the final desired coordinates and
;slides the window to the correct spot.
WinSlide(WinTitle,XF,YF,WF,HF)
{
SetWinDelay,delay
;the tolerance values decide how close the window has to be to the final
;coordinates for the loop to end
tolerance1 = 20
tolerance2 = 20
;get current window position
WinGetPos,XI,YI,WI,HI,%WinTitle%,,,
X := XI
Y := YI
W := WI
H := HI
;compute difference vectors between initial and final coordinates
DX := XF-XI
DY := YF-YI
DW := WF-WI
DH := HF-HI
;velocity controls how fast the window moves. It depends mainly on the change
; in width and height because this slows down the movement so i needed to
;compensate
velocity:=0.2*(magnitude(DW,DH)/200 + 0.1)
;this is the sliding loop that actually moves the window
loop
{
;when we get close enough break
if (abs(X-XF)<=tolerance1 AND abs(W-WF)<=tolerance2 AND abs(Y-YF)<=tolerance1 AND abs(H-HF)<=tolerance2) {
break
}
;putting the difference vector calculation in the loop does two things:
;1) is makes the movement exponential so the farther away the window is from the
;target destination the faster the window moves.
;2) this has the bonus that the movement avoids overshooting the destination
;vector because the closer the window is to its destination the smaller the step
;size taken. In this way overshooting is avoided.
DX := XF-X
DY := YF-Y
DW := WF-W
DH := HF-H
;Here we compute the new position of the window after it has been incremented
;in the right direction.
X := X + DX*velocity
Y := Y + DY*velocity
W := W + DW*velocity
H := H + DH*velocity
;actually move the window
WinMove,%WinTitle%,, X, Y, W, H,
}
;when the loop is done set the window at the exact spot it should be.
WinMove,%WinTitle%,, XF, YF, WF, HF,
return
}
;computes the the magnitude of the vecotr (X,Y)
magnitude(X,Y)
{
M := sqrt(X*X+Y*Y)
return M
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment