Guest User

Untitled

a guest
Apr 21st, 2015
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. ;DockWin v0.3 - Save and Restore window positions when docking/undocking (using hotkeys)
  2. ; Paul Troiano, 6/2014
  3. ;
  4. ; Hotkeys: ^ = Control; ! = Alt; + = Shift; # = Windows key; * = Wildcard;
  5. ; & = Combo keys; Others include ~, $, UP (see "Hotkeys" in Help)
  6.  
  7. ;#InstallKeybdHook
  8. #SingleInstance, Force
  9. SetTitleMatchMode, 2 ; 2: A window's title can contain WinTitle anywhere inside it to be a match.
  10. SetTitleMatchMode, Fast ;Fast is default
  11. DetectHiddenWindows, off ;Off is default
  12. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
  13. CrLf=`r`n
  14. FileName:="WinPos.txt"
  15.  
  16.  
  17.  
  18. ;Win-0 (Restore window positions from file)
  19. #0::
  20. WinGetActiveTitle, SavedActiveWindow
  21. ParmVals:="Title x y height width"
  22. SectionToFind:= SectionHeader()
  23. SectionFound:= 0
  24.  
  25. Loop, Read, %FileName%
  26. {
  27. if !SectionFound
  28. {
  29. ;Read through file until correction 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. Win_Title:="", Win_x:=0, Win_y:=0, Win_width:=0, Win_height:=0
  40.  
  41. Loop, Parse, A_LoopReadLine, CSV
  42. {
  43. EqualPos:=InStr(A_LoopField,"=")
  44. Var:=SubStr(A_LoopField,1,EqualPos-1)
  45. Val:=SubStr(A_LoopField,EqualPos+1)
  46. IfInString, ParmVals, %Var%
  47. {
  48. ;Remove any surrounding double quotes (")
  49. If (SubStr(Val,1,1)=Chr(34))
  50. {
  51. StringMid, Val, Val, 2, StrLen(Val)-2
  52. }
  53. Win_%Var%:=Val
  54. }
  55. }
  56.  
  57. If ( (StrLen(Win_Title) > 0) and WinExist(Win_Title) )
  58. {
  59. WinRestore
  60. WinActivate
  61. WinMove, A,,%Win_x%,%Win_y%,%Win_width%,%Win_height%
  62. }
  63.  
  64. }
  65.  
  66. if !SectionFound
  67. {
  68. 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)
  69. }
  70.  
  71. ;Restore window that was active at beginning of script
  72. WinActivate, %SavedActiveWindow%
  73. RETURN
  74.  
  75.  
  76. ;Win-Shift-0 (Save current windows to file)
  77. #+0::
  78.  
  79. MsgBox, 4,Dock Windows,Save window positions?
  80. IfMsgBox, NO, Return
  81.  
  82. WinGetActiveTitle, SavedActiveWindow
  83.  
  84. file := FileOpen(FileName, "a")
  85. if !IsObject(file)
  86. {
  87. MsgBox, Can't open "%FileName%" for writing.
  88. Return
  89. }
  90.  
  91. line:= SectionHeader() . CrLf
  92. file.Write(line)
  93.  
  94. ; Loop through all windows on the entire system
  95. WinGet, id, list,,, Program Manager
  96. Loop, %id%
  97. {
  98. this_id := id%A_Index%
  99. WinActivate, ahk_id %this_id%
  100. WinGetPos, x, y, Width, Height, A ;Wintitle
  101. WinGetClass, this_class, ahk_id %this_id%
  102. WinGetTitle, this_title, ahk_id %this_id%
  103.  
  104. if ( (StrLen(this_title)>0) and (this_title<>"Start") )
  105. {
  106. line=Title="%this_title%"`,x=%x%`,y=%y%`,width=%width%`,height=%height%`r`n
  107. file.Write(line)
  108. }
  109. }
  110.  
  111. file.write(CrLf) ;Add blank line after section
  112. file.Close()
  113.  
  114. ;Restore active window
  115. WinActivate, %SavedActiveWindow%
  116. RETURN
  117.  
  118. ; -------
  119.  
  120. ;Create standardized section header for later retrieval
  121. SectionHeader()
  122. {
  123. SysGet, MonitorCount, MonitorCount
  124. SysGet, MonitorPrimary, MonitorPrimary
  125. line=SECTION: Monitors=%MonitorCount%,MonitorPrimary=%MonitorPrimary%
  126.  
  127. WinGetPos, x, y, Width, Height, Program Manager
  128. line:= line . "; Desktop size:" . x . "," . y . "," . width . "," . height
  129.  
  130. Return %line%
  131. }
  132.  
  133. ;<EOF>
Advertisement
Add Comment
Please, Sign In to add comment