Jump to content

Easy/Pre-made way to display grep results in Alfred?


Recommended Posts

Hey Alfred friends!

 

I'm spinning my wheels on a workflow trying to accomplish something that seems like it should be straightforward, but is giving me a headache.

 

The high-level idea was that I wanted to recreate an alias I have in the commandline. The alias is:

alias howto="cat ~/.aliases ~/.functions ~/.commands | grep $1"

The aliases, functions, and commands files are basically just files that have commands I'd like to use frequently or aliases that I have and want to reference quickly. It works great for when I forget how to do a thing on the commandline in git – as an example, let's say amend a commit message – and I can just type "howto amend" and it'll spit out every instance of "amend" referenced in any of the above files. This would be far more useful if I could access it from alfred though so I can store non-commandline specific things and don't always have to have a terminal around.

 

The problem I'm facing is figuring out how to display the grep results properly in Alfred. I don't want a notification I want a list of results that I can scan, then ideally press "Enter" on one of them to get a copy of the key (but that's a later issue).  Surely someone has already done something like this? From what I've seen it looks like the best thing to do would be to pass the results of the grep to a JavaScript file and then read in the query in JavaScript, parse it, and return the answer in JSON to that Alfred can understand it. I can't really find a good example of someone doing this with dynamic data though, only the static one referenced here.

 

Any help or general guidance would be appreciated!

 

-Mike

Link to comment
1 hour ago, mmroczka said:

Surely someone has already done something like this?

 

I dare say that most workflows do something very much like this. The Script Filter is arguably the most-used workflow element.

 

1 hour ago, mmroczka said:

From what I've seen it looks like the best thing to do would be to pass the results of the grep to a JavaScript file and then read in the query in JavaScript, parse it, and return the answer in JSON to that Alfred can understand it.

 

You could pipe grep into another script, or call grep from within a script. But you don’t actually need (shouldn’t use) pipes, as your script is technically a Useless Use of Cat: you can just run grep "$1" ~/file1 ~/file2 ~/file3

 

In any case, I wouldn’t recommend JavaScript. JXA has an extremely limited runtime, so you typically end up importing Objective-C libraries to do fairly basic things, and writing an unpleasant mix of JS and Objective-C. And Node isn’t included with macOS, so it’s a poor choice if you want to share the workflow.

 

Ruby or Python would be better choices.

 

Some people would do it in bash/zsh, but generating JSON by smushing strings together is a great way to cause yourself problems.

 

1 hour ago, mmroczka said:

I can't really find a good example of someone doing this with dynamic data though

 

The forum is full of them. Just grab a few workflows and have a look at the code. There are hundreds of workflows on GitHub, or you can go straight to the Awesome Alfred workflows list.

 

Link to comment

Here’s a quick and dirty solution in Ruby. You can paste it into the Script box of a Script Filter with /usr/bin/ruby as the Language and it should work:

require 'json'
require 'pathname'

files = %w(~/.aliases ~/.functions ~/.commands)

search_query = ARGV[0]
script_filter_items = []

files
.map { |f| Pathname.new(f).expand_path }
.each { |f| f.readlines
             .select { |l| l.include?(search_query) }
             .map(&:strip)
             .each { |l| script_filter_items.push(title: l) }
}

puts({ items: script_filter_items }.to_json)

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...