Lately, I’ve been using the awesome Hammerspoon to automate some of the aspects of macOS. As an example of its potential, we’ll now see how to create an interactive emoji search-engine, that will allow us to search, select and paste emojis by using their name or a few keywords.

As a result, we’ll get something like this:

Preliminary setup

Before we begin, download this archive and unzip into your ~/.hammerspoon directory. The archive contains a few thousands PNG images depicting the emojis and a JSON file encoding their details. It has been generated by using the utility you can find here.

Hammerspoon setup

Now, we’ll use Hammerspoon to load the emojis and build the search engine. To do so we’ll use hs.chooser , an Hammerspoon extension that creates a Spotlight-like window that allows filtering and selecting data. We’ll populate it with emojis; once selected, the emoji will be copied to clipboard and “typed” in the focused application.

Copy and paste the following snippet in your init.lua file:

-- Build the list of emojis to be displayed. local choices = {} for _ , emoji in ipairs ( hs . json . decode ( io.open ( "emojis/emojis.json" ): read ())) do table.insert ( choices , { text = emoji [ 'name' ], subText = table.concat ( emoji [ 'kwds' ], ", " ), image = hs . image . imageFromPath ( "emojis/" .. emoji [ 'id' ] .. ".png" ), chars = emoji [ 'chars' ] }) end -- Focus the last used window. local function focusLastFocused () local wf = hs . window . filter local lastFocused = wf . defaultCurrentSpace : getWindows ( wf . sortByFocusedLast ) if # lastFocused > 0 then lastFocused [ 1 ]: focus () end end -- Create the chooser. -- On selection, copy the emoji and type it into the focused application. local chooser = hs . chooser . new ( function ( choice ) if not choice then focusLastFocused (); return end hs . pasteboard . setContents ( choice [ "chars" ]) focusLastFocused () hs . eventtap . keyStrokes ( hs . pasteboard . getContents ()) end ) chooser : searchSubText ( true ) chooser : choices ( choices )

If you want, you can also customize the appearance of the chooser:

chooser : rows ( 5 ) chooser : bgDark ( true )

As a final step, bind the chooser to any key you like:

hs . hotkey . bind ({ "cmd" , "alt" }, "E" , function () chooser : show () end )

Other tips

As a final note, you can find my Hammerspoon complete configuration here.

Please enable JavaScript to view the comments powered by Disqus.

Disqus