Spoons icon indicating copy to clipboard operation
Spoons copied to clipboard

HammerText Text Expansion

Open thomasnield opened this issue 2 years ago • 6 comments

I was a heavy AutoHotkey user on Windows and finding Hammerspoon is the closest replacement for it on Mac. I think there needs to be a text replacement spoon and I found one in this snippet here.

https://gist.github.com/maxandersen/d09ebef333b0c7b7f947420e2a7bbbf5

Can we implement this officially as a spoon?

thomasnield avatar Jan 26 '22 00:01 thomasnield

Mac OS already has this functionality out of the box as explained here: https://www.techradar.com/how-to/computing/apple/how-to-use-text-shortcuts-on-mac-1308652 . Does this spoon offer advantages over that functionality?

gineer01 avatar Jan 26 '22 05:01 gineer01

I know that... but what about text replacements that are not static or literal strings? Like dates, random string generation, auto incrementing, etc...

For example, my big use when writing technical books with lots of figures is generating random strings of alphanumeric characters to assign as figure ID's. I don't know if this is possible to do with Mac's built-in features, at least not easily and without tons of back-and-forth between shell scripts and Automator.

function randomStr(keyLength)
    local upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    local lowerCase = "abcdefghijklmnopqrstuvwxyz"
    local numbers = "0123456789"
    
    local characterSet = upperCase .. lowerCase --.. numbers 
    
    local output = ""
    
    for	i = 1, keyLength do
    	local rand = math.random(#characterSet)
    	output = output .. string.sub(characterSet, rand, rand)
    end
    
    return output
end

-- Text replacement
ht = hs.loadSpoon("HammerText")
ht.keywords = {
    ["[name"] = "Thomas Nield",
    ["[date"] = function() return os.date("%m/%d/%Y") end,
    ["[time"] = function() return os.date("%I:%M %p") end,
    ["[randstr"] = function() return randomStr(10) end,

}

thomasnield avatar Jan 26 '22 15:01 thomasnield

As far as I can tell doing another Google search, again it's just a lot of clicks and separate applications with Automator... building all this functionality with disparate scripts, UI wizards, and allowing permissions. It would be nice if this was a one-stop shop with HammerSpoon where I can add a text replacement function to my other automations without all that UI hassle.

https://www.techradar.com/how-to/computing/apple/how-to-use-text-shortcuts-on-mac-1308652

thomasnield avatar Jan 26 '22 15:01 thomasnield

The use case for date/time and random string is interesting. Thanks for the examples!

gineer01 avatar Jan 27 '22 22:01 gineer01

This PR seems to be old. Any plans on this?

ChillarAnand avatar May 18 '23 10:05 ChillarAnand