Jump to content

How to - type query, press enter, get results .... and repeat?


Recommended Posts

I am trying to create a Gif Search workflow. I know others exist, but I wanted the practice. 

 

The idea I am going for is the following. User types "gif" followed by a query. When they are satisfied with their query, they press enter and the results are displayed. The user can now hover over a result and press Enter a second time to copy the Gif url to the clipboard. The user can also search using a different query by simply modifying the text in the Alfred search bar and then pressing Enter to refresh the results. For example, if they initially typed "gif dog", then that text will still be in the search bar, and they can now change it to "gif dog happy" and press Enter to get new results. The reason for the user needing to press enter after to trigger the script is so the API is not being hit for each new keystroke (I don't want to make API calls for "d", then "do", then "dog", only for "dog").

 

So far, I have achieved the following (best seen by looking at the workflow linked below):

  • User types "gif" followed by a query. When they are satisfied with their query, they press enter (handled by an Alfred Keyword element).
  • Output from Keyword element is passed to an Args & Vars element. Here the users query is assigned to a variable called "search".
  • Next, an external python script (giphy.py) is run via a Script Filter. This filter grabs the value in the search variable and makes an API call to Giphy. It then downloads thumbnail images of the Gifs if they are not already downloaded and stored in the users Cache directory. Finally, it outputs the results to Alfred as a JSON object.
  • Once the results of the API call are displayed as a list, you can preview the Gifs by pressing Shift and you can copy a link to the Gif by pressing Enter. Great!

 

Now for the problems / questions I have:

  • After the users initial query, the Alfred search bar is cleared. I would like the previous text of "gif dog" to remain in the search bar.
  • If the user types something else into the search bar, the results of the first text are filtered. Instead, what I want is for all results to remain displayed to the user as the new text/query is typed. When the user presses enter after typing a new query, the results are refreshed. 

 

Here is the workflow I have come up with so far. Any suggestions are greatly appreciated. 

 

Thanks!

 

 

Link to comment

Hi @Espresso, welcome to the forum.

 

2 hours ago, Espresso said:

After the users initial query, the Alfred search bar is cleared. I would like the previous text of "gif dog" to remain in the search bar.

 

Remove the Arg & Vars (or set Argument to {query}). The Script Filter’s initial input is arg/query.

 

2 hours ago, Espresso said:

When the user presses enter after typing a new query, the results are refreshed.

 

You're not going to be able to get that to work really well because Alfred doesn't work that way. A Keyword always waits until you press ↩, but it doesn't show any results, while a Script Filter shows results, but it doesn't wait for you to press ↩.

 

You could add a secondary action to your Script Filter results (e.g. ⌘↩) that sends you back to the Keyword, and leave the Script Filter to filter the search results (like it does now).

 

Alternatively, you could discard the Keyword and use a query suffix to trigger the search (instead of ↩). So, your script doesn’t search for anything unless the query ends with, for example, .

 

Looking at the code, you should consider using the workflow’s own cache directory (environment variable alfred_workflow_cache).

 

Link to comment

@Espresso

Edit:

Here's your workflow modified to do exactly what you were aiming for: https://drive.google.com/file/d/1eX_pa_E3thrMvdKVKzYDpjrKyOT2oLyS/view?usp=sharing

I haven't edited the python script at all, just re-arranged and edited some stuff in the workflow editor.

Now it looks like this:

image.thumb.png.4b7043638869379afdb3e8ae92838237.png

 

For a crude explanation of how it works, refer below:

 

Before edit:

The following workflow does precisely what you've described:

https://github.com/mr-pennyworth/alfred-gif

 

I know you want to write your own, so, you can take a look at this workflow and see how it is done and do the same in your own workflow. Here's how it achieves the behavior you described:

 

image.thumb.png.f2440bcf962b8fb90b62409e52d6ea2c.png

  1. This is a script filter. As @deanishe mentioned, in a script filter, you can't do wait-for-enter. So, I do this hack instead:image.thumb.png.48be458e086b259f0402cf027f2a5d3a.png
    The script filter basically does no work except for storing the query in a file. When the user presses enter, (2) and (3) are activated simultaneously.
  2. Returns immediately with a "search in progress..." message.
  3. Gets results from the gif website, returns a message about the gif.
  4. The messages from (2) or (3) are stored in an environment variable.
  5. Re-populate Alfred's search box with the query that was just executed.
Edited by Mr Pennyworth
Added edited workflow
Link to comment
19 hours ago, Espresso said:

The reason for the user needing to press enter after to trigger the script is so the API is not being hit for each new keystroke (I don't want to make API calls for "d", then "do", then "dog", only for "dog").

 

That’s exactly what the Queue Delay option in the Run Behaviour section of Script Filters is for.

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