Jump to content

Handle response from script filter


Recommended Posts

I have a workflow in Alfred that uses a python script I have written. The workflow shows the output from the python script in Alfred. However, the input to the python script being executed can sometimes return "null". In that case, I want the workflow to take another path. 

 

The normal output is like this:

{
    "items": [
        {
            "arg": "123",
            "title": "car",
            "subtitle": "car with id: 123",
            "icon": {
                "type": "filetype",
                "path": "/Users/ml/car.png"
            }
        },
        {
            "arg": "456",
            "title": "boat",
            "subtitle": "board with id: 456",
            "icon": {
                "type": "filetype",
                "path": "/Users/ml/boat.png"
            }
        }
    ]
}

 

In Alfred, car and boat is displayed and if car is selected I trigger another script filter with the id from the car, 123, as input to the python script and display the result of that. But if the result in first case is not car and boat, but instead:

 

{
    "items": [
        {
            "arg": "null",
            "title": "N/A",
            "subtitle": "N/A",
            "icon": {
                "type": "filetype",
                "path": "/Users/ml/null.png"
            }
        }
    ]
}

 

Then N/A is displayed in Alfred. But in this case, I want to take another path to to trigger my python script with another set of arguments. How can I do that in Alfred?

Edited by raeserbil
Link to comment

Maybe I did not explain myproblem correctly. I have this script filter in my work flow.

 

4889857_Screenshot2021-10-07at15_34_19.png.f9a71c681f003a0dcba2399dfaa5cc68.png

 

The script filter executes this:

/usr/local/bin/myscript -id 789

 

If the output is 

{
    "items": [
        {
            "arg": "null",
            "title": "N/A",
            "subtitle": "N/A",
            "icon": {
                "type": "filetype",
                "path": "/Users/ml/null.png"
            }
        }
    ]
}

 

I do NOT want to show "N/A" as a selectable option in Alfred. Instead I want to do something else (trigger myscript with another set of arguments and show that output instead)

 

 

Link to comment
10 minutes ago, raeserbil said:

I do NOT want to show "N/A" as a selectable option in Alfred

 

Then you need to not output "N/A" to Alfred.

 

If you can't rewrite the original script to do something other than output "N/A", you'll need to put another script in between to filter it out and do something else instead.

 

Could you explain what you’re trying to achieve, not how you’re trying to do it? (I.e. I want to show X in Alfred if Y, otherwise Z, not this script does X, but I want it to do Y.) The way you describe what you're doing makes it sound like you don't have the right mental model of how Script Filters work.

 

Link to comment

I can output whatever I want in my script, it does not have to be N/A. I will try to explain what I am trying to achive:

 

I am totally new to Alfred Workflows and just trying to figure out how it works. 
What I want to achive is this:

 

I have a tree of 3 levels. For example:

- Soccer
-- England 
--- Div 1
--- Div 2
-- Italy
--- Div 1
-- Spain
--- Div 1
- Basketball
-- NBA
-- Turkey
--- Div 1
- Tennis
-- Grand Slam

 

All of these elements in the tree has a uniqe id. My python script takes the id as an argument and returns the child tree for the speciefied id.

 

For example:

/usr/local/bin/myscript -id 1 
# Returns all root tree levels, e.g. Soccer, Basketball, Tennis

 

/usr/local/bin/myscript -id 456
# id 456 for Soccer 
# Returns all tree on level 2 for Soccer, e.g. England, Italy, Spain

 

/usr/local/bin/myscript -id 789
# id 789 for England (in Soccer) 
# Returns all tree on level 3 for Soccer - England, e.g. Div 1, Div 2

 

/usr/local/bin/myscript -id 99999
# id 99999 does not have any sub trees
# Returns N/A

 

In my workflow I first use a script filter with argument 1 to get all root trees and display them in the result. If I then select Soccer I use antoher script filter with argument 456 to display 2nd level England, Italy, Spain. 

 

The problem is that there is not always 3 levels in the trees.

In this example, if I select Basketball and then NBA, then the script filter running my python script with argument 111 for NBA will return N/A and hence display N/A.

In this case, I do not want to display N/A. Instead, I want to execute my python script with an argument that returns for example all the teams in NBA. 

 

Of course, in the case that the selection only contains 2 levels, I can just display N/A and select that and then after that do what I want. But what I am trying to achive is to not display N/A.

 

Does this make sense?

Edited by raeserbil
Link to comment

Of course I can rewrite my python script to output what I want to display in this case, but I was hoping to avoid this and handle it in Alfred Workflow.

 

if ("output from my script used in the script filter" == "N/A"):
  call my script with another argument and dont display N/A
else:
  display output in Alfred result

 

Link to comment

Sorry, this doesn’t make sense. When the user selects a result from a Script Filter, Alfred passes arg to the next action, not back to the Script Filter script. If you want to drill down into a tree that way, you need to use an External Trigger to pass the ID back into the Script Filter.

 

11 minutes ago, raeserbil said:

The problem is that there is not always 3 levels in the trees

 

Right. You need to look at whether the current node has children or not.

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