Jump to content

Last element of inline results displayed first


Recommended Posts

Posted

Howdy,

 

I just noticed a bug in an otherwise completed workflow that provides dictionary-based translations: the last element in the list keeps being placed first in the inline results. This is problematic since the first line provides the original language definition, which is then followed by lines presenting the target language translation; as such, the results are confusing if they're out of order. 

 

I've attempted a few small workarounds that seem to fix the problem temporarily (and lead me to believe I have it sorted), only to have the issue return after a small period of time. I'm not sure if it's a problem with the source website or what, but here's the code (it first checks to see if the API has already been entered by the user, then returns the translations if it has):


require_once('workflows.php');

$w = new Workflows('wordreference');
$id = $w->get('api','settings.plist');
if(!$id){
$w->result(
'wordreference-noapi',
'You must provide a WordReference API to use the workflow',
"Set your API with the 'setapi' keyword.",
'icon.png',
'yes'
);
echo $w->toxml();
return;
}
$query = urlencode( $argv[1] );
$translations = $w->request( $url );
$translations = json_decode( $translations );
$i=0;
foreach( $translations->term0->PrincipalTranslations as $translation ):
foreach( $translation as $trkey => $tr ):
$icon='icon.png';
if ($trkey == "OriginalTerm"):
$icon = 'france.png';
endif;
if ($trkey !== "Note"):
$w->result( 
$i, 
$query, 
$tr->term,
$tr->POS . " " . $tr->sense,  
$icon, 
'yes' );
endif;
endforeach;
endforeach;
echo $w->toxml();

 

 

Can anybody see what I'm doing wrong?

 

Thanks so much for your help,

Anthony

Posted

I think the problem here is that Alfred learns your most used results and sorts them by priority. You could try to use a random UIDs for your inline results. Since version 2.0.3 Alfred also supports leaving the UID empty. Check back with David if his PHP workflow handler supports this.

 

Either way will force Alfred to show the results in the pre-defined order without learning preferences.

Posted (edited)

Thanks for the feedback, but that leads me to another question: how would I go about leaving the UID empty in my PHP script? Is it a simple fix or do I have to restructure the script? I'm not an actual programmer so I'm really groping through the dark with this. 

 

Thanks in advance for the help!

 

 

ETA: Just in case it's necessary for knowing the right approach, the results the website sends look like this:

 

 

