This repository has been archived by the owner on Feb 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
DockWin.ahk
210 lines (171 loc) · 5.53 KB
/
DockWin.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
;DockWin v0.7 - Save and Restore window positions when docking/undocking (using hotkeys)
; Paul Troiano, 6/2014
; Updated by Ashley Dawson 7/2015
; Updated by Carlo Costanzo 11/2016
; Updated by Rene Weselowski 7/2017,9/2017
;
; To use comandline switches compile as exe and use:
; /restore - restore Window-Configuration on start
;
; Hotkeys: ^ = Control; ! = Alt; + = Shift; # = Windows key; * = Wildcard;
; & = Combo keys; Others include ~, $, UP (see "Hotkeys" in Help)
;#InstallKeybdHook
#SingleInstance, Force
SetTitleMatchMode, 2 ; 2: A window's title can contain WinTitle anywhere inside it to be a match.
SetTitleMatchMode, Fast ;Fast is default
DetectHiddenWindows, off ;Off is default
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
CrLf=`r`n
FileName:="WinPos.txt"
FileInstall, DockWin.ico, %A_ScriptDir%\DockWin.ico
Menu, Tray, Icon, %A_ScriptDir%\DockWin.ico,, 1
WinTitle = DockWin v0.7
Menu, Tray, Icon
Menu, Tray, Tip, %WinTitle%:`nCapture and Restore Screens ; `n is a line break.
Menu, Tray, NoStandard
Menu, Tray, Add, %WinTitle%, mDoNothing
Menu, Tray, Default, %WinTitle%
Menu, Tray, Disable, %WinTitle%
Menu, Tray, Add ; time for a nice separator
Menu, Tray, Add, Edit WinPos.txt, mEdit
Menu, Tray, Add, Capture Screens - Shift+Win+0, mCapture
Menu, Tray, Add, Restore Screens - Win+0, mRestore
Menu, Tray, Add ; time for a nice separator
Menu, Tray, Add, Exit %WinTitle%, mExit
Loop, %0% { ;for each command line parameter
If (%A_Index% = "/restore") ;check if known command line parameter exists
Goto, mRestore
}
Return
; =======
mEdit:
; =======
Run, Notepad %A_WorkingDir%\%FileName%, %A_WorkingDir%, UseErrorLevel
Return ; failsafe / probably never hits this line
; ====
mExit:
; ====
ExitApp, 0
;Win-0 (Restore window positions from file)
#0::
mRestore:
WinGetActiveTitle, SavedActiveWindow
ParmVals:="Title x y height width maximized path"
SectionToFind:= SectionHeader()
SectionFound:= 0
Loop, Read, %FileName%
{
if !SectionFound
{
;Read through file until correct section found
If (A_LoopReadLine<>SectionToFind)
Continue
}
;Exit if another section reached
If ( SectionFound and SubStr(A_LoopReadLine,1,8)="SECTION:")
Break
SectionFound:=1
Win_Title:="", Win_x:=0, Win_y:=0, Win_width:=0, Win_height:=0, Win_maximized:=0
Loop, Parse, A_LoopReadLine, CSV
{
EqualPos:=InStr(A_LoopField,"=")
Var:=SubStr(A_LoopField,1,EqualPos-1)
Val:=SubStr(A_LoopField,EqualPos+1)
IfInString, ParmVals, %Var%
{
;Remove any surrounding double quotes (")
If (SubStr(Val,1,1)=Chr(34))
{
StringMid, Val, Val, 2, StrLen(Val)-2
}
Win_%Var%:=Val
}
}
;Check if program is already running, if not, start it
If (!WinExist(Win_Title) and (Win_path<>""))
{
Try
{
Run %Win_path%
sleep 1000 ;Give some time for the program to launch.
}
}
If ( (Win_maximized = 1) and WinExist(Win_Title) )
{
WinRestore
WinActivate
WinMove, A,,%Win_x%,%Win_y%,%Win_width%,%Win_height%
WinMaximize, A
} Else If ((Win_maximized = -1) and (StrLen(Win_Title) > 0) and WinExist(Win_Title) ) ; Value of -1 means Window is minimised
{
WinRestore
WinActivate
WinMove, A,,%Win_x%,%Win_y%,%Win_width%,%Win_height%
WinMinimize, A
} Else If ( (StrLen(Win_Title) > 0) and WinExist(Win_Title) )
{
WinRestore
WinActivate
WinMove, A,,%Win_x%,%Win_y%,%Win_width%,%Win_height%
}
}
if !SectionFound
{
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)
}
;Restore window that was active at beginning of script
WinActivate, %SavedActiveWindow%
RETURN
;Win-Shift-0 (Save current windows to file)
#+0::
mCapture:
MsgBox, 4,Dock Windows,Save window positions? (it will append to the file!)
IfMsgBox, NO, Return
WinGetActiveTitle, SavedActiveWindow
file := FileOpen(FileName, "a")
if !IsObject(file)
{
MsgBox, Can't open "%FileName%" for writing.
Return
}
line:= SectionHeader() . CrLf
file.Write(line)
; Loop through all windows on the entire system
WinGet, id, list,,, Program Manager
Loop, %id%
{
this_id := id%A_Index%
WinGetClass, this_class, ahk_id %this_id%
WinGetTitle, this_title, ahk_id %this_id%
WinGet, win_maximized, minmax, ahk_class %this_class%
WinActivate, ahk_id %this_id%
WinGetPos, x, y, Width, Height, A ;Wintitle
if ( (StrLen(this_title)>0) and (this_title<>"Start") )
{
line=Title="%this_title%"`,x=%x%`,y=%y%`,width=%width%`,height=%height%`,maximized=%win_maximized%,path=""`r`n
file.Write(line)
}
if(win_maximized = -1) ;Re-minimize any windows that were minimised before we started.
{
WinMinimize, A
}
}
file.write(CrLf) ;Add blank line after section
file.Close()
;Restore active window
WinActivate, %SavedActiveWindow%
RETURN
; -------
;Create standardized section header for later retrieval
SectionHeader()
{
SysGet, MonitorCount, MonitorCount
SysGet, MonitorPrimary, MonitorPrimary
line=SECTION: Monitors=%MonitorCount%,MonitorPrimary=%MonitorPrimary%
WinGetPos, x, y, Width, Height, Program Manager
line:= line . "; Desktop size:" . x . "," . y . "," . width . "," . height
Return %line%
}
; =====
mDoNothing: ; for labels.
; =====