Hammerspoon / Spoons

The official repository of Spoon plugins
https://www.hammerspoon.org/Spoons/
453 stars 141 forks source link

HammerText Text Expansion #264

Open thomasnield opened 2 years ago

thomasnield commented 2 years ago

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?

gineer01 commented 2 years ago

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?

thomasnield commented 2 years ago

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 commented 2 years ago

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

gineer01 commented 2 years ago

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

thomasnield commented 2 years ago

It looks like this all came from #1042

ChillarAnand commented 1 year ago

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

cmsj commented 3 months ago

@thomasnield I know it's taken us a while to get to it, but do you want to proceed with this PR? If so, it will need to be updated to contain the source. The zip files are managed automatically, so you need to put the source for the Spoon in the Sources directory.

Also, just from a quick look, your docstrings probably aren't going to pass the linter, I would suggest trying to make them more like the ones used in other Spoons.