Facility Control

 View Only
  • 1.  Dashboard executing a Javascript action

    Posted 05-20-2019 14:55

    Two Questions:

    1. I have a Javascript file written to copy a file from one location and overwrite a file in a different location. Action is performed when file is double clicked.

    I get an exception message when I try to paste it directly into a Dashboard button.

    "

    function copy()
    {
    var myObject, newpath;
    myObject = new ActiveXObject("Scripting.FileSystemObject");
    myObject.CopyFile ("\\Source Folder\\Source File.txt", "\\Destination Folder\\");
    }

    copy();

    "

    Is there a way to have a Dashboard button "run" the Javascript action?

     

    2. Is there a way to utilize parameter data from dashboard to rewrite parts of the Javascript file to then choose different files to copy and overwrite?

    Thanks!



  • 2.  RE: Dashboard executing a Javascript action

    Posted 05-21-2019 14:26

    Hi Cole.

    This is failing because you are using a browser-based ActiveX mechanism to copy your files (it is using non-standard JavaScript). You an copy a file in ogScript but you will need to use the mechanisms available to its libraries.

     

    Here is a general-purpose file copy function you can paste into an <api/> tag in a CustomPanel:

    function copyFile(src, dst)
    {
    var inputFileSize = ogscript.getFileSize(src); //Get the size of the file
    var input = null; //Create input/output reader/writer variables
    var output = null;

    try
    {
    input = ogscript.createFileInput(src); //Create the file reader
    var bytes = input.readBytes(inputFileSize); //Read the file

    output = ogscript.createFileOutput(dst, false); //Create the file writer
    output.writeByteArray(bytes); //Write the file
    }
    catch (e)
    {
    ogscript.debug("EXCEPTION THROWN: " + e);
    }

    if (input != null) //Make sure the reader is closed
    {
    input.close();
    }

    if (output != null) //Make sure the writer is closed
    {
    output.close();
    }
    }

     

    Here is a full panel that actually makes use of the function:

    <abs contexttype="opengear" id="_top" style="">
    <meta>
    <params>
    <param access="1" maxlength="0" name="Input File" oid="Input_File" type="STRING" value="" widget="file-picker"/>
    <param access="1" maxlength="0" name="Output File" oid="Output_File" type="STRING" value="" widget="file-picker">
    <config key="w.save">true</config>
    </param>
    </params>
    <api>function copyFile(src, dst)
    {
    var inputFileSize = ogscript.getFileSize(src); //Get the size of the file
    var input = null; //Create input/output reader/writer variables
    var output = null;

    try
    {
    input = ogscript.createFileInput(src); //Create the file reader
    var bytes = input.readBytes(inputFileSize); //Read the file

    output = ogscript.createFileOutput(dst, false); //Create the file writer
    output.writeByteArray(bytes); //Write the file
    }
    catch (e)
    {
    ogscript.debug("EXCEPTION THROWN: " + e);
    }

    if (input != null) //Make sure the reader is closed
    {
    input.close();
    }

    if (output != null) //Make sure the writer is closed
    {
    output.close();
    }
    }</api>
    </meta>
    <param expand="true" height="37" left="43" oid="Input_File" top="34" width="269"/>
    <param expand="true" height="39" left="43" oid="Output_File" showlabel="false" top="87" width="271"/>
    <button buttontype="push" height="92" left="319" name="Copy" top="38" width="143">
    <task tasktype="ogscript">/*! block id=1000,1001,1002 !*/
    copyFile(params.getValue('Input_File', 0), params.getValue('Output_File', 0))
    /*!!
    &lt;block id="1000" type="function_copyFile" x="283" y="10" w="243" src="ID:1001" dst="ID:1002" /&gt;
    &lt;block id="1001" type="param__top&amp;amp;Input File (Input_File)[0]" x="10" y="10" w="243" SET="" /&gt;
    &lt;block id="1002" type="param__top&amp;amp;Output File (Output_File)[0]" x="10" y="72" w="243" SET="" /&gt;
    !!*/
    /*!!&lt;checksum&gt;10fcf917cf5160a2a1e605ad05095871&lt;/checksum&gt;!!*/</task>
    </button>
    </abs>

    #DashBoard


  • 3.  RE: Dashboard executing a Javascript action

    Posted 05-21-2019 23:14

    Thanks James!

    This was a huge help and opens a lot of doors for us on our custom panels.

    I was able to utilize values from other parameters to automatically build the source and destination paths, which makes our workflow a hundred times better.


    #DashBoard