Implementing GIF Search with Alfred

· 2 min read

WeChat supports adding GIFs to emoji packs. To enrich my meager emoji collection, I needed a convenient way to find new animated GIFs. There’s a foreign website called Giphy that’s quite good and provides API support, so I spent some time implementing a workflow for searching emoji packs.

Effect

Type “gif” or “sticker” to search, press Enter to copy to clipboard, then ⌘V to paste. There are also other operations, like ⌥ to browse in browser, ⌘ to browse in Finder, etc.

So now I never worry about not finding cool emoji packs.

Workflow download link: Click here

Implementation Details

I encountered some pitfalls implementing this workflow, marking them here:

  1. Alfred’s Script filter Input icon currently doesn't support GIF animation, even for animated GIFs. As you can see above, the GIFs in the search results list are just static first-frame images.

  2. Copying GIFs to clipboard is solved using Apple Script. Key code below - currently tested to work with WeChat, Typora, Evernote, etc.:

    set the clipboard to POSIX file thePath
    
  3. GIF URLs returned by Giphy have some parameters that need to be removed for normal download, otherwise you’ll get a 403 error.

  4. Downloaded GIF animations are cache resources. Alfred has recommended locations for cache:

    Cache: ~/Library/Caches/com.runningwithcrayons.Alfred/Workflow Data/[bundle id], corresponding to environment variable alfred_workflow_cache Data: ~/Library/Application Support/Alfred/Workflow Data/[bundle id], corresponding to environment variable alfred_workflow_data

    I currently choose alfred_workflow_cache, but Alfred doesn’t create the corresponding workflow folder by default - you need to create it manually.

  5. Paths like alfred_workflow_cache contain spaces. When used as quicklookurl, they need encoding spaces replaced with %20 to work properly.

  6. Giphy is blocked in China. Mac needs to use system proxy, and Alfred also needs to use proxy to work normally.

  7. Giphy provides SDKs for various platforms, but since this is for workflow, I use the WEB API.

  8. Giphy API Keys are divided into Production and beta. Production Key requires application review - they reply within 5 business days. From testing, the application threshold isn’t high.