-
Notifications
You must be signed in to change notification settings - Fork 0
/
MenuBarChooser.lua
161 lines (132 loc) · 5.04 KB
/
MenuBarChooser.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
local axuielement = require("hs.axuielement")
local function printChildren(application, children, recursive)
for _, child in ipairs(children) do
debugElement(child,"child")
print(application:name())
print(child.AXRoleDescription)
debugMenuItem(application, child, "child")
local role = child:attributeValue("AXRole")
local subrole = child:attributeValue("AXSubrole")
if role == "AXMenuBarItem" and subrole == "AXMenuExtra" then
print("Found NSAccessibilityMenuExtrasMenuBar for application:", application:name())
--return child
end
if recursive then
local childs = child:attributeValue("AXChildren")
debugTable(childs)
if childs then
printChildren(application, childs)
end
end
end
end
local function findMenuExtrasMenuBarForApplication(application)
local pid = application:pid()
local appElement = axuielement.applicationElementForPID(pid)
--debugElement(appElement,"appElement")
if appElement then
-- Get the menu bar element
local menuBar = appElement:attributeValue("AXExtrasMenuBar")
--debugElement(menuBar,"menuBar")
if menuBar then
local children = menuBar:attributeValue("AXChildren")
--debugTable(children, "children")
if children then
--printChildren(application, children)
end
return children
end
end
return nil
end
local function printMenuItems(menuItems)
-- Iterate over bigTable and run the method for each item
for _, menu in ipairs(menuItems) do
for _, item in ipairs(menu.children) do
debugMenuItem(menu.app, item)
end
end
end
local function getMenuItemsForAllApps()
local runningApplications = hs.application.runningApplications()
local allMenuItems = {}
for _, app in ipairs(runningApplications) do
local children = findMenuExtrasMenuBarForApplication(app)
if children then
table.insert(allMenuItems, { app = app, children = children })
end
end
--debugTable(allMenuItems)
--printMenuItems(allMenuItems)
return allMenuItems
end
local FuzzyMatcher = require("Helpers.FuzzyMatcher")
local function getSelection(appItems)
local selection = {}
local menuItemsCounter = 0
local menuItems = {}
local function addMenuItem(value)
menuItemsCounter = menuItemsCounter + 1
local key = tostring(menuItemsCounter)
menuItems[key] = value
return key
end
for _, menu in ipairs(appItems) do
for _, item in ipairs(menu.children) do
local names = item:actionNames()
if names then
for i, name in ipairs(names) do
if name == 'AXPress' then
local key = addMenuItem(item)
table.insert(selection,
{
--text = hs.styledtext.new(p(menu.app:name())..":"..p(item.AXTitle) .. p(item.AXValue) .. p(item.AXDescription).. p(item.AXHelp)),
text = p(menu.app:name())..":"..p(item.AXTitle) .. p(item.AXValue) .. p(item.AXDescription).. p(item.AXHelp),
subText = p(item:actionDescription(name)),
image = hs.image.imageFromAppBundle(menu.app:bundleID()),
element = {
key = key,
action = name
}
}
)
end
end
end
end
end
return { chooserItems = selection, menuItems = menuItems }
end
function MenuBarChooser()
local appItems = getMenuItemsForAllApps()
local selectionMap = getSelection(appItems)
local chooserCallback = function(selection)
if selection then
local menuItem = selectionMap.menuItems[selection.element.key]
menuItem:doAXPress()
end
end
--for i = 1,#keys do
-- table.insert(help, {
-- --text = keys[i].msg,
-- text = hs.styledtext.new(keys[i].msg),
-- --subText = hs.styledtext.new('a little text')
-- subText = 'a little text'
-- })
--end
local choices = selectionMap.chooserItems
chooser = hs.chooser.new(chooserCallback)
--chooser:choices(choices)
FuzzyMatcher.setChoices(choices, chooser, false, FuzzyMatcher.Sorter.asc)
chooser:queryChangedCallback(function()
FuzzyMatcher.setChoices(choices, chooser, false, FuzzyMatcher.Sorter.asc)
end)
--chooser:rows(#choices)
chooser:rows(20)
chooser:width(60)
--chooser:bgDark(true)
--chooser:fgColor(hs.drawing.color.x11.orange)
--chooser:subTextColor(hs.drawing.color.x11.chocolate)
chooser:show()
end
hs.hotkey.bind(hyper, "b", keyInfo("macOS MenuBar"), MenuBarChooser)