Jump to content

[SOLVED] Workflow Concept - Calling API once and showing response as list item


Recommended Posts

I am looking to implement a workflow that calls an API after a user has entered some text. In order to not reach certain API call limits, the callout should only be performed once the user presses Enter (one time - not after every key stroke). The webservice will then return a response to be displayed to the user as an Alfred list item. The user may press Enter on the displayed list item to open a certain URL that the API responded with.

 

This is the concept I came up with (note: No workflow that I can export yet):
1. [Keyword] User enters a keyword and then some text (e.g. 'mywf some text')
2. User presses 'Enter'
3. [Run Script] API is called
4. [Run Script] API returns response, passed as input to Script Filter
5. [Script Filter] Response is shown as list items in Alfred

  • Issue 1: Step 2 would close the Alfred launcher, I want to keep it open to wait for the response via 4. My assumption is, this would only possible using a Script Filter at 1? Unfortunately, this would hit the API call limits.
  • Issue 2: As Alfred is closed at 2, and reopened at 4, the "input" field of Alfred would be cleared of the user's input (as Run Script ended), and instead the output of the previous node would be shown (Run Script). However, as this output is a JSON string - to show the list items in Alfred -, this would now show as text in the input field (i.e. { "items" ... }).

6. [Script Filter] User presses 'Enter' on selected list item
7. [Script Filter] URL is opened in browser

 

Due to Issue 1 & 2 this approach does not seem to work in theory. My questions are:
1) Is there any way to only execute a Script Filter upon pressing 'Enter'? This would allow me to have a Script Filter only, instead of an additional Run Script filter before.
2) How can I keep the Alfred launcher open due all of this?
3) How can I prevent the Alfred launcher input field from being filled with the output of the previous node?

Edited by leahcim
Link to comment
8 minutes ago, leahcim said:

1) Is there any way to only execute a Script Filter upon pressing 'Enter'? This would allow me to have a Script Filter only, instead of an additional Run Script filter before.

 

No, but some people used to make Script Filters in a way they would only run after you typed a period. Make your script exit early unless the last character is a period. When the caracter is a period, strip it from the argument and run the rest of the script.


But a better way would be to click on Run Behaviour in your Script Filter window and change Queue Delay.

 

15 minutes ago, leahcim said:

2) How can I keep the Alfred launcher open due all of this?

 

After you connect two nodes, you’ll see some connecting lines will have a circle in the middle. Double-click it and tick Don’t close the Alfred window on actioning result to keep the window open.

 

15 minutes ago, leahcim said:

3) How can I prevent the Alfred launcher input field from being filled with the output of the previous node?

 

Stick an Argument and Variables Utility node in between them and delete the Argument.

Link to comment

Thank you, @vitor!

On 1/27/2020 at 12:37 AM, vitor said:

No, but some people used to make Script Filters in a way they would only run after you typed a period. Make your script exit early unless the last character is a period. When the caracter is a period, strip it from the argument and run the rest of the script.

 

I have seen that as well. Unfortunately, that seems a bit cumbersome from a user's perspective - as a user, I simply want to hit 'Enter' once I am done entering my text. As a developer, I don't know whether the user is done - or just waiting a few seconds.

 

On 1/27/2020 at 12:37 AM, vitor said:

After you connect two nodes, you’ll see some connecting lines will have a circle in the middle. Double-click it and tick Don’t close the Alfred window on actioning result to keep the window open.

 

Good to know!

 

On 1/27/2020 at 12:37 AM, vitor said:

Stick an Argument and Variables Utility node in between them and delete the Argument.

That works, good to know as well - however, this also means that my Script Filter no longer has any input to display the list item, as {query} is now empty. Is there a way to have the Script Filter use the {query} to display a list item, but without populating Alfred's input box? Here is an example workflow: Example Workflow - v1 After the Args&Vars node, {query} will be empty (as intended, to clear the input field) - unfortunately, this also means the Script Filter receives no input for showing the list items:

14:25:12.385] MyWF[Run Script]  Passing output '{
	"items": [
		{
			"title": "Created task - test",
			"valid": true,
			"arg": "www.test.de",
			"icon": {
				"path": "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarInfo.icns"
			}
		}
	]
}
' to Arg and Vars
[14:25:12.388] MyWF[Arg and Vars] Processing complete
[14:25:12.389] MyWF[Arg and Vars] Passing output '' to Script Filter

I've tried storing the {query} value before clearing via Args&Vars in a variable, and then use this to iterate over in Script Filter to show it as list items, but it did not work, as I run into the same problem again that the input field is populated with JSON - see here for an example: Example Workflow - v2

Edited by leahcim
Link to comment
1 hour ago, leahcim said:

Is there a way to have the Script Filter use the {query} to display a list item, but without populating Alfred's input box?

1 hour ago, leahcim said:

I've tried storing the {query} value before clearing via Args&Vars in a variable, and then use this to iterate over in Script Filter to show it as list items

 

That’s the way. Your problem is in how you’re doing it.

 

Get rid of the Run Script; you need to run the code in the Script Filter. Connect your Keyword to an Arg and Vars, saving {query} to a variable and making Argument empty. Connect that to the Script Filter and make that run the code, but as an argument give it the variable you saved instead of {query} or a positional argument ($1).

Link to comment

Understood. Thanks, @vitor. I will give it a try! However, this also means that. I have to go with the 'hacky' way of using a dot to end the command. I'll have to try whether I can add a high delay (e.g. 5 seconds) but still allowing the user to press enter immediately. That might do the trick. 

Link to comment
6 minutes ago, leahcim said:

However, this also means that. I have to go with the 'hacky' way of using a dot to end the command.

 

No, it doesn't. You collect the user's query with a Keyword element, pass it to a Script Filter as a variable, and just use the Script Filter to retrieve and show the results.

Link to comment
15 hours ago, deanishe said:

No, it doesn't. You collect the user's query with a Keyword element, pass it to a Script Filter as a variable, and just use the Script Filter to retrieve and show the results.

 

Good point. Having a Keyword node and a Script Filter node would mean that

  • the user enters the keyword and text
  • the input is passed to the Script Filter
  • the user is still able to enter/change his input, causing the Script Filter to be re-started for each keystroke, causing multiple callouts

Is there a way to block the input field in the Script Filter? I've tried setting an environment var that is changed when the script is called, however as the Script Filter is restarted with each keystroke, also this var's value is reset...

Edited by leahcim
Link to comment
3 hours ago, leahcim said:

the user is still able to enter/change his input

 

No, they aren’t, because you’re not giving their changing input as the argument to the script, you’re giving the fixed value from the variable you set up.

 

That would call the Script Filter with the same input on each keystroke, but is easily avoided by ticking the Alfred filters results checkbox, making it run only once.

Link to comment
2 hours ago, vitor said:

 

No, they aren’t, because you’re not giving their changing input as the argument to the script, you’re giving the fixed value from the variable you set up.

 

That would call the Script Filter with the same input on each keystroke, but is easily avoided by ticking the Alfred filters results checkbox, making it run only once.

Yes, that is what I meant - the user could "change" his input i.e. repeat it by typing anything.

 

I found out about the checkbox as well, its effect is well hidden!

 

Thanks a lot, all is working now - API gets called only once, user is redirected to web page where their input is displayed.

Link to comment
  • vitor changed the title to [SOLVED] Workflow Concept - Calling API once and showing response as list item

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