Setting NSUserAutomatorTask variables without requiring Automator Workflows to declare that variable

I'm using `NSUserAutomatorTask` to launch a `.workflow` file, created via the Automator app in macOS 10.13.



I'm passing variables to the workflow via the `variables` property:



https://developer.apple.com/documentation/foundation/nsuserautomatortask/1418099-variables



The parent app is sandboxed. The script is located in the `.applicationScriptsDirectory` and runs successfully when variables are not set, or when the same variables are set from the app and declared in the workflow.



if let workflow = try? NSUserAutomatorTask(url: url) {

workflow.variables = ["randomVariable": "value"] // NOTE

workflow.execute(withInput: nil) { (value, error) in

if let error = error {

print(error) // No variable named "randomVariable"

}

}

}



The workflow fails to run with the error:



> No variable named "randomVariable"



However, if I edit the workflow and add a Variable to match the one set in code, everything is fine.



[![Add var to workflow][1]][1]

[1]: https://i.stack.imgur.com/gOWDL.png



I no longer get the error, and the workflow executes correctly:



[![Execute workflow][2]][2]

[2]: https://i.stack.imgur.com/CycAn.png



This is an issue because I want to pass multiple pieces of information as variables to any potential workflow, and for each workflow to individually decide to handle each needed parameter.



I do not want to *require* that every workflow declare the variables that my app will *optionally* provide.



Note that in my sample workflow the variable is never used. I do not want the additional requirement that it be declared.



Is there any way to avoid each workflow declaring the variables that my app passes when executing the workflow?



Or, is there a way to inspect which variables a workflow has declared? I could then only pass the ones actually used by the workflow.