Jump to content

searching script filter results?


Recommended Posts

require 'json'
require 'net/http'
require "rexml/document"

jsonUrl = 'jsonFile'
resp = Net::HTTP.get_response(URI.parse(jsonUrl))
data = resp.body

result = JSON.parse(data)
document = REXML::Element.new("items")
result['data'].each {
 |x|
 x['properties'].each {
 	|y|
 	new_item = REXML::Element.new('item')
	new_item.add_attributes({
	    'uid'          => 'test', 
	    'arg'          => y['id'], 
	    'valid'        => 'no', 
	    'autocomplete' => y['name']
	})
	REXML::Element.new("title", new_item).text    = y['name']
	document << new_item
 }
}
puts document.to_s

So I have this script filter that loads in a JSON file and the result you select sends off an ID to open a webpage using that ID.

 

It is fired by typing 'dashboard search_item_here'

 

The results all load into alfred fine, however it just loads all my results in and you have to select them manually with the up/down arrows (theres over 100 results). I want it to filter the results taking the 'search_item_here' text but I'm totally lost on how to do that? What am i missing?

 

Thanks all

Link to comment
So I have this script filter that loads in a JSON file and the result you select sends off an ID to open a webpage using that ID

 

Hi there, before asking a Powerpack-related question, could you please fill in your Powerpack email address in your forum profile? 

Link to comment

Updated!

 

But I also solved my own problem, I thought this was somethign that alfred would handle but I just built it into ruby

 

for other peoples use, i'm now using:

require 'json'
require 'net/http'
require "rexml/document"

jsonUrl = 'JSON url'
resp = Net::HTTP.get_response(URI.parse(jsonUrl))
data = resp.body

result = JSON.parse(data)
document = REXML::Element.new("items")
results = Hash.new();
result['data'].each {
 |x|
 x['properties'].each {
 	|y , i|
	searchName = y['name'].downcase
	if searchName.include? "{query}" 
		results[y['name']] = y['id']
	 	new_item = REXML::Element.new('item')
		new_item.add_attributes({
		    'uid'          => 'test', 
		    'arg'          => y['id'], 
		    'valid'        => 'no', 
		    'autocomplete' => y['name']
		})
		REXML::Element.new("title", new_item).text    = y['name']
		document << new_item
	end
 }
}
puts document.to_s

if anyone knows of a way to make this faster however.. I'd be very greatful

Link to comment
  • 2 weeks later...

If you want to make it faster, cache the JSON file.

 

The way Alfred works is it will call your workflow every time you type a letter. So, it will try to call the workflow 4 times if your query is 'test'. Alfred won't call your workflow again until the previous run has finished, so typically your workflow will block for several seconds if it does something really slow like getting data from the web.

 

I don't know what JSON file you're grabbing, so I've no idea whether 10 seconds or 10 hours is a more appropriate time to cache it for, but that's what you need to do to speed up the workflow.

Link to comment

As Dean said, caching is always a good idea. It will give you the best bang for your buck.

 

It sometimes helps to have a minimum number of characters for the argument before you start searching. Generally searches with fewer than three or four characters are worthless,

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...