Jump to content

How to create multiple arg variables from Run script python workflow?


Recommended Posts

I was wondering how to create multiple argument variable using python.

After hours of googling I found this link:

 https://www.deanishe.net/post/2018/10/workflow/environment-variables-in-alfred/

 

and I figured out myself.

 

# Create env variables

```python

 

import sys

s = "apple banana; car truck"
lst = s.split(';')

a = lst[0]
b = lst[1]


myjson = """
{"alfredworkflow": {
    "arg": "",
    "variables": {"a": "%s",
                  "b": "%s"
                 }
   }
}
""" % (a,b)

myjson = myjson.strip()

sys.stdout.write(myjson)

```

 

#  Use env variables

```python

import sys,os

arg1 = os.getenv('b')

changed = arg1 + ' called from another object'

sys.stdout.write(changed)

```

Edited by Bhishan
added additional links
Link to post
3 hours ago, Bhishan said:

After hours of googling I found this link:

 

It's literally stickied at the top of this forum …

 

3 hours ago, Bhishan said:

myjson = """
{"alfredworkflow": {
    "arg": "",
    "variables": {"a": "%s",
                  "b": "%s"
                 }
   }
}
""" % (a,b)

myjson = myjson.strip()

sys.stdout.write(myjson)

 

This is a really bad way to create JSON. It's almost impossible to be sure it will be valid. Use the built-in Python module:

import json
import sys

lst = ['some data', 'some more data']

data = {
    'alfredworkflow': {
        'arg': '',
        'variables': {
            'a': lst[0],
            'b': lst[1],
        },
    }
}

json.dump(data, sys.stdout)

 

Link to post

Thanks for the suggestion of use of json. I appreciate it.

 

What I meant by spending hours is, I saw your solution, but tried to make it work in python alone and it took me hours.

 

There is much to absorb in the given tutorial (python, applescript and so many things) but not a single stand alone python workflow which is kind of complete.

So, I tried to implement the workflow in python borrowing all the ideas from that link. The link is helpful but its broad and takes some time to filter out.

 

Link to post

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