Jump to content

script filter order of execution


Recommended Posts

Hi all,

I have a script filter (argument optional) launching the simple python script below, but the JSON content is served to Alfred after the delay.

 

#!/usr/bin/python3 
import time
import json

result= {"items": [{
        "title": "Testing!",
        "subtitle": "testing script order of execution"

    }]}
    
print (json.dumps(result))

time.sleep (5)

 

... in fact, the second script below returns a garbage JSON error. How do I serve the 2 JSON strings sequentially? 

Thanks!

 

#!/usr/bin/python3 
import time
import json



result= {"items": [{
        "title": "Testing!",
        "subtitle": "testing script order of execution"

    }]}
    
print (json.dumps(result))

time.sleep (5)

result= {"items": [{
        "title": "Testing again!",
        "subtitle": "testing script execution"

    }]}
    
print (json.dumps(result))

 

Link to comment
1 hour ago, giovanni said:

but the JSON content is served to Alfred after the delay.

 

It’s served when the script ends, so the position of the delay does not make a difference. That is why in your World Cheater you can echo (in your case, cat a heredoc) sequentially and it works.

 

1 hour ago, giovanni said:

How do I serve the 2 JSON strings sequentially?

 

You can’t serve two JSONs, only one. The reason you’re getting an error is that while you are spitting out two valid JSONs individually, what Alfred is seeing is the concatenated text of those. Two valid JSONs stuck together are not a valid JSON string.

 

You can either add all your data at once (which I recommend; fewer moving parts) or you create a variable to hold your data as you modify it and print it at the end (which I do in the example, due to the timeout):

 

#!/usr/bin/env python3

import time
import json

result = []

result.append({
    "title": "Testing!",
    "subtitle": "testing script order of execution"
})

time.sleep (5)

result.append({
    "title": "Testing again!",
    "subtitle": "testing script execution"
})

print (json.dumps({ "items": result }))

 

Or perhaps what you’re thinking is:

 

#!/usr/bin/env python3

import time
import json

result = [{
    "title": "Testing!",
    "subtitle": "testing script order of execution"
}]

time.sleep (5)

result = [{
    "title": "Testing again!",
    "subtitle": "testing script execution"
}]

print (json.dumps({ "items": result }))

 

But that will only show the second one, which is expected.

 

If what you’re looking for is to show one JSON then wait and show a different one, you don’t sleep in your code but tell Alfred (via the JSON) to rerun it after a number of seconds. You may also need variables to hold some data (e.g. so the script knows if this is the first run or a subsequent one). The Getting Started → Advanced Script Filters Workflow which comes bundled with Alfred has examples of both.

Link to comment

thank you! I am updating to Python3 my Paperpile workflow and was trying to send 2 JSONs because occasionally the script needs to rebuild the underlying database and I wanted to send a message to let the user know (sleep was to simulate this operation). I ended up using the "Please wait" Subtext in the script filter which will get shown for appreciable time only if the script is rebuilding the database, and then is replaced by the query results. Thanks again!   

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