Jump to content

Workflow Environment Variables questionGiulio


Recommended Posts

Hi,

 

I'm looking how can I specify Workflow Environment Variables to store data to use on the workflow itself.

 

what I have not still clear is how to change their values:

 

In the documentation one of the examples is the need for a user to specify his own API key for a service,

but how this will be done?

 

OK, he can click on 'configure workflow and variables' and change the value, but is this possible to use the workflow itself to ask for the API Key and store the new value?

if yes, how?

 

in the docs is said:

 

These variables can be overridden dynamically within a workflow with the JSON utility object, but overridden means temporarily or what?

I did try to set an  Environment Variable  called 'foo' with value 'aaaa', the put in the workflow a simple 'Args and Vars' object setting 'foo' to 'bbb', and after that displaying {var:foo} shows 'bbbb', but the Environment Variable 'foo' is not changed, so the change seems temporarily.

did try also a JSON object:

 

{
  "alfredworkflow" : {
    "arg" : "{query}",
    "config" : {
    },
    "variables" : {
      "foo" : "cccc"
    }
  }
}

and again the value of 'foo' is changed, but but the Environment Variable 'foo' is not changed ( did try also to change in the "config" section.

 

I have not seen on the examples something that stores new values on Workflow Environment Variables,

I'm sure I'm missing something.

 

thank you,

 

     Giulio

Edited by juliosecco
Link to comment

Yeah, I think the expectation is that users will use the configuration sheet to configure your workflow. You put the instructions in the About this Workflow box on the left, and the user has to fill in his/her API key/username/filepaths in the Workflow Environment Variables table. Alfred preserves a user's changes to the variables across workflow updates.
 
The Workflow Environment Variables are read-only defaults, to be overridden temporarily at runtime by JSON Config and Args and Vars Utilities. The only way to permanently change the values of the Workflow Environment Variables programatically is to edit info.plist. That's generally a last resort, however.
 
The usual way to persist user settings is to store them in the workflow's data directory, which is ~/Library/Application Support/Alfred 3/Workflow Data/<bundleid>, and available as the alfred_workflow_data environment variable.
 
Assuming you're using the Script box like a shell, the simplest way to persist settings is, I guess, to write their values to files in the data directory and load them from your workflow script. That would override the Workflow Environment Variables and any JSON Config/Args and Vars settings.
 
If you have multiple variables, you could write them in a file in "export" style and source it:
 
Script filter: 

myvar="The early bird catches the worm"
 
test -f "${alfred_workflow_data}/settings" && source "${alfred_workflow_data}/settings"
 
cat <<EOS
{"items": [
  {"title":"myvar", "subtitle":"$myvar"}
]}
EOS

settings file:

export myvar="But the second mouse gets the cheese"

If you only have one or two settings, you could just write their values to files and load them via cat:

test -f "${alfred_workflow_data}/myvar" && myvar="$( cat "${alfred_workflow_data}/myvar" )"

Link to comment

 The Workflow Environment Variables are read-only defaults, to be overridden temporarily at runtime by JSON Config and Args and Vars Utilities.

 

Hi deanishe,

 

OK, got it.

I was hoping to be able to change Environment Variables directly, but storing all in a file is fine anyway

 

thanks

Link to comment

Depending on what you're doing, maybe changing info.plist is the right way to go. If a user can set their API key via the configuration sheet or a workflow action, it would be confusing for the API_KEY field in the configuration sheet to be empty.

 

It's simple enough to change a variable with PlistBuddy: 

/usr/libexec/PlistBuddy -c 'Set :variables:API_KEY "api-key-goes-here"' info.plist
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...