Jump to content

Possible to pass variables from a /bin/zsh script to the next item?


Recommended Posts

I have a /bin/zsh script that pulls in an API from an external website with some numbers in it.

 

I'd like to assign these as variables for the next step of the workflow. Is that possible?

 

Currently I'm just sending one big block of text through as the {query}. But I'd like to have the bash script assign, for example, {var:subscribers} so I can then use that as a query in the next workflow block.

Link to comment

@jamescridland You can output JSON from your Run Script to tap directly into a workflow's connection stream. If you add a JSON Config utility, this will show you the format you need.

 

As an example, if I output the following from a Run Script, I can set the output arg to "cat" and a variable "animal" to "dog":

 

{
  "alfredworkflow" : {
    "arg" : "cat",
    "variables" : {
      "animal": "dog"
    }
  }
}

 

Link to comment

That did the trick, thank you!


To help others, this code takes a simple API which gives four nicely-formatted numbers on separate lines, and makes them available as variables.
 

# fetch contents of URL and save them to variable "count"
count="$( curl -fsSL https://www.example.com/api )"
a=("${(f)count}")
# output to next action

echo -en '{
  "alfredworkflow" : {
    "arg" : "egg",
    "config" : {
    },
    "variables" : {
     "subscribers" : "'
echo -e $a[1]'"'
echo -n ',
    "views" : "'
echo -e $a[3]'"'
echo -n ',
    "downloads" : "'
echo -e $a[2]'"'
echo -n ',
    "pbjsubscribers" : "'
echo -e $a[4]'"'
echo -n '
    }
  }
}'

 

... I'm sure there's a nicer way of doing the above, but it works for me!

They're then available to the next building block (copy-to-clipboard and paste) as {var:subscribers} as one example.

 

 

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