Jump to content

Setting variables in a python script doesn't work


Recommended Posts

I'm new to making workflows using python scripts. I am trying to set som variables inside a python file that runs through the the "run scripts" block. However I can't get it to work, and I don't know what I'm doing wrong.

import sys
import re
import json

query = sys.argv[1]

data = {
    "alfredworkflow": {
        "arg": "",
        "config": {
        },
        "variables": {
            "a": "test"
        }
    }
}

json.dump(data, sys.stdout)

 

The python file outputs isn't able to parse the JSON, and instead just outputs it as a string: "{"alfredworkflow": {"arg": "", "config": {}, "variables": {"a": "test"}}}". And when I use {var:a} in a notification block, it's empty. 

[16:32:12.639] Notion quick add[Keyword] Processing complete
[16:32:12.640] Notion quick add[Keyword] Passing output 'input' to Run Script
[16:32:12.695] Notion quick add[Run Script] Processing complete
[16:32:12.714] Notion quick add[Run Script] Passing output '{"alfredworkflow": {"arg": "", "config": {}, "variables": {"a": "test"}}}input
' to Post Notification

 

Link to comment

Welcome @villi,

 

You have unnecessary code your snippet. Unless input is supposed to come into play. But if it is, as @giovanni noted it’s not clear what it’s supposed to refer to.

 

With the given information, this should work:

 

import sys
import json

data = {
  "alfredworkflow": {
    "variables": { "a": "test" }
  }
}

json.dump(data, sys.stdout)

 

Link to comment
9 hours ago, vitor said:

Welcome @villi,

 

You have unnecessary code your snippet. Unless input is supposed to come into play. But if it is, as @giovanni noted it’s not clear what it’s supposed to refer to.

 

With the given information, this should work:

 

import sys
import json

data = {
  "alfredworkflow": {
    "variables": { "a": "test" }
  }
}

json.dump(data, sys.stdout)

 

 

 

@vitor Thanks!

Don't know what the difference between your code and mine was, but it somehow works now! 

Link to comment
  • 1 month later...

Legends!

 

Thanks so much for posting this here. I've been looking for docs on the JSON object for the workflow for ages and no amount of Googling yielded anything until I found this solution which worked a treat. It's incredibly useful for validating and calculating variables which can be used later in the workflow. Thanks again.

Link to comment

@Sam Michel As a heads-up alongside Vítor's response (for you and anyone else who comes across this thread), every workflow object has a question mark icon in the configuration, which will take you to the documentation for that specific object :) This is the best starting point for insight on configuring that particular workflow object!

 

Screenshot 2022-07-15 at 14.40.00.png

Link to comment
  • 2 weeks later...

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