-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_stashing.lua
95 lines (71 loc) · 2.43 KB
/
window_stashing.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
--[[
-
- Window stashing
-
]]
local stashedWindowPositions = {};
function stashWindowPositions(alert)
print('--------');
print('-- Stashing window locations --');
local screenCount = tablelength(hs.screen.allScreens());
stashedWindowPositions[screenCount] = {};
for i,app in pairs(hs.application.runningApplications()) do
for i,window in ipairs(app:allWindows()) do
local id = window:id();
local config = {
app_id = app:pid(),
screen = window:screen(),
frame = window:frame(),
name = app:name(),
title = window:title(),
};
print(
'Stashing window: ' .. app:bundleID() .. ' - ' .. window:title() ..
' at (' .. config.frame.x .. ', ' .. config.frame.y .. ', ' .. config.frame.w .. ', ' .. config.frame.h .. ')'
);
if (id) then
stashedWindowPositions[screenCount][id] = config;
end
end
end
print('--------');
if (alert) then
hs.alert('Stashed window locations');
end
end
function restoreWindowPositions(alert)
print('--------');
print('-- Restoring window locations --');
local screenCount = tablelength(hs.screen.allScreens());
if (screenCount == 0) then
return;
end;
-- If there is no stashed data, return false
if (stashedWindowPositions[screenCount] == nil) then
print('-- No stashed window location data --');
hs.alert('No stashed window data');
return false;
end;
-- Restore stashed window data
for id,config in pairs(stashedWindowPositions[screenCount]) do
local app = hs.application.get(config.app_id);
local window = hs.window.get(id);
local screen = hs.screen.find(config.screen);
if (window and app) then
window:setFrame(config.frame);
window:moveToScreen(screen);
print(
'Restoring window: ' .. app:name() .. ' - ' .. window:title() ..
' to (' .. config.frame.x .. ', ' .. config.frame.y .. ', ' .. config.frame.w .. ', ' .. config.frame.h .. ')'
);
elseif (app) then
print(
'Unable to find window for: ' .. app:name());
end;
end;
print('--------');
if (alert) then
hs.alert('Restored window locations');
end;
return true;
end