Jump to content

Advice on returning result of script in Alfred?


Recommended Posts

I had been using an extension in Alfred 1 that did conversions. I found it to be one of the most flexible extensions I found for doing conversions. It always knows what I mean.
 
It is based on this java library: https://github.com/gnapse/metric The author of the java library originally posted the extension, but his blog is no longer online. I have been working on converting it to Alfred 2, though, and he said he didn't mind if I posted my workflow here for advice.
 
It also does currency, but I discovered that sometime between when I began using the extension and now the provider of the currency info (OpenExchangeRates) had switched to requiring an App ID. So the versions of the workflow I'm going to post won't do currency conversion. However, I've found you can get a free App ID for personal use, so I've asked the author of the java library if he could add something that would let one pass an App ID so folks could sign up for their own. He said he would take a look. However that's not required for my question.
 
The original extension simply called a brief bash script to pass your query to the java library and return a result to a notification. I first recreated that easily in an Alfred workflow: http://cl.ly/0J2f0A163y0Q

 

Now I'm trying to make it return results in the Alfred window. I've actually got a version of it working already, but it could use some polishing: http://cl.ly/013j3V1L0P0X

 

The trouble is how to handle the result when you are still typing. At first it returned errors in the Alfred window before your query was complete. Harmless, but ugly! The correct result will appear shortly after you finish typing. So far, I've managed to suppress the errors from going to stdout, so now it shows nothing for the second line if the library doesn't return a valid result. I haven't figured out how to put alternate text there, such as "Waiting for valid query" or some such.

 

So I'm looking for advice on what I can do to provide the nicest output experience. It seems like Alfred starts calling the script shortly after you start typing following the keyword and continues to call it repeatedly as the typed query changes.

 

As I see it, there are two options. Try to make a pause before the script kicks off, in hopes that you'd be done typing by the time it starts. However my concern is making it too long of a pause and slowing things down.

 

Second would be to try to return some friendlier text instead of the errors. You might still get a valid result before you are finished typing. For example typing "15 g in pounds" might return "15 grams = 15 grams" if the script processes after you type the g, but it does update to the desired result when you finish typing.

 

It's possible this is more of a bash question than an Alfred one, but I'd welcome any feedback/suggestions.

Link to comment

I had been using an extension in Alfred 1 that did conversions. I found it to be one of the most flexible extensions I found for doing conversions. It always knows what I mean.

 

It is based on this java library: https://github.com/gnapse/metric The author of the java library originally posted the extension, but his blog is no longer online. I have been working on converting it to Alfred 2, though, and he said he didn't mind if I posted my workflow here for advice.

 

It also does currency, but I discovered that sometime between when I began using the extension and now the provider of the currency info (OpenExchangeRates) had switched to requiring an App ID. So the versions of the workflow I'm going to post won't do currency conversion. However, I've found you can get a free App ID for personal use, so I've asked the author of the java library if he could add something that would let one pass an App ID so folks could sign up for their own. He said he would take a look. However that's not required for my question.

 

The original extension simply called a brief bash script to pass your query to the java library and return a result to a notification. I first recreated that easily in an Alfred workflow: http://cl.ly/0J2f0A163y0Q

 

Now I'm trying to make it return results in the Alfred window. I've actually got a version of it working already, but it could use some polishing: http://cl.ly/013j3V1L0P0X

 

The trouble is how to handle the result when you are still typing. At first it returned errors in the Alfred window before your query was complete. Harmless, but ugly! The correct result will appear shortly after you finish typing. So far, I've managed to suppress the errors from going to stdout, so now it shows nothing for the second line if the library doesn't return a valid result. I haven't figured out how to put alternate text there, such as "Waiting for valid query" or some such.

 

So I'm looking for advice on what I can do to provide the nicest output experience. It seems like Alfred starts calling the script shortly after you start typing following the keyword and continues to call it repeatedly as the typed query changes.

 

As I see it, there are two options. Try to make a pause before the script kicks off, in hopes that you'd be done typing by the time it starts. However my concern is making it too long of a pause and slowing things down.

 

Second would be to try to return some friendlier text instead of the errors. You might still get a valid result before you are finished typing. For example typing "15 g in pounds" might return "15 grams = 15 grams" if the script processes after you type the g, but it does update to the desired result when you finish typing.

 

It's possible this is more of a bash question than an Alfred one, but I'd welcome any feedback/suggestions.

 

You are correct in that, Alfred fires a script filter as soon as you start entering data (after the keyword) and does fire at every character until your done.

 

The alternate subtext is only shown when waiting on feedback from the script. So if the script fires back a result really fast, you may not ever see it. 

 

There isn't currently a way to pause the execution of the script for a certain amount of time, Only firing when the input reaches a certain length is how many others are doing it. I could see how that causes issues for you though. It seems that the script would need to be edited to look and make sure that it has all it needs. If it doesn't, it can return nothing or it can return a feedback item with the valid flag set to 'no' informing the user that it's still waiting on more data. So if it hasn't found two amounts and two units, return nothing or return some form of feedback saying that it's missing something. Once it sees that it has all that data, then return the actual feedback item that it should.

 

Does all that make sense? Let me know if you still need help or have other questions.

Link to comment

You are correct in that, Alfred fires a script filter as soon as you start entering data (after the keyword) and does fire at every character until your done.

 

The alternate subtext is only shown when waiting on feedback from the script. So if the script fires back a result really fast, you may not ever see it. 

 

There isn't currently a way to pause the execution of the script for a certain amount of time, Only firing when the input reaches a certain length is how many others are doing it. I could see how that causes issues for you though. It seems that the script would need to be edited to look and make sure that it has all it needs. If it doesn't, it can return nothing or it can return a feedback item with the valid flag set to 'no' informing the user that it's still waiting on more data. So if it hasn't found two amounts and two units, return nothing or return some form of feedback saying that it's missing something. Once it sees that it has all that data, then return the actual feedback item that it should.

 

Does all that make sense? Let me know if you still need help or have other questions.

Would it be possible to grab the input, and run an if () else () on the input if it's greater than or less than a certain number of characters (assuming that the length could provide you enough time for this). Just an idea, not sure if it would solve the problem here.

Link to comment

Would it be possible to grab the input, and run an if () else () on the input if it's greater than or less than a certain number of characters (assuming that the length could provide you enough time for this). Just an idea, not sure if it would solve the problem here.

 

That's somewhat what I had suggested above. Several users have done this for workflows that require an internet connection. If the length of the input string is less than 3 characters, don't fire (returning no results). Once you hit the 3rd character, start searching. By waiting a little longer for a longer string length, you reduce the size of the result site from the external resource and typically speeds things up a little.

Link to comment

Thanks for the tips. I think it would be hard to evaluate whether or not the entry is complete, at. The length would vary depending on what you wanted to convert. But some minimum number of characters that's not too high would probably help delay it a bit

Link to comment

Sorry, fumbled the submit button too soon. I was going to say:

 

I think it would be hard to evaluate whether or not the entry is complete. The length would vary depending on what you wanted to convert. So, I don't expect to be able to make it perfect, just trying to make it a little more attractive. But some minimum number of characters that's not too high would probably help delay it a bit. I still think that I may also be able to tweak the bash script to return an alternate string if there's an error returned. Anything more elegant would probably have to be done in the Java library, and I don't know Java.

 

The only "problem" with returning nothing on an error, as I currently have it, is that visually it makes the text in the Alfred jump around a bit based on whether there is a second line or not, which I find irksome. My husband is good with bash, so I'll ask him about that.

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