Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HammerText Text Expansion #264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thomasnield
Copy link

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?

I was a [heavy AutoHotkey user](https://www.autohotkey.com/docs/Tutorial.htm#s2) 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
Copy link

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
Copy link
Author

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
Copy link
Author

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
Copy link

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

@thomasnield
Copy link
Author

@ChillarAnand
Copy link

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

@cmsj cmsj closed this Aug 7, 2024
@cmsj cmsj reopened this Aug 7, 2024
@cmsj
Copy link
Member

cmsj commented Aug 7, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants