-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheatsheets.lua
213 lines (199 loc) · 7.12 KB
/
cheatsheets.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
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
211
212
213
------------------------------------------------------------------------
--/ Cheatsheet Copycat /--
------------------------------------------------------------------------
local commandEnum = {
[0] = '⌘',
[1] = '⇧ ⌘',
[2] = '⌥ ⌘',
[3] = '⌥ ⇧ ⌘',
[4] = '⌃ ⌘',
[5] = '⇧ ⌃ ⌘',
[6] = '⌃ ⌥ ⌘',
[7] = '',
[8] = '⌦',
[9] = '',
[10] = '⌥',
[11] = '⌥ ⇧',
[12] = '⌃',
[13] = '⌃ ⇧',
[14] = '⌃ ⌥',
}
function getAllMenuItemsTable(t)
local menu = {}
for pos,val in pairs(t) do
if(type(val)=="table") then
if(val['AXRole'] =="AXMenuBarItem" and type(val['AXChildren']) == "table") then
menu[pos] = {}
menu[pos]['AXTitle'] = val['AXTitle']
menu[pos][1] = getAllMenuItems(val['AXChildren'][1])
elseif(val['AXRole'] =="AXMenuItem" and not val['AXChildren']) then
if( val['AXMenuItemCmdModifiers'] ~='0' and val['AXMenuItemCmdChar'] ~='') then
menu[pos] = {}
menu[pos]['AXTitle'] = val['AXTitle']
menu[pos]['AXMenuItemCmdChar'] = val['AXMenuItemCmdChar']
menu[pos]['AXMenuItemCmdModifiers'] = val['AXMenuItemCmdModifiers']
end
elseif(val['AXRole'] =="AXMenuItem" and type(val['AXChildren']) == "table") then
menu[pos] = {}
menu[pos][1] = getAllMenuItems(val['AXChildren'][1])
end
end
end
return menu
end
function getAllMenuItems(t)
local menu = ""
for pos,val in pairs(t) do
if(type(val)=="table") then
-- do not include help menu for now until I find best way to remove menubar items with no shortcuts in them
if(val['AXRole'] =="AXMenuBarItem" and type(val['AXChildren']) == "table") and val['AXTitle'] ~="Help" then
menu = menu.."<ul class='col col"..pos.."'>"
--print("---------------| "..val['AXTitle'].." |---------------")
menu = menu.."<li class='title'><strong>"..val['AXTitle'].."</strong></li>"
menu = menu.. getAllMenuItems(val['AXChildren'][1])
menu = menu.."</ul>"
elseif(val['AXRole'] =="AXMenuItem" and not val['AXChildren']) then
if( val['AXMenuItemCmdModifiers'] ~='0' and val['AXMenuItemCmdChar'] ~='') then
--print(val['AXMenuItemCmdModifiers'].." | "..val['AXTitle'].." | CmdChar: "..val['AXMenuItemCmdChar'])
menu = menu.."<li><div class='cmdModifiers'>"..commandEnum[val['AXMenuItemCmdModifiers']].." "..val['AXMenuItemCmdChar'].."</div><div class='cmdtext'>".." "..val['AXTitle'].."</div></li>"
end
elseif(val['AXRole'] =="AXMenuItem" and type(val['AXChildren']) == "table") then
menu = menu..getAllMenuItems(val['AXChildren'][1])
end
end
end
return menu
end
function generateHtml()
--local focusedApp= hs.window.frontmostWindow():application()
local focusedApp = hs.application.frontmostApplication()
local appTitle = focusedApp:title()
local allMenuItems = focusedApp:getMenuItems();
local myMenuItems = getAllMenuItems(allMenuItems)
local html = [[
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
*{margin:0; padding:0;}
html, body{
background-color:#eee;
font-family: arial;
font-size: 13px;
}
a{
text-decoration:none;
color:#000;
font-size:12px;
}
li.title{ text-align:center;}
ul, li{list-style: inside none; padding: 0 0 5px;}
footer{
position: fixed;
left: 0;
right: 0;
height: 48px;
background-color:#eee;
}
header{
position: fixed;
top: 0;
left: 0;
right: 0;
height:48px;
background-color:#eee;
z-index:99;
}
footer{ bottom: 0; }
header hr,
footer hr {
border: 0;
height: 0;
border-top: 1px solid rgba(0, 0, 0, 0.1);
border-bottom: 1px solid rgba(255, 255, 255, 0.3);
}
.title{
padding: 15px;
}
li.title{padding: 0 10px 15px}
.content{
padding: 0 0 15px;
font-size:12px;
overflow:hidden;
}
.content.maincontent{
position: relative;
height: 577px;
margin-top: 46px;
}
.content > .col{
width: 23%;
padding:10px 0 20px 20px;
}
li:after{
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.cmdModifiers{
width: 60px;
padding-right: 15px;
text-align: right;
float: left;
font-weight: bold;
}
.cmdtext{
float: left;
overflow: hidden;
width: 165px;
}
</style>
</head>
<body>
<header>
<div class="title"><strong>]]..appTitle..[[</strong></div>
<hr />
</header>
<div class="content maincontent">]]..myMenuItems..[[</div>
<footer>
<hr />
<div class="content" >
<div class="col">
by <a href="https://github.com/dharmapoudel" target="_parent">dharma poudel</a>
</div>
</div>
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.isotope/2.2.2/isotope.pkgd.min.js"></script>
<script type="text/javascript">
var elem = document.querySelector('.content');
var iso = new Isotope( elem, {
// options
itemSelector: '.col',
layoutMode: 'masonry'
});
</script>
</body>
</html>
]]
return html
end
local myView = nil
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "C", function()
if not myView then
myView = hs.webview.new({x = 100, y = 100, w = 1080, h = 600}, { developerExtrasEnabled = true })
:windowStyle("utility")
:closeOnEscape(true)
:html(generateHtml())
:allowGestures(true)
:windowTitle("CheatSheets")
:show()
--myView:asHSWindow():focus()
--myView:asHSDrawing():setAlpha(.98):bringToFront()
else
myView:delete()
myView=nil
end
end)