Jump to content
Bhishan

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

Share this post


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)

 

Share this post


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.

 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...