Advertisement
Primer81

snapWindowUpDown.ahk

Nov 1st, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
  2. ; #Warn  ; Enable warnings to assist with detecting common errors.
  3. SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
  4. SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
  5.  
  6. ; sorts a given array
  7. sort(array) {  ; Function initialization
  8.     sorted := false  ; Variable used to decide if the array is alphabetized yet:  false, because it isn't
  9.  
  10.     While !sorted  ; Beginning of loop
  11.     {
  12.         swapped := false  ; Variable that decides if any swapping occurred: false, because it hasn't yet
  13.         Loop, % array.MaxIndex() {  ; Loops through for the max amount of array items
  14.             i := A_Index ; This is just for convenience, so I don't have to type A_Index multiple times
  15.             j := i + 1
  16.             if (i < array.MaxIndex()) {  ; Obviously only checking if A_Index is less than number of items in arrayD
  17.                 if (array[i] > array[j]) {  ; AHK allows < and > for alphabetical strings
  18.                     tempVal := array[j]  ; Holds onto the array value that we'll be moving
  19.                     array.RemoveAt(j)  ; Deletes that value from the array
  20.                     array.InsertAt(i, tempVal)  ; Inserts it into the previous position
  21.                     swapped := true  ; Set to true, because a swap happened
  22.                 }
  23.             }
  24.         }
  25.         if !swapped  ; If no swaps happened...
  26.             sorted := true  ; then we can exit the loop, otherwise, it repeats the process
  27.     }
  28.     return array  ; returns the sorted array
  29. }
  30.  
  31. ; finds the monitor number that the selected window is on (NOTE: this only works if your
  32. ; monitors are arranged horizontally. You'll have to make some changes if you want it
  33. ; to work for vertically arranged monitors, and even more so to work for monitors arranged
  34. ; vertically and horizontally)
  35. getMonitorNumber() {
  36.     ;80 is the code for the SM_CMONITORS sub-command. Retrieves # of display monitors
  37.     SysGet, numMon, 80
  38.  
  39.     ; init MonLeftArray to store all X coords of monitors. Indexes correspond to monitor number
  40.     MonLeftArray := Object()
  41.     MonLeftArray.SetCapacity(numMon)
  42.  
  43.     ; populate MonLeftArray
  44.     Loop, %numMon% {
  45.         i := A_Index
  46.         SysGet, mon, Monitor, %i%
  47.         MonLeftArray[i] := monLeft
  48.     }
  49.  
  50.     ; init SortedMonLeftArray to store an ordered array of X coords
  51.     SortedMonLeftArray := Object()
  52.     SortedMonLeftArray.SetCapacity(numMon)
  53.  
  54.     ; populate SortedMonLeftArray
  55.     Loop, %numMon% {
  56.         i := A_Index
  57.         SysGet, mon, Monitor, %i%
  58.         SortedMonLeftArray[i] := monLeft
  59.     }
  60.  
  61.     ; sort SortedMonLeftArray
  62.     SortedMonLeftArray := sort(SortedMonLeftArray)
  63.  
  64.     ; get the data of selected window
  65.     WinGetPos,X,Y,W,H,A,,,
  66.  
  67.     ; find where the selected window is
  68.     Loop, %numMon% {
  69.         i := A_Index
  70.         ; is %i% the first index? and is X before this index?
  71.         if (i = 1 and X < SortedMonLeftArray[1]) {
  72.             currMonLeft := SortedMonLeftArray[i]
  73.             break
  74.         }
  75.         ; is %i% the last index?
  76.         if (i + 1 > numMon) {
  77.             currMonLeft := SortedMonLeftArray[i]
  78.         }
  79.         ; %i% is not the last index. There is a next monitor.
  80.         else {
  81.             j := i + 1
  82.             ; there are eight pixels of padding in windows for some reason
  83.             if (SortedMonLeftArray[i] <= X + 8 and X + 8 < SortedMonLeftArray[j]) {
  84.                 currMonLeft := SortedMonLeftArray[i]
  85.                 break
  86.             }
  87.         }
  88.     }
  89.  
  90.     ; find which monitor corresponds to currMonLeft
  91.     Loop, %numMon% {
  92.         i := A_Index
  93.         if (MonLeftArray[i] = currMonLeft) {
  94.             return i
  95.         }
  96.     }
  97. }
  98.  
  99. ; Move window up (Windows + Shift + UP)
  100. +#Up::
  101.  
  102.     monNumber := getMonitorNumber()
  103.  
  104.     ; get the work area of active monitor
  105.     SysGet, mon, MonitorWorkArea, %monNumber%
  106.  
  107.     ; move window
  108.     WinMove,A,,monLeft, 0, (monRight - monLeft), (monBottom/2)
  109.  
  110. return
  111.  
  112. ; Move window down (Windows + Shift + DOWN)
  113. +#Down::
  114.  
  115.     monNumber := getMonitorNumber()
  116.  
  117.     ; get the work area of active monitor
  118.     SysGet, mon, MonitorWorkArea, %monNumber%
  119.  
  120.     ; move window
  121.     WinMove,A,,monLeft, monBottom/2 , (monRight - monLeft), (monBottom/2)
  122.  
  123. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement