Jump to content

Calling Ruby Script with {Query} parameter


Recommended Posts

I have a Ruby script that I have developed that searches through an internal system we use through it's API.  

 

 

When I run the script in the terminal by calling:

ruby search.rb "TEST"

it runs just fine and I get the list of results I would expect, in the XML format to be passed to Alfred.

 

 

 

However when I call the script from Alfred:

ruby search.rb "{query}"

I type the keyword "S" and it shows the proper placeholder Title (so I know the keyword worked).  However, as soon as I start to type the query text it just goes to the default search items (e.g. Search Google for....). It never shows the "Please Wait Subtext...."

 

Has anyone encountered this issue or am I missing something critical?  Any help would be greatly appreciated.  

 

Also as an aside I'm not sure I understand the purpose of the two steps in the sample "Script Filter To Notification" workflow.  What is the "Script Filter" step doing different form the "Run Script" step?

 

 

Thanks in advance for any help...

 

 

 

Here's a picture of my workflow:

Screen%20Shot%202013-05-15%20at%201.44.0

 

Screen%20Shot%202013-05-15%20at%202.01.5

 

 

 

 

 

I have pasted as much of the script below as I can (it has some proprietary info in it so I can't share the full script), but I don't think i have omitted any critical information.  If there are any specific questions about certain parts of the missing scripts, I'll share what I can.

$logger = Yell.new "Alfred Logger", :level => :error

$search = ARGV[0].to_s.gsub("\\","")

 .......

$logger.info  "#{$search} was passed in as search paramater"

def xmlPackage(items)
  resultXML = Builder::XmlMarkup.new(:target => $output,:indent => 2)
  resultXML.instruct!


  resultXML.items do
    items.each do |item|
      resultXML.item(:uid=>"suggest {query}",:arg=>item.arg) {
        resultXML.title item.title
        resultXML.subtitle item.subtitle
        resultXML.icon item.icon
        resultXML.valid item.valid
      }
    end
  end  
end




Login = Struct.new( :url, :user, :password)
sessions = ...login paramateres......


items = []
newSessions = []


sessions.each_with_index do |session,index|
  
  $logger.info "checking session to #{session.url}: #{$sessions[index]}"
  $url = "https://#{session.url}/api"
  qUrl =  $url + "&search=#{$search}&returnlimits=20"


  
  results = JSON.parse(RestClient.get qUrl,:authorization=>$sessions[index],:content_type => :json, :accept => :json)


  if results["responseStatus"].eql? "FAILURE"
      ....address common failure cause....
  end
            
  $logger.info "Creating XML for session #{session.url} #{$session}"
  results["documents"].each_with_index do |result,index|
    icon     = getIcon(result["document"]["format__v"].to_s)
    arg      = .......
    title    = ....Title....
    subtitle = arg.to_s
    $logger.info "Found item: #{title}"




    items << Item.new(arg,title,subtitle,icon,"yes")
  end    
  $logger.info "XML created for session #{session.url}"
  
  newSessions << $session
  
end


...cycles through multiple sessions....


puts xmlPackage(items)
Link to comment

 

I have a Ruby script that I have developed that searches through an internal system we use through it's API.  

 

 

When I run the script in the terminal by calling:

ruby search.rb "TEST"

it runs just fine and I get the list of results I would expect, in the XML format to be passed to Alfred.

 

 

 

However when I call the script from Alfred:

ruby search.rb "{query}"

I type the keyword "S" and it shows the proper placeholder Title (so I know the keyword worked).  However, as soon as I start to type the query text it just goes to the default search items (e.g. Search Google for....). It never shows the "Please Wait Subtext...."

 

Has anyone encountered this issue or am I missing something critical?  Any help would be greatly appreciated.  

 

Also as an aside I'm not sure I understand the purpose of the two steps in the sample "Script Filter To Notification" workflow.  What is the "Script Filter" step doing different form the "Run Script" step?

 

 

Thanks in advance for any help...

 

 

 

Here's a picture of my workflow:

Screen%20Shot%202013-05-15%20at%201.44.0

 

Screen%20Shot%202013-05-15%20at%202.01.5

 

 

 

 

 

I have pasted as much of the script below as I can (it has some proprietary info in it so I can't share the full script), but I don't think i have omitted any critical information.  If there are any specific questions about certain parts of the missing scripts, I'll share what I can.

$logger = Yell.new "Alfred Logger", :level => :error

$search = ARGV[0].to_s.gsub("\\","")

 .......

$logger.info  "#{$search} was passed in as search paramater"

def xmlPackage(items)
  resultXML = Builder::XmlMarkup.new(:target => $output,:indent => 2)
  resultXML.instruct!


  resultXML.items do
    items.each do |item|
      resultXML.item(:uid=>"suggest {query}",:arg=>item.arg) {
        resultXML.title item.title
        resultXML.subtitle item.subtitle
        resultXML.icon item.icon
        resultXML.valid item.valid
      }
    end
  end  
end




Login = Struct.new( :url, :user, :password)
sessions = ...login paramateres......


items = []
newSessions = []


sessions.each_with_index do |session,index|
  
  $logger.info "checking session to #{session.url}: #{$sessions[index]}"
  $url = "https://#{session.url}/api"
  qUrl =  $url + "&search=#{$search}&returnlimits=20"


  
  results = JSON.parse(RestClient.get qUrl,:authorization=>$sessions[index],:content_type => :json, :accept => :json)


  if results["responseStatus"].eql? "FAILURE"
      ....address common failure cause....
  end
            
  $logger.info "Creating XML for session #{session.url} #{$session}"
  results["documents"].each_with_index do |result,index|
    icon     = getIcon(result["document"]["format__v"].to_s)
    arg      = .......
    title    = ....Title....
    subtitle = arg.to_s
    $logger.info "Found item: #{title}"




    items << Item.new(arg,title,subtitle,icon,"yes")
  end    
  $logger.info "XML created for session #{session.url}"
  
  newSessions << $session
  
end


...cycles through multiple sessions....


puts xmlPackage(items)

 

 

When creating workflows, Alfred doesn't import your profile so only /sbin;/bin;/usr/sbin;/usr/bin are in your $PATH variable. Alfred should be finding the ruby executable because it's in /usr/bin but is the script that you are attempting to execute in the workflow directory? If not, you need to specify the full path to it.

 

Script Filter perform a little differently that a Run Script item. Script Filters allow you to execute a script, and then generate an XML list of results to be passed back to Alfred. Those results are shown in Alfred's main interface as actionable items. The reason you drop back to a fallback search when you enter text is because Alfred is expecting you to have returned xml feedback. You script either isn't returning proper xml (could be as simple as an extra warning or error message in the script output), or isn't returning xml at all.

Link to comment

When creating workflows, Alfred doesn't import your profile so only /sbin;/bin;/usr/sbin;/usr/bin are in your $PATH variable. Alfred should be finding the ruby executable because it's in /usr/bin but is the script that you are attempting to execute in the workflow directory? If not, you need to specify the full path to it.

 

Script Filter perform a little differently that a Run Script item. Script Filters allow you to execute a script, and then generate an XML list of results to be passed back to Alfred. Those results are shown in Alfred's main interface as actionable items. The reason you drop back to a fallback search when you enter text is because Alfred is expecting you to have returned xml feedback. You script either isn't returning proper xml (could be as simple as an extra warning or error message in the script output), or isn't returning xml at all.

 

 

Thanks for the info David.  A couple of things.  I am calling the ruby script from in the workflow folder.  Based on your comment i believe that is setup correctly. Correct?!

 

 

Also, here is a sample of the XML responses I get from terminal (I think it looks correct).

<items>
  <item uid="suggest {query}" arg="https://sytem.com/1426">
    <title>Workbook</title>
    <subtitle>1426</subtitle>
    <icon>excel.png</icon>
  </item>
  <item uid="suggest {query}" arg="https://sytem.com/1067">
    <title>Working Template</title>
    <subtitle>1067</subtitle>
    <icon>word.png</icon>
  </item>
  <item uid="suggest {query}" arg="https://system.com/321">
    <title>Questionnaire</title>
    <subtitle>321</subtitle>
    <icon>word.png</icon>
  </item>
</items>

This is what the script in the "Script Filter" step is running

Edited by jhnielson
Link to comment

Thanks for the info David.  A couple of things.  I am calling the ruby script from in the workflow folder.  Based on your comment i believe that is setup correctly. Correct?!

 

 

Also, here is a sample of the XML responses I get from terminal (I think it looks correct).

<items>
  <item uid="suggest {query}" arg="https://sytem.com/1426">
    <title>Workbook</title>
    <subtitle>1426</subtitle>
    <icon>excel.png</icon>
  </item>
  <item uid="suggest {query}" arg="https://sytem.com/1067">
    <title>Working Template</title>
    <subtitle>1067</subtitle>
    <icon>word.png</icon>
  </item>
  <item uid="suggest {query}" arg="https://system.com/321">
    <title>Questionnaire</title>
    <subtitle>321</subtitle>
    <icon>word.png</icon>
  </item>
</items>

This is what the script in the "Script Filter" step is running

 

The XML appears to be valid. I created a quick script filter just to test it but I get results. 

 

Just to shortcut this a bit, is there any way you could share the workflow with me so I could look at it directly? You could email it to david@alfredapp.com

Link to comment

The XML appears to be valid. I created a quick script filter just to test it but I get results. 

 

Just to shortcut this a bit, is there any way you could share the workflow with me so I could look at it directly? You could email it to david@alfredapp.com

 

 

 

Thanks for all the help David.  I emailed you the workflow and appreciate any insights you can provide

Link to comment

The XML appears to be valid. I created a quick script filter just to test it but I get results. 

 

Just to shortcut this a bit, is there any way you could share the workflow with me so I could look at it directly? You could email it to david@alfredapp.com

 

 

So i think I have discovered something relevant that may have something to do with the issue.  I simply copied the XML output from Terminal and pasted it directly into the script filter in Aflred:

<?xml version="1.0" encoding="UTF-8"?>
<items>
<item uid="suggest {query}" arg="https://system.com/ui/?tk=ABSCDEFG#doc/894">
    <title>v001015 Test</title>
    <subtitle>"https://system.com/ui/?tk=ABSCDEFG#doc/894"</subtitle>
    <icon>binder.png</icon>
  </item>
</items>

 and it still didn't provide any results (which I would assume it would automatically). Then in comparing my file to the example xmlformat in Alfred, I noticed that the first line contained:  "cat << EOB" in the first line and "EOB" in the last line.  When i pasted the XML below... BAM! I got the filter result.  

cat << EOB
<?xml version="1.0" encoding="UTF-8"?>
  <items>
     <item uid="suggest {query}" arg="https://system.com/ui/?tk=ABSCDEFG#doc/894">
     <title>v001015 Test</title>
     <subtitle>"https://system.com/ui/?tk=ABSCDEFG#doc/894"</subtitle>
     <icon>binder.png</icon>
  </item>
</items>
EOB

so I created a very basic script that will just print out a xml block

Item = Struct.new( :arg, :title, :subtitle, :icon)

def xmlPackage(items)
  resultXML = Builder::XmlMarkup.new(:target => $output,:indent => 2)
  resultXML.instruct!

  resultXML.items do
    items.each do |item|
      resultXML.item(:uid=>"suggest {query}",:arg=>item.arg) {
        resultXML.title item.title
        resultXML.subtitle item.subtitle
        resultXML.icon item.icon
        #resultXML.valid item.valid
      }
    end
  end  
end


items = []

    items << Item.new("TEST","TITLE_TEST","Subtitle_TEST","test.png")
    puts "cat << EOB\n" + xmlPackage(items) + "EOB"

But that still won't give me any results. It just goes to the fallback results.

It still seems like Alfred is not executing the script

Edited by jhnielson
Link to comment

So i think I have discovered something relevant that may have something to do with the issue.  I simply copied the XML output from Terminal and pasted it directly into the script filter in Aflred:

<?xml version="1.0" encoding="UTF-8"?>
<items>
<item uid="suggest {query}" arg="https://system.com/ui/?tk=ABSCDEFG#doc/894">
<title>v001015 Test</title>
<subtitle>"https://system.com/ui/?tk=ABSCDEFG#doc/894"</subtitle>
<icon>binder.png</icon>
</item>
</items>

 and it still didn't provide any results (which I would assume it would automatically). Then in comparing my file to the example xmlformat in Alfred, I noticed that the first line contained:  "cat << EOB" in the first line and "EOB" in the last line.  When i pasted the XML below... BAM! I got the filter result.  

cat << EOB
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item uid="suggest {query}" arg="https://system.com/ui/?tk=ABSCDEFG#doc/894">
<title>v001015 Test</title>
<subtitle>"https://system.com/ui/?tk=ABSCDEFG#doc/894"</subtitle>
<icon>binder.png</icon>
</item>
</items>
EOB

so I created a very basic script that will just print out a xml block

Item = Struct.new( :arg, :title, :subtitle, :icon)

def xmlPackage(items)
  resultXML = Builder::XmlMarkup.new(:target => $output,:indent => 2)
  resultXML.instruct!

  resultXML.items do
    items.each do |item|
      resultXML.item(:uid=>"suggest {query}",:arg=>item.arg) {
        resultXML.title item.title
        resultXML.subtitle item.subtitle
        resultXML.icon item.icon
        #resultXML.valid item.valid
      }
    end
  end  
end


items = []

    items << Item.new("TEST","TITLE_TEST","Subtitle_TEST","test.png")
    puts "cat << EOB\n" + xmlPackage(items) + "EOB"

But that still won't give me any results. It just goes to the fallback results.

It still seems like Alfred is not executing the script

 

I didn't get to look back into your workflow last night but will try to today/tonight. I would suggest though, take the xml you know works, put it in a file, and have a workflow that, using ruby, calls a script that does nothing but read the file, and return the contents. You won't need the cat <<EOB for this. Just the xml.

Link to comment
  • 3 months later...

I have same issue with my workflow in ruby. I'm trying to get data from a API. but Alfred give me  'fallback results' back.

I can get correct feedback from Alfred if Item num is less than 11.

 

persons = get_persons_from_a_API

persons.each_with_index do |story, i|
  item = {
    :title =>    person['name'],
    :subtitle => person['address'],
    :arg =>      person['id'],
    :uid =>      person['id'],
    :icon =>     { :name => "icon.png" }
  }

  # I can't get proper feedback without 'if statement'
  feedback.add_item(item) if i < 10
end

puts feedback.to_xml

Edited by akira
Link to comment

 

I have same issue with my workflow in ruby. I'm trying to get data from a API. but Alfred give me  'fallback results' back.

I can get correct feedback from Alfred if Item num is less than 11.

 

persons = get_persons_from_a_API

persons.each_with_index do |story, i|
  item = {
    :title =>    person['name'],
    :subtitle => person['address'],
    :arg =>      person['id'],
    :uid =>      person['id'],
    :icon =>     { :name => "icon.png" }
  }

  # I can't get proper feedback without 'if statement'
  feedback.add_item(item) if i < 10
end

puts feedback.to_xml

 

Alfred shouldn't care about the number of items in the list. Have you tried outputting the generated XML to a text file to verify that it is correct?

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