Jump to content

Prevent "flash" of no result


Recommended Posts

I've created a workflow to get search results from an url (JSON-RPC) and display it in Alfred. Everything works, but I can't find a way to prevent Alfred from briefly showing the "no result" first and then the actual results. Below is a very simplified version of my code. 

if (($json = @file_get_contents($my_url, false, $context)) !== false) { 

  $obj = json_decode($json);

  // check if there is any results
  if ($obj->result) {
  
    // do some stuff

    // prepare the result to return to Alfred
    $wf->result( 
      $uid, 
      $goto_url, 
      $title, 
      $subtitle, 
      'icon.png'
      );

    // echo the result as xml for Alfred
    echo $wf->toxml();

  } else {
    
    // set the "no result" to return to Alfred
    $wf->result( 
      'mysearch', 
      'na', 
      'No result', 
      'No result was found matching the search term.', 
      'icon.png',
      'no'
      );

    // echo the result as xml for Alfred
    echo $wf->toxml();

  }

}

 

 

I've tried a lot of things, like moving the echo and counting the length of the query to prevent it from search for the first two characters, but nothing has solved the flashing. I want to be able to tell the user if there is no result, but I don't want to show it if there is results. 

 

Anyone else had a similar problem? Or am I approaching this problem in the wrong way? I've seen workflows without these problems, but I haven't understood why it is happening to my workflow. (I also didn't find an answer in this forum when I browsed it.)

Link to comment

I've created a workflow to get search results from an url (JSON-RPC) and display it in Alfred. Everything works, but I can't find a way to prevent Alfred from briefly showing the "no result" first and then the actual results. Below is a very simplified version of my code. 

if (($json = @file_get_contents($my_url, false, $context)) !== false) { 

  $obj = json_decode($json);

  // check if there is any results
  if ($obj->result) {
  
    // do some stuff

    // prepare the result to return to Alfred
    $wf->result( 
      $uid, 
      $goto_url, 
      $title, 
      $subtitle, 
      'icon.png'
      );

    // echo the result as xml for Alfred
    echo $wf->toxml();

  } else {
    
    // set the "no result" to return to Alfred
    $wf->result( 
      'mysearch', 
      'na', 
      'No result', 
      'No result was found matching the search term.', 
      'icon.png',
      'no'
      );

    // echo the result as xml for Alfred
    echo $wf->toxml();

  }

}

 

 

I've tried a lot of things, like moving the echo and counting the length of the query to prevent it from search for the first two characters, but nothing has solved the flashing. I want to be able to tell the user if there is no result, but I don't want to show it if there is results. 

 

Anyone else had a similar problem? Or am I approaching this problem in the wrong way? I've seen workflows without these problems, but I haven't understood why it is happening to my workflow. (I also didn't find an answer in this forum when I browsed it.)

 

Adding this to the top of the php file or code doesn't fix it?

$in = "{query}";

if ( strlen( $in ) < 3 ):
   exit(1);
endif;

 

Obviously replacing {query} with $argv[1] if you are using a file..

 

That should make it exit the script and never even get to the point of processing it and generating output if the input is less than 3 characters

Link to comment

Adding this to the top of the php file or code doesn't fix it?

$in = "{query}";

if ( strlen( $in ) < 3 ):
   exit(1);
endif;

 

Obviously replacing {query} with $argv[1] if you are using a file..

 

That should make it exit the script and never even get to the point of processing it and generating output if the input is less than 3 characters

 

I actually did a "if (strlen($query) < 2)" but 3 works better, didn't think it would matter that much.

The other thing I found was that it was somewhat "by design" because for some search terms Alfred is just to quick :) For example, the terms "int" and "intel" produce results but "inte" does not. Because Alfred is quick to fire of the search for "inte" when I'm typing "intel" then Alfred first displays "no result" for a brief moment (because of "inte") and then shows the result from "intel".

Is there a way to tell Alfred not to be so trigger happy and just slow down a bit? :) I tried with sleep() but that just stalls the entire script (as it should). 

Link to comment

By the way, strlen() of "på" returns 4 in Alfred result and not 2, so it's not a good way to count characters. mb_strlen() also returns 4 unless I set mb_internal_encoding("UTF-8") first, then mb_strlen() returns 3. Unless I run my script from the command line, then I actually get 2 as I should. There's something odd with the encoding inside Alfred or my script. Or both. Or I don't know... :)

 

Edit: Oh, there was a online fix for that: $query = iconv("UTF-8-MAC", "UTF-8", $query);

Edited by Adrian
Link to comment

By the way, strlen() of "på" returns 4 in Alfred result and not 2, so it's not a good way to count characters. mb_strlen() also returns 4 unless I set mb_internal_encoding("UTF-8") first, then mb_strlen() returns 3. Unless I run my script from the command line, then I actually get 2 as I should. There's something odd with the encoding inside Alfred or my script. Or both. Or I don't know... :)

 

Edit: Oh, there was a online fix for that: $query = iconv("UTF-8-MAC", "UTF-8", $query);

 

Ah, nicely done sir. Gonna have to keep that one in mind for a few things. Thanks for sharing.

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