-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow-manager.lua
executable file
·126 lines (106 loc) · 3.57 KB
/
window-manager.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
-----------------------------------------------------------------------------------
-- File: window-manager.lua
-- Author: Joaquín Cuitiño
-- License: MIT
-----------------------------------------------------------------------------------
local grid = require("grid")
local spaces = hs.spaces
local logger = hs.logger.new("windowManager", "verbose")
hs.application.enableSpotlightForNameSearches(true)
local This = {}
function This.positionApp(appTitle, screen, space, position)
-- spaces.changeToSpace(space, false)
-- This.focusScreen(screen)
-- logger.v("Positioning " .. appTitle)
if (hs.application.get(appTitle) == nil) then
logger.e("Application " .. appTitle .. " not found")
return
end
app = hs.application.get(appTitle)
logger.v("Positioning app "..appTitle)
if (app ~= nil and app:kind() == 1) then
app:hide()
logger.v("Bundle ID: "..app:bundleID())
windows = app:allWindows()
if (#windows == 0) then
logger.v("No windows found for ".. appTitle)
end
for k,v in pairs(windows) do
logger.v("Positioning window "..v:id().. " of app "..appTitle)
spaces.moveWindowToSpace(v:id(), space)
if (position == nil) then
position = grid.max
end
hs.grid.set(v, position, screen)
if (v:isMinimized()) then
logger.v("Unminimizing window "..v:id().. " of app "..appTitle)
v:unminimize()
end
end
app:unhide()
end
end
function This.arrange(apps)
local screens = hs.screen.allScreens()
-- Select all aplications for filter
-- local filter = hs.window.filter.new(false):setCurrentSpace(nil)
-- logger.v(dump(filter:getWindows()))
-- for appName,app in pairs(apps) do
-- -- hs.application.launchOrFocus(appName)
-- filter:allowApp(appName)
-- end
-- Hide all windows (this fixes inconsistent window arrangement)
-- local windows = filter:getWindows()
-- logger.v(dump(windows))
-- for k,v in pairs(windows) do
-- v:application():hide()
-- end
-- Get spaces
-- logger.v("Fetching spaces and screens")
local allSpaces = spaces.allSpaces()
-- logger.v(dump(allSpaces))
-- Find primary and secondary screens UUIDs
local primaryScreenUUID = hs.screen.primaryScreen():getUUID()
logger.v("Primary screen UUID: " .. primaryScreenUUID)
local secondaryScreenUUID = hs.screen.primaryScreen():next():getUUID()
logger.v("Secondary screen UUID: " .. secondaryScreenUUID)
-- Organize all windows
for appName,app in pairs(apps) do
local screenSpaces = nil
if app.screen == 1 then
screenSpaces = allSpaces[primaryScreenUUID]
else
screenSpaces = allSpaces[secondaryScreenUUID]
end
This.positionApp(appName, screens[app.screen], screenSpaces[app.space], app.position)
end
end
function This.focusScreen(screen)
local frame = screen:frame()
local mousePosition = hs.mouse.getAbsolutePosition()
-- if mouse is already on the given screen we can safely return
if hs.geometry(mousePosition):inside(frame) then return false end
-- "hide" cursor in the lower right side of screen
-- it's invisible while we are changing spaces
local newMousePosition = {
x = frame.x + frame.w - 1,
y = frame.y + frame.h - 1
}
hs.mouse.setAbsolutePosition(newMousePosition)
hs.timer.usleep(1000)
return true
end
-- Prints a table.
function dump(o)
if type(o) == "table" then
local s = "{ "
for k,v in pairs(o) do
if type(k) ~= "number" then k = '"'..k..'"' end
s = s .. "["..k.."] = " .. dump(v) .. ","
end
return s .. "} "
else
return tostring(o)
end
end
return This