Velocity: Scripting, Basic Replace

Version 3

    This Document is to give Scripting examples for Velocity.. this will go over the basic functionality of the scripts..

     

    Basic Replace:

    1. Open the Script Editor in the Velocity Console.

    2. Create a new Script

         a. the script:

     

     

    function onScan(event)

    {

         //Optional Method   

         // var replaceWhat = 4

         // var replaceWith = 0

     

         //Optional event

         // event.data = event.data.replace(4, 0);

     

                   //This is where we modify the scan data event.data is what wavelink calls the scanned data to be edit. If you plan on editing scanned data, you must use the event.data

                   event.data = event.data.replace(replaceWhat, replaceWith);

     

    }

    //Register the onScan function to be called when a scan occurs. Without this it will never run.

    WLEvent.on("Scan", onScan);

     

         b. You will also need to create the Parameters for this script.. As you can see in the above script, i have two parameters called, replacewhat and replaceWith. These can be defined in a couple ways.. you can use the normal method of using, var NAME= "*", You can also directly input the values into the event.data script so they are hard coded into the event, or lastly you can use the parameters editor with in the script editor as show in the screen shots below.

     

    In the above screenshot you can see i called out the regex by way of "^[0-9]+$", minus the "" of course, declaring the use of numbers 0-9. Using this method of calling out the variables will enable you to be able to edit these values at a later date without having to go into the script editors / parameters .

    You can see above that i am able to edit those values here.

     

    Once you have your script created be sure to link to a session and save and then deploy.. this will create a WLDEP file that you can copy and paste to your devices com.wavelink.velocity folder.

    This script will function as follows:

    Scanned Data: 112233445566778899

    OutPut:     112233045566778899

     

     

    Global RegExp:

     

    Now lets say that I want all characters replaced with in the scanned barcode that i designate..

     

    for example:

     

    scanned data: 112233445566778899 and I want to replace all 4 with a 0. the script above will only replace the first 4 it happens across but what if I want to change all of the 4 in the scanned data. This is where we will need to use a global expression to make this happen.. The "g" modifier within Javascript is used to perform a global match.

     

    the syntax to make the above script work globally will be as follows:

     

    function onScan(event)

     

    {

        var regex = new RegExp(replaceWhat, "g");

        event.data = event.data.replace(regex, replaceWith);

    }

     

    WLEvent.on("Scan", onScan);

     

    As you can see i have added a new line to the script: var regex = new RegExp(replaceWhat, "g"); I have also modified the script to use the new variable of regex, event.data = event.data.replace(regex, replaceWith);


    this addition to the script will set the replace to work globally and replace all of a certain character in the scan.

     

    The out put of your scanned data should appear as such: 112233005566778899

     

    Replacing a string or replace with a string value

     

    In order to replace a sting value or non standard character in a bar code you will need to call out these characters in the parameters..

     

    An example parameter would be:

     

    Now you can select which character you want to replace and the same for what you want to replace it with

     

     

    Your script would look the same as the one above just with the different variables being called out

     

    function onScan(event)

     

    {

        var regex = new RegExp(replaceStringWhat, "g");

        event.data = event.data.replace(regex, replaceStringWith);

    }

     

    WLEvent.on("Scan", onScan);

     

     

    You can even call both functions with in the same script if you wanted to do so

     

    In a crude fasion of the script would be:

     

    function onScan(event)

     

    {

        var regex = new RegExp(replaceWhat, "g");

        event.data = event.data.replace(regex, replaceWith);

        var regex = new RegExp(replaceStringWhat, "g");

        event.data = event.data.replace(regex, replaceStringWith);

    }

     

    WLEvent.on("Scan", onScan);