jamescridland Posted May 8 Share Posted May 8 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
Stephen_C Posted May 8 Share Posted May 8 The Alfred help page on Reading Environment Variables will probably be helpful in that context. Stephen Link to comment
jamescridland Posted May 8 Author Share Posted May 8 I think that's the other way round. I don't want to read a variable in the /bin/zsh script; I'd like to set a variable, to be read by a subsequent workflow block. Link to comment
Andrew Posted May 8 Share Posted May 8 @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
jamescridland Posted May 8 Author Share Posted May 8 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now