Jump to content

How do I filter the results matching title or subtitle in a workflow?


Recommended Posts

I have written a workflow that gives output the attached file.

 

The way I have added in alfred feedback is this:

       fb.add_item({
          :uid => project["id"],
          :title => project["content"],
          :subtitle => [name,dueDate].map(&:to_s).join(''),
          :arg => url,
          :autocomplete => project["content"],
          :valid => "yes"
        })

In the search results,  I want to search by both title and subtitle. How do we do this? Currently it looks only in the title.

 

Here is the complete code:

#!/usr/bin/env ruby
# encoding: utf-8

($LOAD_PATH << File.expand_path("..", __FILE__)).uniq!

require "rubygems" unless defined? Gem # rubygems is only needed in 1.8
require "bundle/bundler/setup"
require "alfred"
require "net/http"
require "json"

Alfred.with_friendly_error do |alfred|

  ALFRED = alfred

  # prepend ! in query to refresh
  is_refresh = false
  if ARGV[0].start_with? '!'
    is_refresh = true
    ARGV[0] = ARGV[0].gsub(/!/, '')
  end

  # contants
  QUERY = ARGV[0]
  BASECAMP_TOKEN = ARGV[1]
  BASECAMP2_COMPANY_IDS = ARGV[2].split(",")
  BASECAMP3_COMPANY_IDS = ARGV[3].split(",")

  def get_uri(api, company_id, x)
    if api == 2
      "https://basecamp.com/#{company_id}/api/v1/projects.json"
    else
      "https://3.basecampapi.com/#{company_id}/projects/recordings.json?type=Todo&page=#{x}"
    end
  end

  def get_project_json(api, company_id)
   

    # use system curl because system ruby on osx is 2.0.0
    # 2.0.0 does not have updated openssl bindings for Net:HTTP
    # el capitan has an issue with libcurl based solutions finding libcurl
    # so, just curl the joker
    x = 0
    parsed = []
    loop do
      x = x + 1
      uri = get_uri(api, company_id, x)
      # puts uri
      request = <<-EOF
        curl -s -H "Authorization: Bearer #{BASECAMP_TOKEN}" \
        -H 'User-Agent: alfred2-basecamp (john.pinkerton@me.com)' \
        #{uri}
      EOF
      response = `#{request}`
      parsed1 = JSON.parse(response)
      parsed = parsed + parsed1
      break if x > 0
    end
    # puts parsed.length
    # request = <<-EOF
    #   curl -s -H "Authorization: Bearer #{BASECAMP_TOKEN}" \
    #   -H 'User-Agent: alfred2-basecamp (john.pinkerton@me.com)' \
    #   #{uri}&page=#{x}
    # EOF

    # response = `#{request}`
    # parsed = JSON.parse(response)
    # puts parsed
    begin
      throw_token_error if parsed["error"]
    rescue
      parsed
    end
  end

  def load_projects
    fb = ALFRED.feedback

    b2_projects = BASECAMP2_COMPANY_IDS.map{ |company|
      get_project_json(2, company)
    }

    b3_projects = BASECAMP3_COMPANY_IDS.map{ |company|
      get_project_json(3, company)
    }

    projects_collection = b2_projects + b3_projects

    projects_collection.each do |projects|
      projects.each do |project|
        url = project["app_url"] || project["url"]
        url.sub!("basecampapi", "basecamp") if url.include?("basecampapi")
        name = ""
        names = []
        if project["assignees"].length > 0
          project["assignees"].each do |assignee|
            names << assignee['name']
            name = names.join(', ')
            name = '(' + name + ')'
            name = name + '   '
          end
        end
        dueDate = project['due_on']
        if dueDate
          dueDate = 'Due Date: ' + dueDate
        end
        fb.add_item({
          :uid => project["id"],
          :title => [project["content"]].map(&:to_s).join(''),
          :subtitle => [name,dueDate].map(&:to_s).join(''),
          :arg => url,
          :autocomplete => [project["content"],name,dueDate].map(&:to_s).join(''),
          :valid => "yes"
        })
      end
    end

    fb
  end

  def throw_token_error
    fb = ALFRED.feedback

    if BASECAMP_TOKEN != ""
      title = "You need a new token"
    else
      title = "You need a token"
    end

    fb.add_item({
      :uid => "gettoken",
      :title => title,
      :subtitle => "Press enter and follow the instructions",
      :arg => "https://alfred2-basecamp-auth.herokuapp.com"
    })

    puts fb.to_alfred
    exit
  end

  throw_token_error if BASECAMP_TOKEN == ""

  ALFRED.with_rescue_feedback = true
  ALFRED.with_cached_feedback do
    use_cache_file :expire => 86400
  end

  if !is_refresh and fb = ALFRED.feedback.get_cached_feedback
    # cached feedback is valid
    puts fb.to_alfred(ARGV[0])
  else
    fb = load_projects
    fb.put_cached_feedback
    puts fb.to_alfred(ARGV[0])
  end
end

 

image.png

Link to comment
  • deanishe changed the title to How do I filter the results matching title or subtitle in a workflow?

Presumably you're using the "Alfred filters results" option. The way that works is, it first checks to see if an item has a match field. If it does, Alfred filters against that. If it doesn't, it filters based on title.


So you need to add a match field to each item that contains both the item's title and subtitle (or whatever else you want Alfred to match the query against).

 

However, I'm pretty sure that the alfred library you're using doesn't support match (it has never been updated for Alfred 3). So you'll have to ditch its feedback class and generate your own JSON.

 

Note: I've changed the title of the thread. HOWTO is customarily used to mark explanations of how to do something, not questions about how to do something. At least, that's the way we employ it on this forum.

Edited by deanishe
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...