Skip to content

Instantly share code, notes, and snippets.

@asmagill
asmagill / altHold.lua
Last active November 27, 2016 18:26
Capture holding alt key down for set time period in Hammerspoon
local alert = require("hs.alert")
local timer = require("hs.timer")
local eventtap = require("hs.eventtap")
local events = eventtap.event.types
local module = {}
-- You either override these here or after including this file from another, e.g.
--
@asmagill
asmagill / ctrlDoublePress.lua
Created April 18, 2016 03:20
Capture double tap of Ctrl in Hammerspoon
local alert = require("hs.alert")
local timer = require("hs.timer")
local eventtap = require("hs.eventtap")
local events = eventtap.event.types
local module = {}
-- Save this in your Hammerspoon configuration directiorn (~/.hammerspoon/)
-- You either override timeFrame and action here or after including this file from another, e.g.
@asmagill
asmagill / javascripttest.lua
Last active March 13, 2022 23:59
Test for hs.javascript in Hammerspoon
-- Example to test hs.javascript. This is a modified version of the code found at
-- https://trans4mind.com/personal_development/JavaScript/longnumPiMachin.htm
--
-- Note that the last 5 digits are probably wrong (this returns 5 more digits than
-- requested), but the specified N digits are provably correct (see web page for
-- details)
--
-- This results in the following on my machine:
--
-- true PI (10005)=3.
#! /bin/sh
# This is the sample file used for testing during the development of hs.styledtext
# for Hammerspoon. See https://github.com/Hammerspoon/hammerspoon/pull/620
# It is based on code I found when I first discovered GeekTool, but I forget
# where it originated or how much I edited it for my purposes, so if you find
# this and feel additional attribution is warranted, please let me know.
##################
@asmagill
asmagill / loopstop.lua
Last active March 12, 2019 12:34
prevent infinite loop in lua code from locking Hammerspoon up...
--
-- uses debug.sethook and a timer to break out of infinite loops in lua code within Hammerspoon
--
-- Haven't had any problems with it or false positives, but YMMV -- standard disclaimers, etc.
--
-- Updates 2015-12-21:
-- should play nicely with other hooks by storing info about it and chaining
-- you can force an "immediate" break by holding down CMD-CTRL-SHIFT-ALT-CAPSLOCK-FN all at once
-- you'll need to remove "and mods.fn" where noted below if your keyboard does not have this
-- modifier (non-laptops, I suspect)
@asmagill
asmagill / graphpaper.lua
Last active October 22, 2015 20:04
Hammerspoon: generate a table of images of "graph lines" which will fill the specified screen via imageFromASCII
-- save file in ~/.hammerspoon and use as follows:
--
-- graph = require("graphpaper")
-- images = graph.fillScreen(x,y,screen)
-- all three parameters are optional. x and y specify the graph size in screen points.
-- default 10 for x, default whatever x is for y, default hs.screen.mainScreen() for screen
--
-- so, on a MacBook Air with a 1920x900 non-retina screen: `images = graph.fillScreen(20)`
-- images will be an array of 6 images. The array has a metatable set replicating the hs.drawing methods
-- and applies them to all elements of the array, so you can show the graph with:
@asmagill
asmagill / dash.lua
Created September 19, 2015 22:37
Sample Hammerspoon Dash docset viewer
--
-- Requirements: hs.webview -- currently a proposed module addition
-- (see https://github.com/Hammerspoon/hammerspoon/pull/544)
-- luarocks-5.3, lsqlite3 lua-rock, and sqlite3 installed with extension support
-- (stock 10.11 sqlite3 doesn't have this... not sure about earlier OS X's,
-- but the brew versions work)
-- Uncompressed Hammerspoon.docset (Dash 2 version will work, not sure yet how to
-- uncompress Dash 3 version yet...)
--
-- Update _rootPath to match where your docset is located
@asmagill
asmagill / constants.lua
Last active December 19, 2015 08:00
protect tables of constants in Hammerspoon
--[[
A way to protect tables of constants in Hammerspoon modules from inadvertant changes
Updated 2015-12-19: differentiate __tostring output for keyed table vs array
recursively descend, making each sub-table a constant table as well
weak key for storing original table so can be used with generated data (in testing)
notifies if wrong type passed in and returns unchanged value instead
]]--
--
-- Code snippit to check/add LSUIElement to Hammerspoon's Info.plist file
-- if running 10.8, since this entry is required for HS to be able to toggle
-- the dock icon. Surprisingly it isn't necessary in later OS X versions...
--
-- Save this file as LSUIElementCheck.lua in ~/.hammerspoon/ and add:
--
-- dofile("LSUIElementCheck.lua")
--
-- to the top of your init.lua also located in ~/.hammerspoon/
@asmagill
asmagill / autohide.lua
Created August 1, 2015 19:42
Auto hide Hammerspoon console when it loses focus
--
-- Basic console auto-hide when Hammerspoon loses focus
--
-- ConsoleWatcher:start()
-- Turns on the watcher, and the Hammerspoon console, if open, will close when you enter another application
-- ConsoleWatcher:stop()
-- Turns off the watcher; Hammerspoon console open/close state is left untouched.
ConsoleWatcher = hs.application.watcher.new(function(name,event,hsapp)
if name then