Page 1 of 1
Process Switch Variables
Posted: Thu Oct 16, 2025 1:16 pm
by JimmyHartington
I am trying to create a script, which can recieve HTML input in various ways.
- Single line text with variables
- Multi line text with variables
When using the Single/multi line text with variables a variable as [Job.NameProper] or [Job.PrivateData:Key="UUID"] gets replaced with the correct values.
But if I read a static file with this content in my script the variables are not replaced.
Code: Select all
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<p><strong>Name: </strong>[Job.NameProper]</p>
</body>
</html>
I can not seem to find in the documentation for scripting if this is possible.
What would be a dream scenario is to have a Process Switch Variables Yes/No list.
Re: Process Switch Variables
Posted: Thu Oct 16, 2025 3:15 pm
by freddyp
In the case of something with variables, it is Switch that evaluates the input before passing it to your script. In the case of a static file, Switch does not read the file, it only gives you the path. It is your script that reads the file, so any string replacements in the file are up to your script. You can do what you want by searching with a regular expression that returns a list of matches and you loop over the matches to replace them.
You could also use the "String Replace" app: inject your static file, send it through "String Replace", pick it up as a dataset and use the dataset in your script.
Note that in either case you can also work with your own variable strings in the static file, they do not have to be Switch variables.
Re: Process Switch Variables
Posted: Thu Oct 16, 2025 3:25 pm
by JimmyHartington
So there is no way in a script to use a Switch class or function to take the string and replace all Switch Variables?
Re: Process Switch Variables
Posted: Thu Oct 16, 2025 6:12 pm
by freddyp
No.
Re: Process Switch Variables
Posted: Tue Oct 21, 2025 9:12 pm
by tdeschampsBluewest
I think it's doable, but it will depend on how confident you are about catching the variable inside your HTML.
If it’s a known list of variables in advance, it’s really easy you can catch the pattern(s) with a regex, then evaluate the result.
If it’s not known in advance, you’ll need a more generic regex that can detect them dynamically, but that can lead to many edge cases depending on what’s inside the brackets (especially if the pattern itself includes brackets).
Example:
Code: Select all
async function evaluateVariable(job: Job, string: string): Promise<string> {
const result = await job.getVariableAsString(string);
return result;
}
async function replaceVariablesInHtml(job: Job, html: string): Promise<string> {
const regex = /\[(?:Job|Metadata|Doc)\.[^\]]+\]/g; //enhance the regex as needed or catch specific pattern
return html.replace(regex, (match) => {
const evaluatedValue = await evaluateVariable(job, match);
return evaluatedValue;
});
}
async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {
const html = `<!DOCTYPE html>
<html>
<body>
<h1>'[Metadata.Rational:Dataset="JSON",Model="JSON",Path="path/to/my/value"]</h1>
<p>My first paragraph.</p>
<p><strong>Name: </strong>[Job.NameProper]</p>
</body>
</html>
`;
const output = await replaceVariablesInHtml(job, html);
}
Bonus:
Here’s a
TypeScript Playground example.
It’s basically the same logic, but without the Switch context, instead, it replaces matches with a random number (since we can’t evaluate variables there).
Re: Process Switch Variables
Posted: Wed Oct 22, 2025 7:50 am
by JimmyHartington
Hi Thomas
Thanks. I will look into it, but hoped there would be a way to have Switch just do the replacement of variables on my data the same way it happen with Multiline with variables.
So a function baked into the Switch scripting environment.
Re: Process Switch Variables
Posted: Wed Oct 22, 2025 9:23 am
by tdeschampsBluewest
Yeah, as Freddy mentioned, unfortunately Switch doesn’t offer a simple solution for that.
It got me thinking, so I’ve started building the foundation for an app with a more robust approach. If that sounds interesting, feel free to reach out directly at
appstore@bluewest.fr, I’d be happy to discuss your specific needs and how it could help.