Using VWorks to get the PC Name as a string

Hi,

I’ve been working on this for a few days now with no luck. But I’ve been trying to figure out how to get a JavaScript in VWorks to pull the PC name so that it can be cross referenced from a list before a protocol is ran. Most of the examples I’ve found online so far use ActiveX controls which don’t work within VWorks or use other functions that are also not recognized.

Any help would be great thanks!

1 Like

I don’t think it is possible to get the PC name from Javascript in VWorks or in most browsers.

I would solve this with a text file that contains the PC name and any other helpful information. Just read that file into VWorks. If there are a lot of PCs to set up I would write a script (in python) to automatically generate the file.

Also, I have had pretty good luck using this to add some of the modern JS methods to the VWorks JS interpreter: GitHub - es-shims/es5-shim: ECMAScript 5 compatibility shims for legacy (and modern) JavaScript engines

1 Like

What about having VWorks run a bat file off the cmd.exe? Have the bat file get the PC info and write it to a .txt file, then have the VWorks JS parse the file and get your info.

I’ve got experience doing all these things. I run bat files from VWorks, I have written txt files from bat files, and I have consumed txt file info into VWorks. We use a lot of this in our lab’s LIMS api to roundabout talk to Vworks.

EDIT: What DCurrier said above works too. Instead of bat files, use a python script, which you can have VWorks run. I have ran .py files from Vworks as well. This is definitely doable. Just not as straightforward as having the VWorks JS do it directly.

This modern to legacy JS interpreter patch is pretty awesome. I’ve been wanting something like this for years for VWorks. How do you implement this exactly? Do I just need to call/open these .js files to have access to the additional methods and stuff?

I would do almost anything to not write another bat script in my life, but it is definitely a way to get the job done. I have never executed an external command from VWorks; do you have an example of how to do it? Does VWorks have an execute() type of method?

To implement the es5-shim you just use the open() method in VWorks and you have all access to all of the methods in it. I usually put it in the startup script for the protocol so it runs before anything else.

Also, pro tip, I write most of my JS as functions in a file external to VWorks, open the file after the shim in the startup script, then just call the function at the step you want to run it. Its so much easier to write and maintain in a text editor than in the VWorks code box. You can fix bugs in your code without opening VWorks.

Hey Duane,

I’ve previously had a need to run command prompt things from JS. You’ll see exactly why in my example:

run(“cmd /C C:\"Program Files\MySQL\MySQL Server 5.1\bin\mysql" < C:\ResetV11DB.sql”, 1);

You can do this in a .js file you call via an “open()” or directly within the JS box in VWorks. I, like you, prefer to keep everything tidy in .js files that I call over and over again. I have a main.js that typically runs across all VWorks machines and one of the first things that calls is a localvariables.js which can tell that particular instance of VWorks some unique aspects of that machine, like hotel size or that this Bravo has a shaker, that sort of thing.

@ser i wish i saw this thread earlier. You can absolutely use Active X controls in VWorks however the Syntax is a bit different than standard javascript.

This is pretty old but if you wanted to connect to the VWorks DB in js:

var ConnDB = new ActiveX(“ADODB.Connection”);
ConnDB.ConnectionString = “dsn=velocity11;”;
ConnDB.Open();

var Rs = new ActiveX(“ADODB.Recordset”);

I used to build the controls myself all of the time to extend the functionality of the JS engine, you can set breakpoints or attach the ide to vworks too.

Here is a real simple ActiveX Control I wrote many years ago, it doesn’t do much but you can see that once you dive in to C#, you can access just about any library.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.IO;
namespace VWorksUtilities
{

    [
    Guid("9ACB9898-B31E-492D-91B6-706BE05846FB"),
    ProgId("VWorksUtilites.FileManager"),
    ClassInterface(ClassInterfaceType.None),
    ComDefaultInterface(typeof(IFileManager)),
    ComVisible(true)
    ]
    #region IFileUtilities Members
    public class FileManager : IFileManager
    {
        public string[] getFilePaths(string directory) {


            try
            {
                string[] files = Directory.GetFiles(directory);

                return files;

            }
            catch (Exception e)
            {
                string[] emptyArray = { e.Message.ToString() };
            }

            return Directory.GetFiles(directory); }

        public string getLatestFile(string directory, string extension)
        {

            try
            {
                var dir = new DirectoryInfo(directory);
                var myFile = dir.GetFiles("*." + extension)
                 .OrderByDescending(f => f.LastWriteTime)
                 .First().ToString();

                return myFile;

            } catch(Exception e)
            {

                return e.Message.ToString();

            }

        }

    }
    #endregion
}

After you build the control and register it, you would can call it in JS like this:

var utilities = new ActiveX(“VWorksUtilites.FileManager”);

var utilities.getFilePaths(“C:\path\to\folder”);

This was before ChatGPt so i think i had to use netscape or yahoo to figure this out :slight_smile:

I wrote an application note on how to do this about 13 years ago if you can find some at agilent, they may still have it

2 Likes

Ask an ye shall receive!

Pete’s extra special App Note.

1 Like

Hi @DCurrier, I use the VWorks run() method all the time to get things done. For example, suppose that you need to create a folder (myFolder) if not existent you can do something like:

var f = new File()
if (!f.Exists(myFolder)) run("cmd /c mkdir \"" + myFolder + "\"", true)

where the true/false flag after the command returns control to VWorks after the shell command completes or immediately.

And yes, the VWorks JS is old (ES3 - 1999!), but you can create “polyfills” at will and implement the functions you need.

Another (not much known feature) is the possibility of creating JS Wrappers that look like custom VWorks tasks. In this way you simply write your code once and reuse it in multiple protocols. if interested, please check out this link.

2 Likes