Action

Emojify

Posted by agiletortoise, Last update over 3 years ago

Swap common words with their matching emoji characters, or reverse the process.

Emoji matches are defined in a variable in the first step of the action, and can be easily modified to support other emoji. Only a few emoji are included as examples, like “pizza”, “dog”, “cat”, “shrug”. Add addition ones by editing the “emojis” variable with other values.

The action also supports the option to require the word be followed by the word “emoji” when the “Require ‘emoji’ keyword is enabled. This works well with dictation to allow you to say, “Do you want to eat pizza emoji?” and later convert only the words you marked with “emoji”.

Steps

  • script

    // if true, replacements will look for "emoji" after the keyword.
    // so "pizza emoji" would become "🍕", rather than just
    // "pizza". 
    // Useful converting dictated passages.
    let requireEmojiKeyword = false;
    
    // edit the below to configure the emoji matches you want replaced
    const emojis = {
    	"crying": "😭",
    	"winking": "😉",
    	"pizza": "🍕",
    	"praying": "🙏",
    	"monkey": "🐒",
    	"dog": "🐶",
    	"cat": "🐈",
    	"shrug": "🤷‍♂️",
    	"praying": "🙏",
    	"halo": "😇",
    	"sunglasses": "😎",
    	"ghost": "👻",
    	"fistbump": "🤜"
    };
    
  • script

    function inverse(obj){
      var retobj = {};
      for(var key in obj){
        retobj[obj[key]] = key;
      }
      return retobj;
    }
    
    let f = () => {
    	let p = new Prompt();
    	p.title = "Emojify";
    	p.message = "Would you like to emojify (convert text to emoji) or de-emojify (replace emoji with text)?"
    	p.addSwitch("requireEmoji", "Require \"emoji\" after", requireEmojiKeyword);
    	p.addButton("Emojify");
    	p.addButton("De-Emojify");
    
    	if (!p.show()) { return  false; }
    
    	requireEmojiKeyword = p.fieldValues['requireEmoji'];
    	let [st, len] = editor.getSelectedRange();
    	if (len == 0) { // no selection, use full draft
    		st = 0;
    		len = editor.getText().length;
    	}
    	let text = editor.getTextInRange(st, len);
    
    	const isReversing = p.buttonPressed == 'De-Emojify';
    	const vals = (isReversing ? inverse(emojis) : emojis);
    	for (let key in vals) {
    		let lookup = key;
    		if (!isReversing && requireEmojiKeyword) {
    			lookup = `${key} emoji`;
    		}
    		let re = new RegExp(`${lookup}`, 'gi');
    		text = text.replace(re, vals[key]);
    	}
    	editor.setTextInRange(st, len, text);
    }
    
    if (!f()) {
    	context.cancel();
    }

Options

  • After Success Default
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.