Written by David Newman
  • 10/29/2024
  • Read Time : 5 min.

Can Microsoft Copilot Write Mathcad Advanced Controls Scripts?

Can Copilot write Mathcad Advanced Controls?

Why code with generative AI?

It’s pretty clear that generative artificial intelligence (AI) cannot be trusted with performing even the most basic of engineering calculations. AI proponents would say that this was never a use case to begin with, and that it’s better for writing blog articles (not mine!), creating artwork, or generating code. It’s that last use case that I’d like to examine.

PTC Mathcad Prime 10 released earlier in 2024, and the biggest new feature is Advanced Controls with scripting. Advanced Controls make your Mathcad Prime worksheets interactive in a way that was not possible before. Its applications run from highly convenient to game-changing (or game-making, in a literal sense).

However, to get the most out of Advanced Controls with scripting, you need to learn… scripting. And that means learning how to code. Mathcad Prime 10 offers two scripting languages: VBScript, which Microsoft has deprecated, and Microsoft JScript ES3, an old JavaScript competitor mostly mutually compatible with JavaScript but without the benefit of active development in the past couple of decades.

Upcoming Mathcad Prime releases have compatibility with additional programming languages on the roadmap. For now, the vast majority of Mathcad users, myself included, have to learn a new programming language to unlock the powers of Advanced Controls. It’s not a trivial learning curve, but it is doable with the right training (PTC recently hosted a two-hour free training event now available on-demand). But is there a shortcut with generative AI?

Asking Copilot preliminary questions

If AI is so great at generating content and code snippets, perhaps it could help us get to a basic running start with the scripting required using JScript? To figure this out, I turned to Microsoft’s Copilot generative AI, since I figure it should be more competent than ChatGPT at spitting out Microsoft’s JScript language.

At first, I asked it how up-to-date its knowledge base was. Oddly enough, it asked me to ask it something else, claiming it can’t give a response. I asked again, and it said 2021, but it also has the ability to fetch new knowledge if asked to. I tested this by asking if it knows about Mathcad Prime 10’s Advanced Controls with scripting, along with my specific (I asked this verbatim) question of if Copilot knows “its Events for Scriptable Controls and each controls' class, methods, format properties, and state properties?”

It’s very important to specify Mathcad Prime 10—just saying “Mathcad” will have a generative AI think you’re talking about legacy versions of Mathcad and its old implementations of scriptable controls in VBScript. I’d like to avoid VBScript if I can, in order to future-proof my Mathcad Prime worksheets.

Copilot confirmed that it knows about Mathcad Prime 10, citing Mathcad.com’s What’s New page and our own blog articles, as well as the Mathcad Prime 10 Help Center documentation. Copilot admitted that the documentation is the best place to get the full details on each control’s class/methods/properties and it might not be able to synthesize that information itself. Fair enough.

Surprisingly, it prompted me via suggested follow-up to ask it how to create a slider control in Mathcad Prime 10, grabbing its information from the Mathcad Prime Help Center. This proves that Copilot has the knowledge base to be able to answer our questions.

I include that context here because you might need to “prime” a generative AI with questions like these so it will know what you’re talking about. Conversations with a generative AI bot build off previous statements, and in this example, Copilot had to specifically search the Mathcad Prime 10 Help Center because of my questions instead of that being part of its existing knowledge base.

As a cautionary tale, my colleague who chatted with ChatGPT in last year’s blog about unit conversions tried prompting ChatGPT again this year to do the below task. His result was an utter disaster because he went straight to asking ChatGPT to create Mathcad JScript code and it had no idea how to handle it.

Coding a conditionally formatted textbox

Now that I was satisfied with Copilot’s knowledge of Mathcad Prime 10, it was time to put it to productive work. At this point in the blog, I’ll be copy-pasting the conversation log between myself and Copilot (with some additional commentary), starting with this prompt from me:

How do I write JScript ES3 code that would work in Mathcad Prime 10 for an advanced "text box" control where I enter in two variable input values, "budget" defined as a variable equal to Inputs[0].Value; and "cost" defined as a variable equal to Inputs[1].Value; and have the box background colour turn green if budget is greater than or equal to cost and change the text box text to "Passed, you have enough budget!", or else the box background colour turns red if the budget is less than the cost and change the text box text to "Failed, you need more budget!"?

Note that I was very specific about JScript ES3 and defining the two input variable values in the syntax that Mathcad Prime 10 will approve. Here’s how the bot replied:

Sure, I can help with that. Here's an example of how you might write the JScript ES3 code for an advanced "text box" control in Mathcad Prime 10:


```javascript
// Define the variables
var budget = Inputs[0].Value;
var cost = Inputs[1].Value;
// Check if budget is greater than or equal to cost
if (budget >= cost) {
// Change the text box text and background color
Outputs[0].Value = "Passed, you have enough budget!";
Outputs[0].BackColor = 0x00FF00; // RGB for green
} else {
// Change the text box text and background color
Outputs[0].Value = "Failed, you need more budget!";
Outputs[0].BackColor = 0xFF0000; // RGB for red
}
```


