Use Text Snippets to Invoke Node.js Commands with Alfred Workflows

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

With Alfred, you can customize snippets to execute scripts anywhere on your computer. This allows you to type a couple of characters to generate text from runtimes such as Node.js. This lesson walks you through setting up a custom Alfred Workflow to type a snippet, run a script, then paste out the result of the script.

John Lindquist: [00:00] Project Name Generator is a package which can be used as a CLI. Once you install it globally, you can invoke it with these options. I'm going to invoke project-name-generator -a -o with "dashed" passed in to give me a generated project name.

[00:15] In Alfred, if I open my Preferences, and I go into my Workflows, I'll create a new Blank Workflow. We'll give this a name of Project Name Generator, say that it generates project names, tack on a projectnamegenerator to our Id, hit Create.

[00:31] We want our initial trigger to be a Snippet. The keyword is going to be pn for project name. You can make that anything. I'm leaving my prefix empty and making my suffix ,, which will trigger that keyword. We'll hit Save there.

[00:45] Then the action we want it to take is to run a script. We can simply take what we wrote out here, copy this and paste it in here. The one thing we have to do before this runs is to source our zshrc file and set the language to zsh. This way, it can pull in the environment variables and the required things from Node.

[01:06] If you are missing this line, it would just have an output as this as text. With this line, it'll run this command with these arguments. I'll hit Save here.

[01:15] The output here is going to be Copy to Clipboard. The query is just the result from the previous step. The result from here will be the input here to copy to the clipboard. Then the final step is an output of Dispatch Key Combo. We want it to paste, so just Command-V, hit Save, and then go here.

[01:33] The steps are pn,, invokes this script which generates a name which copies it to the clipboard which then pastes it out, meaning that anywhere on my computer -- I'll type in Chrome right here pn,, -- you'll see it generated funny-fall. Do that again, pn,, disgusting-design, or even in here pn,, serene-step. That snippet should work anywhere you need it.

[01:54] As a final step, we'll open this back up, do an image search for a generator, come over here, grab a generator, drop it right here just so it looks a bit nicer.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today