Process Switch Variables

Post Reply
User avatar
JimmyHartington
Advanced member
Posts: 492
Joined: Tue Mar 22, 2011 7:38 am

Process Switch Variables

Post 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
  • Static file
  • Incoming job or
  • Dataset
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.
freddyp
Advanced member
Posts: 1158
Joined: Thu Feb 09, 2012 3:53 pm

Re: Process Switch Variables

Post 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.
User avatar
JimmyHartington
Advanced member
Posts: 492
Joined: Tue Mar 22, 2011 7:38 am

Re: Process Switch Variables

Post 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?
freddyp
Advanced member
Posts: 1158
Joined: Thu Feb 09, 2012 3:53 pm

Re: Process Switch Variables

Post by freddyp »

No.
User avatar
tdeschampsBluewest
Member
Posts: 153
Joined: Tue Jun 01, 2021 11:57 am

Re: Process Switch Variables

Post 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).
Do you like the Enfocus Apps developed by Bluewest?
Feel free to leave a comment on the Appstore!
User avatar
JimmyHartington
Advanced member
Posts: 492
Joined: Tue Mar 22, 2011 7:38 am

Re: Process Switch Variables

Post 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.
User avatar
tdeschampsBluewest
Member
Posts: 153
Joined: Tue Jun 01, 2021 11:57 am

Re: Process Switch Variables

Post 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.
Do you like the Enfocus Apps developed by Bluewest?
Feel free to leave a comment on the Appstore!
Post Reply