This script checks if the `budget` is greater than or equal to the `cost`. If it is, it sets the text box text to "Passed, you have enough budget!" and changes the background color to green. If the `budget` is less than the `cost`, it sets the text box text to "Failed, you need more budget!" and changes the background color to red.
Please note that you'll need to replace `Inputs[0].Value` and `Inputs[1].Value` with the actual input values you're using, and `Outputs[0].Value` and `Outputs[0].BackColor` with the actual output text box you're using.

Here is a picture of what the outcome of the above code is when put in a text box control in Mathcad Prime 10, with the two input values properly defined using slider controls, which are simple enough to add to a worksheet without having to code anything.

Using Copilot's Advanced Controls script in Mathcad Prime failed to conditionally format the Text Box

While Copilot did create code that produced an output that can be used further down the worksheet, I specifically wanted the textbox itself to change rather than just being a code vehicle.The Outputs[0].BackColor is an example of AI hallucination; such a property doesn’t exist in Mathcad.

The reason you want to use the Text Box is that the text itself can change values and colors based on the scripts. This was a close attempt by Copilot, but it ignored my asks regarding “change the text box text.” I would have to clarify my ask, but in order to be able to write my clarification properly, I would have to use specific language that I only know because I already read through and understood the Mathcad Prime 10 Help Center documentation. Here’s how I replied:

Can you change the use of Outputs[0].Value and Outputs[0].BackColor to TextBox.Text() and TextBox.BackColor() to match Mathcad Prime 10’s supposed Text Box State Properties and Text Box Format Properties, respectively, as noted in the Mathcad Prime 10 documentation? My end goal is to have the text box itself change its background colour and have the text programmatically appear in the textbox based on the two input variables. Thank you!

Here is how Copilot replied:

Absolutely, I understand your requirement better now. Here's how you might adjust the JScript ES3 code for the advanced "text box" control in Mathcad Prime 10:


```javascript
// Define the variables
var budget = Inputs[0].Value;
var cost = Inputs[1].Value;

// Check if budget is greater than or equal to cost
if (budget >= cost) {
// Change the text box text and background color
TextBox.Text("Passed, you have enough budget!");
TextBox.BackColor(0x00FF00); // RGB for green
} else {
// Change the text box text and background color
TextBox.Text("Failed, you need more budget!");
TextBox.BackColor(0xFF0000); // RGB for red
}
```

In this script, `TextBox.Text()` is used to change the text in the text box, and `TextBox.BackColor()` is used to change the background color of the text box. The RGB values for green and red are `0x00FF00` and `0xFF0000`, respectively.
Please note that you'll need to replace `Inputs[0].Value` and `Inputs[1].Value` with the actual input values you're using.

Here is how the new scripted code manifests when used in Mathcad Prime 10:

Using Copilot's Advanced Controls script in Mathcad Prime successfully conditionally formatted the Text Box

Copilot’s second code attempt is exactly how I'd code it by hand. You could set Outputs[0].Value to something if you want Check2 to have a value for use later in the worksheet, but for this use case it's unnecessary.

Amazingly, it worked out how I envisioned it! I thanked Copilot (this is an optional step) and it said that I’m welcome and “happy computing with Mathcad Prime 10!”

Concluding observations & where to learn more about Advanced Controls

I’m still not sure that Copilot knows JScript ES3 specifically, since both of its code blocks were labelled as JavaScript. This is fine for a simple example like the one we used here, but for more advanced use cases, this may pose a problem. JScript and JavaScript are very similar with the same syntax style, but JavaScript has gone on to develop significantly deeper built-in capabilities that JScript could only dream of. You’ll want to test your code.

You should not expect an AI to conjure up the exact code you want on the first try. The more specific your prompt is, the better it will do. The only way you’ll be able to be very specific with your prompt is to already have an understanding of Mathcad Prime’s supported methods and properties for the scripts.

AI can be a shortcut in writing scripts, but it won’t be a shortcut for you to take the time to learn for yourself what’s going on. I didn’t have to learn JScript syntax to be able to get a successful prompt, but I did have to know how inputs and outworks work to the extent that I could recognize Copilot made a mistake in its first iteration, and to the extent that I could guide Copilot to using TextBox.Text and TextBox.BackColor instead of the hallucinated Outputs[0].BackColor.

Besides our Advanced Controls crash course webcast, PTC will be posting blog content and videos that will help provide you with pre-coded common use cases for each of the six advanced controls, plus how converting controls from legacy Mathcad versions works. Stay tuned for a specific blog on each of these topics:


Watch the Advanced Controls Crash Course on-demand!

Join PTC’s scriptable controls expert as they take you through the process of learning to use the controls and get you off the ground to rapidly achieve results in your worksheets. Plus, you can download our code samples!

Tags:

About the Author

David J. Newman is a Senior Marketing Specialist for PTC’s CAD segment. He works with PTC’s partners so they have the marketing assets they need, and he also is in charge of the strategy and execution for PTC Mathcad marketing.

Can Microsoft Copilot Write Mathcad Advanced Controls Scripts?
Learn if generative AI can write PTC Mathcad Prime scriptable controls, best practices if you choose to do so, and other easy ways to make Advanced Controls.