Wednesday December 18, 2019

First Switch Scripting Experience

Topics: Switch

The best place to start

Have you considered giving Switch Scripting a whirl? I recently did and here’s what I learned.

This started out from my wanting to build a job board and dashboard in Switch. Graphs, charts and metrics are fascinating to me, so why not build my own; with live data and everything. The problem that I ran into was needing jobs to be present in a flow for any of that to matter. I could have easily dragged piles on jobs into a flow and watched the numbers on my dashboard, but once they finish, it’s boring again. What looks really cool there is a constant flow of jobs queued at various points in a flow. It reminds me of the scene in Willy Wonka and the Chocolate Factory where Mr. Wonka is demonstrating the machine that creates Everlasting Gobstoppers. Check it out on YouTube, later. No distractions.

What I needed was a script that would send jobs, over and over, into my flow. While I was at it, it became obvious that a useful testing script may as well come out of this exercise. Here’s how I stepped through my first Switch scripting experience.

The first thing I needed to do was add a Script Element to my flow.


Then, I needed a code-y, script-y sample to work with. Luckily, there was a very helpful example in the Enfocus Switch Scripting guide. Copy, paste, then I’m off and running. The example showed how to use the timer function to send a new job into a flow. It was nearly perfect but needed some additional features.

At the start, the code looks like this:

I’ve highlighted the first modification that needed to be done. One of the wow-that’s-cool things that can be done with the Scripter is easily creating Element Properties that a script will consume as variables. So, I added a Flow element property called, “timerInterval”, then created a variable in the script with the same exact name: s.getPropertyValue(“timerInterval”);. The bit in quotes needs to match the name in the Flow Element Properties list. The magic takes place with a built in function to get the variable, “s.getPropertyValue().” The Tag property is the script variable name and the Name property is how it will display in the Properties Pane.

Now, do a little cleanup to make it easier for users by changing the Inline editor value to “Number” and my flow element properties starts to look useful.

At this point, the script will send a job into the flow every 30 seconds. It will be a Switch dummy job and be named something clever like, “dummyFileForNewJob.dat”. It’s a text file with one line, “Sample line.”

It was all downhill from there; in a good way. More functionality needed to be added for the sake of usefulness. For instance, I created a Flow Element Property and script variable to make, “Sample line” whatever I wanted. The first text line is static and the second comes from the variable.

I could have called it a day and used the Rename Job element to name each file coming from the script in a unique way. Who knows what tests I may need to run in the future? What file types? What values in an XML file? So, I went nuts with it. My script allows me to choose whether I want an XML, a TXT or a PDF to be injected. It creates the XML from node related properties, creates a text file as described here, and copies a PDF selected in the properties into the flow. For the sake of flexibility, I threw in some if statements to allow me to choose combinations of the three file types. Each file injected is named with a timestamp, by the script, so that each one is unique and they would be matching sets of files.

​There is one last eye-gouging screenshot of code to leave you with. The script is creating a properly formed, albeit very basic, XML file. Do some renaming in the flow and I’m just about done.

Change the extension to “.xml” so Switch can do an XML Pickup, then rename it with metadata from itself and my test job injector is ready.

How about some icing on this cake? I created a 32px by 32px icon in Illustrator to replace the default Script Package icon.

I made my first script, found out it didn’t require a degree in computer science, filled my flow with test jobs and ended up with a job board and dashboard full of wonderfully fake metrics. Mission accomplished.

To get started with your own scripting, check out the Enfocus Switch Scripting Guide and the JavaScript reference from W3Schools.

Don't want to miss anything? Subscribe now!

Subscribe to our monthly newsletter and get the latest Enfocus articles delivered to your inbox.

(*) Required field. By submitting this form, you confirm that you have reviewed and agree with the Enfocus general terms, conditions, EULA and privacy policy.





About John Dean

Marketing Content Specialist Emoji

Marketing Content Specialist at Enfocus

John brings a unique perspective, leveraging nearly thirty years of print production experience, technical savvy and an enthusiastic creativity. An understanding of user experience beginning in the early days of Pitstop and on through to building web-to-print workflows for commercial print positions him to provide a valuable viewpoint of Enfocus products. He is a self-proclaimed Switch Enthusiast, enjoys good XML and quiet walks from Web to Print.



Read more about:

Sign in or create an Enfocus ID to post comments