--- === KSheet === --- --- Keybindings cheatsheet for current application --- --- Download: [https://github.com/Hammerspoon/Spoons/raw/master/Spoons/KSheet.spoon.zip](https://github.com/Hammerspoon/Spoons/raw/master/Spoons/KSheet.spoon.zip) local obj={} obj.__index = obj -- Metadata obj.name = "KSheet" obj.version = "1.0" obj.author = "ashfinal " obj.homepage = "https://github.com/Hammerspoon/Spoons" obj.license = "MIT - https://opensource.org/licenses/MIT" -- Workaround for "Dictation" menuitem hs.application.menuGlyphs[148]="fn fn" obj.commandEnum = { cmd = '⌘', shift = '⇧', alt = '⌥', ctrl = '⌃', } function obj:init() self.sheetView = hs.webview.new({x=0, y=0, w=0, h=0}) self.sheetView:windowTitle("CheatSheets") self.sheetView:windowStyle("utility") self.sheetView:allowGestures(true) self.sheetView:allowNewWindows(false) self.sheetView:level(hs.drawing.windowLevels.modalPanel) end local function processMenuItems(menustru) local menu = "" for pos,val in pairs(menustru) do if type(val) == "table" then -- TODO: Remove menubar items with no shortcuts in them if val.AXRole == "AXMenuBarItem" and type(val.AXChildren) == "table" then menu = menu .. "" elseif val.AXRole == "AXMenuItem" and not val.AXChildren then if not (val.AXMenuItemCmdChar == '' and val.AXMenuItemCmdGlyph == '') then local CmdModifiers = '' for key, value in pairs(val.AXMenuItemCmdModifiers) do CmdModifiers = CmdModifiers .. obj.commandEnum[value] end local CmdChar = val.AXMenuItemCmdChar local CmdGlyph = hs.application.menuGlyphs[val.AXMenuItemCmdGlyph] or '' local CmdKeys = CmdChar .. CmdGlyph menu = menu .. "
  • " .. CmdModifiers .. " " .. CmdKeys .. "
    " .. " " .. val.AXTitle .. "
  • " end elseif val.AXRole == "AXMenuItem" and type(val.AXChildren) == "table" then menu = menu .. processMenuItems(val.AXChildren[1]) end end end return menu end local function generateHtml(application) local app_title = application:title() local menuitems_tree = application:getMenuItems() local allmenuitems = processMenuItems(menuitems_tree) local html = [[
    ]] .. app_title .. [[

    ]] .. allmenuitems .. [[

    ]] return html end --- KSheet:show() --- Method --- Show current application's keybindings in a webview --- function obj:show() local capp = hs.application.frontmostApplication() local cscreen = hs.screen.mainScreen() local cres = cscreen:fullFrame() self.sheetView:frame({ x = cres.x+cres.w*0.15/2, y = cres.y+cres.h*0.25/2, w = cres.w*0.85, h = cres.h*0.75 }) local webcontent = generateHtml(capp) self.sheetView:html(webcontent) self.sheetView:show() end --- KSheet:hide() --- Method --- Hide the cheatsheet webview --- function obj:hide() self.sheetView:hide() end return obj