{
"term0" : {
"PrincipalTranslations" : {
    "0" :{
        "OriginalTerm" : { "term" : "chien", "POS" : "nm", "sense" : "animal domestique", "usage" : ""},
        "FirstTranslation" : {"term" : "dog", "POS" : "n", "sense" : ""},
        "SecondTranslation" : {"term" : "hound", "POS" : "n", "sense" : ""},
        "ThirdTranslation" : {"term" : "mutt", "POS" : "n", "sense" : "informal"}, "Note" : ""},
    "1" :{
        "OriginalTerm" : { "term" : "chien", "POS" : "nm", "sense" : "percuteur de pistolet", "usage" : ""},
        "FirstTranslation" : {"term" : "cock", "POS" : "n", "sense" : "pistol"},
        "SecondTranslation" : {"term" : "hammer", "POS" : "n", "sense" : "pistol"}, "Note" : ""},
    "2" :{
        "OriginalTerm" : { "term" : "chien", "POS" : "nm", "sense" : "personne pas aimable", "usage" : "argot"},
        "FirstTranslation" : {"term" : "bitch", "POS" : "n", "sense" : "female, pejorative"},
        "SecondTranslation" : {"term" : "dog, bullhound", "POS" : "n", "sense" : "male, pejorative"}, "Note" : ""}},
Edited by avk_tp
Posted

When Carlos mentioned leaving the UID empty he means in the output for items. So for 

 

<items>

  <item>

  </item>

</items>

 

do NOT place a UID such as <item UID=>

 

BUT, when mk suggests defining a random UID for EACH entry, this WILL force Alfred to list each item the exact way you want it to, no matter which entry is clicked on or selected in the results list.

 

For instance: in terminal/BASH you can simply type "uuidgen" in terminal and a totally unique number is generated.

 

So, let's say I have a bash script that outputs the list as such (notice the item line with the uid=):

 

 

echo "<?xml version=\"1.0\"?>
<items>
  <item uid=$randomnumber1 arg='argument here' valid='yes'>
<title>Title Goes Here</title>
<subtitle>Subtitle Goes Here</subtitle>
<icon>icon.png</icon>
  </item>
</items>
 
in my bash script I would generate the number with:
 
randomnumber1=$(uuidgen)
 
This means, the script makes a random number called "randomnumber1", and I refer to that generated number in the output with "$randomnumber1"
 
Now, I'm not sure how that plays out with PHP, because I have no idea how PHP works ... but MAYBE with the info provided it might be easier for you to find the answers online?
Posted

 

When Carlos mentioned leaving the UID empty he means in the output for items. So for 

 

<items>

  <item>

  </item>

</items>

 

do NOT place a UID such as <item UID=>

 

BUT, when mk suggests defining a random UID for EACH entry, this WILL force Alfred to list each item the exact way you want it to, no matter which entry is clicked on or selected in the results list.

 

For instance: in terminal/BASH you can simply type "uuidgen" in terminal and a totally unique number is generated.

 

So, let's say I have a bash script that outputs the list as such (notice the item line with the uid=):

 

 

echo "<?xml version=\"1.0\"?>
<items>
  <item uid=$randomnumber1 arg='argument here' valid='yes'>
<title>Title Goes Here</title>
<subtitle>Subtitle Goes Here</subtitle>
<icon>icon.png</icon>
  </item>
</items>
 
in my bash script I would generate the number with:
 
randomnumber1=$(uuidgen)
 
This means, the script makes a random number called "randomnumber1", and I refer to that generated number in the output with "$randomnumber1"
 
Now, I'm not sure how that plays out with PHP, because I have no idea how PHP works ... but MAYBE with the info provided it might be easier for you to find the answers online?

 

 

Thanks so much. I had been trying to get the uid to be empty as per Carlos' suggestion (the best I could come up with was setting it to "' '", which only fixed the problem temporarily), but generating a random number for it instead was comparatively easy to figure out. This seems to have fixed the problem, even with me purposefully trying to throw the ordering out of whack. 

 

Thanks everyone!

Posted

Thanks so much. I had been trying to get the uid to be empty as per Carlos' suggestion (the best I could come up with was setting it to "' '", which only fixed the problem temporarily), but generating a random number for it instead was comparatively easy to figure out. This seems to have fixed the problem, even with me purposefully trying to throw the ordering out of whack. 

 

Thanks everyone!

 

I don’t know PHP but it seems you let a space between quotes and you shouldn’t.

 

In AppleScript you can create an empty string using two double quotes without any space between them:

Set x to ""

In the XML the UID could be x (an empty string) and then Alfred would know that you want to keep the order.

Posted

I don’t know PHP but it seems you let a space between quotes and you shouldn’t.

 

In AppleScript you can create an empty string using two double quotes without any space between them:

Set x to ""

In the XML the UID could be x (an empty string) and then Alfred would know that you want to keep the order.

 

You're totally right: I should've left the space out to create an empty string. Unfortunately it doesn't resolve the issue, however, as Alfred still changes the order. I also tried declaring the uid without setting any value whatsoever, but that didn't correct the problem either. For some reason only generating a random variable seems to work. 

Posted

You're totally right: I should've left the space out to create an empty string. Unfortunately it doesn't resolve the issue, however, as Alfred still changes the order. I also tried declaring the uid without setting any value whatsoever, but that didn't correct the problem either. For some reason only generating a random variable seems to work. 

 

How about I just try to get a few minutes today to update the Workflows class and repost it to my blog? :)

Posted

How about I just try to get a few minutes today to update the Workflows class and repost it to my blog? :)

 

That would be great, thanks!

Posted

That would be great, thanks!

 

Check my blog/github. It should be resolved now. uid can be left blank to let alfred generate it. blank autocomplete values aren't added into the xml.

Posted

Check my blog/github. It should be resolved now. uid can be left blank to let alfred generate it. blank autocomplete values aren't added into the xml.

 

Thanks a bunch. That fixed it.

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