-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_arrangement.lua
160 lines (125 loc) · 4.49 KB
/
window_arrangement.lua
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
require 'functions';
--[[
-
- App Arrangement
-
]]
apps = {};
--[[
Define Grid
--]]
hs.grid.MARGINX = 0
hs.grid.MARGINY = 0
hs.grid.GRIDWIDTH = 12
hs.grid.GRIDHEIGHT = 12
-- Pre-defined grid layouts
windowMaximize = { x = 0, y = 0, w = 12, h = 12 };
windowCenter6x6 = { x = 3, y = 3, w = 6, h = 6 };
windowCenter6x8 = { x = 3, y = 2, w = 6, h = 8 };
windowCenter6x10 = { x = 3, y = 1, w = 6, h = 10 };
windowCenter8x8 = { x = 2, y = 2, w = 8, h = 8 };
windowCenter8x10 = { x = 2, y = 1, w = 8, h = 10 };
windowCenter10x10 = { x = 1, y = 1, w = 10, h = 10 };
windowLeft2_12 = { x = 0, y = 0, w = 2, h = 12 };
windowLeft3_12 = { x = 0, y = 0, w = 3, h = 12 };
windowLeft4_12 = { x = 0, y = 0, w = 4, h = 12 };
windowLeft6_12 = { x = 0, y = 0, w = 6, h = 12 };
windowLeft8_12 = { x = 0, y = 0, w = 8, h = 12 };
windowLeft9_12 = { x = 0, y = 0, w = 9, h = 12 };
windowLeft10_12 = { x = 0, y = 0, w = 10, h = 12 };
windowLeft2_10 = { x = 0, y = 1, w = 2, h = 10 };
windowLeft3_10 = { x = 0, y = 1, w = 3, h = 10 };
windowLeft4_10 = { x = 0, y = 1, w = 4, h = 10 };
windowLeft6_10 = { x = 0, y = 1, w = 6, h = 10 };
windowLeft8_10 = { x = 0, y = 1, w = 8, h = 10 };
windowLeft9_10 = { x = 0, y = 1, w = 9, h = 10 };
windowLeft10_10 = { x = 0, y = 1, w = 10, h = 12 };
windowRight2_12 = { x = 10, y = 0, w = 2, h = 12 };
windowRight3_12 = { x = 9, y = 0, w = 3, h = 12 };
windowRight4_12 = { x = 8, y = 0, w = 4, h = 12 };
windowRight6_12 = { x = 6, y = 0, w = 6, h = 12 };
windowRight8_12 = { x = 4, y = 0, w = 8, h = 12 };
windowRight9_12 = { x = 3, y = 0, w = 9, h = 12 };
windowRight10_10 = { x = 2, y = 1, w = 10, h = 10 };
windowRight2_10 = { x = 10, y = 1, w = 2, h = 10 };
windowRight3_10 = { x = 9, y = 1, w = 3, h = 10 };
windowRight4_10 = { x = 8, y = 1, w = 4, h = 10 };
windowRight6_10 = { x = 6, y = 1, w = 6, h = 10 };
windowRight8_10 = { x = 4, y = 1, w = 8, h = 10 };
windowRight9_10 = { x = 3, y = 1, w = 9, h = 10 };
windowRight10_10 = { x = 2, y = 1, w = 10, h = 10 };
-- Pre-defined grid layouts
function arrangeApps(apps, alert)
print('--------');
print('-- Arranging apps to pre-defined locations --');
for i,app in pairs(apps) do
arrangeApp(i, app);
end
if (alert) then
hs.alert('Window arrangement reset');
end;
print('--------');
end
function arrangeApp(id, appConfig)
if appConfig.id == nil then
error('The app definition: ' .. id .. ' is missing an "id" property');
return;
end;
if appConfig.windows == nil then
error('The app definition: ' .. id .. ' is missing a "windows" property');
return;
end;
for i,app in pairs(hs.application.applicationsForBundleID(appConfig.id)) do
arrangeWindows(app, appConfig);
end
end
function arrangeWindows(app, config)
local screenCount = tablelength(hs.screen.allScreens());
for i,window in ipairs(app:allWindows()) do
local windowConfig = findwindow(config.windows, screenCount);
if (windowConfig) then
print(
'Arranging window: ' .. app:name() .. ' - ' .. window:title() ..
' at grid (' .. windowConfig.position.x .. ', ' .. windowConfig.position.y .. ', ' .. windowConfig.position.w .. ', ' .. windowConfig.position.h .. ')' ..
' on screen (x: ' .. windowConfig.screen.x .. ', y: ' .. windowConfig.screen.y .. ')'
);
if (windowConfig.screen) then
hs.grid.set(window, windowConfig.position, windowConfig.screen);
else
hs.grid.set(window, windowConfig.position);
end;
end;
end
end
function findwindow(windows, index)
if (index == 0) then
return nil;
end;
if windows[index] then
return windows[index];
end;
return findwindow(windows, index-1);
end
function addAppConfig(id, position, numScreens, screen)
config = {
position = position
}
if numScreens == nil then
error("numScreens must not be nil")
end;
if screen == nil then
error("screen must not be nil")
end;
-- If screen provided, check if it's a table
if (type(screen) == "table") then
config.screen = screen;
else
screen = screen - 1
config.screen = {x = screen, y = 0};
end;
-- Initialise the app config
if apps[id] == nil then
apps[id] = {id = id, windows = {}};
end;
apps[id].windows[numScreens] = config
end