Advertisement
Guest User

DockWin v0.4c - Save and Restore window positions when docki

a guest
Dec 18th, 2019
990
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;DockWin v0.4c - Save and Restore window positions when docking/undocking (using hotkeys) https://autohotkey.com/board/topic/112113-dockwin-storerecall-window-positions/
  2. ; Paul Troiano, 6/2014
  3. ; Updated by Ashley Dawson 7/2015
  4. ; Updated by WZ 12/2017
  5. ;
  6. ; Hotkeys: ^ = Control; ! = Alt; + = Shift; # = Windows key; * = Wildcard;
  7. ;          & = Combo keys; Others include ~, $, UP (see "Hotkeys" in Help)
  8.  
  9. ;#InstallKeybdHook
  10. #SingleInstance, Force
  11. SetTitleMatchMode, 2            ; 2: A window's title can contain WinTitle anywhere inside it to be a match.
  12. SetTitleMatchMode, Fast         ; Fast is default
  13. DetectHiddenWindows, off        ; Off is default
  14. SetWorkingDir %A_ScriptDir%     ; Ensures a consistent starting directory.
  15. CrLf=`r`n
  16. FileName:="WinPos.txt"
  17.  
  18. ; Win-0 (Restore window positions from file)
  19. #0::
  20.   WinGetActiveTitle, SavedActiveWindow
  21.   ParmVals:="Title x y height width maximized path"
  22.   SectionToFind:= SectionHeader()
  23.   SectionFound:= 0
  24.  
  25.   Loop, Read, %FileName%
  26.   {
  27.     if !SectionFound
  28.     {
  29.       ; Read through file until correct section found
  30.       If (A_LoopReadLine<>SectionToFind)
  31.         Continue
  32.     }
  33.  
  34.         ; Exit if another section reached
  35.         If ( SectionFound and SubStr(A_LoopReadLine,1,8)="SECTION:")
  36.             Break
  37.  
  38.         SectionFound:=1
  39.        
  40.         Win_Title:="", Win_x:=0, Win_y:=0, Win_width:=0, Win_height:=0, Win_maximized:=0, Win_id:=0
  41.  
  42.         Loop, Parse, A_LoopReadLine, CSV
  43.         {
  44.             EqualPos:=InStr(A_LoopField,"=")
  45.             Var:=SubStr(A_LoopField,1,EqualPos-1)
  46.             Val:=SubStr(A_LoopField,EqualPos+1)
  47.             IfInString, ParmVals, %Var%
  48.             {
  49.                 ;Remove any surrounding double quotes (")
  50.                 If (SubStr(Val,1,1)=Chr(34))
  51.                 {
  52.                     StringMid, Val, Val, 2, StrLen(Val)-2
  53.                 }
  54.                 Win_%Var%:=Val  
  55.             }
  56.         }
  57.        
  58.         ; Check if program is already running, if not, start it
  59.         If  (!WinExist(Win_Title) and (Win_path<>""))
  60.         {
  61.             Try
  62.             {
  63.                 Run %Win_path% 
  64.                 sleep 1000      ; Give some time for the program to launch.
  65.             }
  66.         }
  67.        
  68.         ; if the window name have changed (like another tab activated in Chrome) fall back to id
  69.         If (!WinExist(Win_Title) and (Win_id<>0))
  70.         {
  71.             WinGetTitle, Win_Title, ahk_id %Win_id%
  72.         }
  73.        
  74.         If ( (Win_maximized = 1) and WinExist(Win_Title) )
  75.         {  
  76.             WinRestore
  77.             WinActivate
  78.             WinMove, A,,%Win_x%,%Win_y%,%Win_width%,%Win_height%
  79.             WinMaximize, A
  80.         } Else If ((Win_maximized = -1) and (StrLen(Win_Title) > 0) and WinExist(Win_Title) )       ; Value of -1 means Window is minimised
  81.         {  
  82.             WinRestore
  83.             WinActivate
  84.             WinMove, A,,%Win_x%,%Win_y%,%Win_width%,%Win_height%
  85.             WinMinimize, A
  86.         } Else If ( (StrLen(Win_Title) > 0) and WinExist(Win_Title) )
  87.         {  
  88.             WinRestore
  89.             WinActivate
  90.             WinMove, A,,%Win_x%,%Win_y%,%Win_width%,%Win_height%
  91.         }
  92.   }
  93.  
  94.   if !SectionFound
  95.   {
  96.     msgbox,,Dock Windows, Section does not exist in %FileName% `nLooking for: %SectionToFind%`n`nTo save a new section, use Win-Shift-0 (zero key above letter P on keyboard)
  97.   }
  98.  
  99.   ; Restore window that was active at beginning of script
  100.   WinActivate, %SavedActiveWindow%
  101. RETURN
  102.  
  103.  
  104. ; Win-Shift-0 (Save current windows to file)
  105. #+0::
  106.  
  107.  MsgBox, 4,Dock Windows,Save window positions?
  108.  IfMsgBox, NO, Return
  109.  
  110.  WinGetActiveTitle, SavedActiveWindow
  111.  
  112.  file := FileOpen(FileName, "w")
  113.  if !IsObject(file)
  114.  {
  115.     MsgBox, Can't open "%FileName%" for writing.
  116.     Return
  117.  }
  118.  
  119.   line:= SectionHeader() . CrLf
  120.   file.Write(line)
  121.  
  122.   ; Loop through all windows on the entire system
  123.   WinGet, id, list,,, Program Manager
  124.   Loop, %id%
  125.   {
  126.     this_id := id%A_Index%
  127.    
  128.     ; as WinActivate restores the window, we first need to know if it was minimised
  129.     WinGetTitle, this_title, ahk_id %this_id%
  130.     WinGetClass, this_class, ahk_id %this_id%
  131.     WinGet, win_maximized, minmax, %this_title%
  132.     WinActivate, ahk_id %this_id%
  133.     WinGetPos, x, y, Width, Height, A
  134.    
  135.     if(win_maximized = -1)      ; Re-minimize any windows that were minimised before we started.
  136.     {
  137.         WinMinimize, A
  138.     }
  139.    
  140.     if ( (StrLen(this_title)>0) and (this_title<>"Start") )
  141.     {
  142.         line=Title="%this_title%"`,x=%x%`,y=%y%`,width=%width%`,height=%height%`,maximized=%win_maximized%`,path="",id=%this_id%`r`n
  143.         file.Write(line)
  144.     }
  145.   }
  146.  
  147.   file.write(CrLf)  ; Add blank line after section
  148.   file.Close()
  149.  
  150.   ; Restore active window
  151.   WinActivate, %SavedActiveWindow%
  152. RETURN
  153.  
  154. ; -------
  155.  
  156. ; Create standardized section header for later retrieval
  157. SectionHeader()
  158. {
  159.     SysGet, MonitorCount, MonitorCount
  160.     SysGet, MonitorPrimary, MonitorPrimary
  161.     line=SECTION: Monitors=%MonitorCount%,MonitorPrimary=%MonitorPrimary%
  162.  
  163.         WinGetPos, x, y, Width, Height, Program Manager
  164.     line:= line . "; Desktop size:" . x . "," . y . "," . width . "," . height
  165.  
  166.     Return %line%
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement