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.
Why code with generative AI?
It’s pretty clear that off-the-shelf 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.
One of PTC Mathcad Prime’s most impactful new features in recent years has been 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. That means learning how to code. PTC Mathcad Prime 11 offers three scripting languages: VBScript, which Microsoft has deprecated, Microsoft JScript ES3, an old JavaScript competitor that is mostly mutually compatible with JavaScript but without the benefit of active development in the past couple of decades, and the very popular Python programming language. VBScript and JScript are already pre-installed on Windows computers, while using Python in Mathcad requires both the worksheet’s creator and recipient to have independently downloaded Python—an easy task, but one that’s still a barrier.
Many Mathcad users, myself included, must 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. But is there a shortcut with generative AI?
Coding a conditionally formatted textbox with Copilot
Things to consider
The version of this article from late October 2024 had an entire section dedicated to “priming” the generative AI with preliminary questions to make sure it absorbed the existing body of knowledge from the then-latest release’s help center, and to make sure it knew about Mathcad Prime 10 because its implementation of scriptable control events, classes, methods, and properties was brand-new information for it. Since then, Copilot seems to already have this information in its database.
I also chose Microsoft Copilot over ChatGPT because I assumed Copilot is more capable of a Microsoft-based language (JScript), and ChatGPT already had its chance in the previous article hyperlinked in the opening.
Compared to the 2024 article (where it took two prompts), Copilot spat out working code with just one prompt in 2025 (although my prompt wasn’t exactly identical between 2024 and 2025, since I improved it a bit). Copilot’s 2025 script was not identical to the final, correct script from 2024, so Copilot was not just copying its earlier answer to the same problem. In 2024’s article, Copilot only gave information for the Exec event; below, you’ll see it gave the script to copy-paste for the entire Text Box.
My Prompt
“Please write JScript ES3 code that would work in PTC Mathcad Prime 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. The code must make the text box background colour turn green if "budget" is greater than or equal to "cost", and change the text box's text to "Passed, you have enough budget!". If "budget" is lower than "cost", change the text box background colour to red and change the text box text to "Failed, you need more budget!"
Please refer to the Mathcad Prime Help Center documentation for specific state properties and format properties.”
Copilot’s Response
Here’s an example JScript ES3 code snippet for PTC Mathcad Prime Advanced Text Box Control that meets your requirements. It uses the documented properties from the Mathcad Help Center for Text Box Class (e.g., BackColor, Text) and follows the event-driven structure (Start, Exec, Stop):
✅ JScript ES3 Code for Conditional Formatting in Text Box
// This script runs in the Text Box control in Mathcad Prime
// Inputs[0].Value = budget
// Inputs[1].Value = cost
function TextBoxEvent_Start(Inputs, Outputs) {
// Optional: Initialize font or other properties
TextBox.Font.Size = 14;
}
function TextBoxEvent_Exec(Inputs, Outputs) {
var budget = Inputs[0].Value;
var cost = Inputs[1].Value;
if (budget >= cost) {
// Passed condition
TextBox.BackColor(0xAAFFAA); // Light green background
TextBox.Text("Passed, you have enough budget!");
} else {
// Failed condition
TextBox.BackColor(0xFFAAAA); // Light red background
TextBox.Text("Failed, you need more budget!");
}
// Output the current text for reference
Outputs[0].Value = TextBox.Text();
}
function TextBoxEvent_Stop(Inputs, Outputs) {
// No cleanup needed here
}
✅ Key Points from Mathcad Help Center:
- Formatting Properties:
-
BackColor(colorCode)→ Sets background color using hex ARGB (e.g., 0xAAFFAA for green, 0xFFAAAA for red).
-
Font.Size→ Adjusts text size.
- State Properties:
-
Text(string)→ Sets or retrieves the text displayed in the Text Box.
- Events:
-
Start→ Initialization.
-
Exec→ Main logic for processing inputs and updating appearance.
-
Stop→ Cleanup (optional).
What the Copilot-created Text Box looks like in a Mathcad Prime worksheet
I defined budget and cost (each as Slider advanced controls), and I also manually typed in the evaluations of budget and cost. I also defined Check as a Text Box advanced control and I added Budget and Cost as input variables to Check. What I didn’t do with my own human action was the actual script in the Text Box control, which I copy-pasted directly from Copilot. It does what I asked it to.
If I’m able to properly define exactly what I want, which is itself a skill that needs to be practiced, it seems that Copilot can figure things out. Granted, this is a simple example. I doubt that Copilot would be able to output highly specialized custom scripts. You should study up on what’s available in the Mathcad Prime Help Center, both so you’re able to troubleshoot what a generative AI spits out, and so you’re able to specify what the prompt should say.
Converting Scripts to Python
Legacy Mathcad users may have many worksheets using old scripting languages they’d like to convert into Python, or they may even wish to convert JScript-scripted examples others have made. If you’re working in an environment where everyone has Python installed, and Python scripting is standardized, is generative AI able to convert legacy JScript (or even VBScript) scripts into Python? Using the same conditional formatting Text Box example, let’s turn back to Microsoft Copilot for this task.
My Prompt to convert to Python
“Please convert that same Text Box script from JScript into Python for use in Mathcad Prime.”
Copilot’s First Response
Here’s the equivalent Python script for Mathcad Prime using its scripting interface (which supports Python for custom controls):
✅ Python Code for Advanced Text Box Control in Mathcad Prime
# This script runs inside Mathcad Prime's Python-based control
# Inputs[0].Value = budget
# Inputs[1].Value = cost
def TextBoxEvent_Start(Inputs, Outputs):
# Optional initialization
TextBox.Font.Size = 14
def TextBoxEvent_Exec(Inputs, Outputs):
budget = Inputs[0].Value
cost = Inputs[1].Value
if budget >= cost:
# Passed condition
TextBox.BackColor(0xAAFFAA) # Light green background
TextBox.Text("Passed, you have enough budget!")
else:
# Failed condition
TextBox.BackColor(0xFFAAAA) # Light red background
TextBox.Text("Failed, you need more budget!")
# Output the current text for reference
Outputs[0].Value = TextBox.Text()
def TextBoxEvent_Stop(Inputs, Outputs):
# No cleanup needed
✅ Notes:
- Properties remain the same as in JScript:
TextBox.Text("...")→ Sets text
TextBox.BackColor(colorCode)→ Sets background color
TextBox.Font.Size→ Adjusts font size
- Color codes use ARGB hex (e.g., 0xAAFFAA for green, 0xFFAAAA for red).
- The event names
(TextBoxEvent_Start, TextBoxEvent_Exec, TextBoxEvent_Stop)are consistent with Mathcad Prime’s scripting model.
Error Messages
The code as-is does not work, because PTC Mathcad Prime 11’s Script Editor returned, “Expected an indented block after function definition on line 26 (<string>, line 27)." It’s a simple error, but it still isn’t one that Copilot should be making. As long as your code has syntax errors like that, Mathcad Prime won’t let you save and “Apply” the code.
I complained to Copilot:
“Mathcad responds, "Expected an indented block after function definition on line 26 (<string>
Copilot’s Corrected (but still wrong) Response
Copilot replied:
That error means Python expects an indented block after the def line, but the next line is either missing or incorrectly indented. In Mathcad’s Python scripting environment, indentation rules are strict—every function body must be indented consistently (usually 4 spaces).
Here’s the corrected version with proper indentation:
✅ Fixed Python Script
def TextBoxEvent_Start(Inputs, Outputs):
# Optional initialization
TextBox.Font.Size = 14
def TextBoxEvent_Exec(Inputs, Outputs):
budget = Inputs[0].Value
cost = Inputs[1].Value
if budget >= cost:
# Passed condition
TextBox.BackColor(0xAAFFAA) # Light green background
TextBox.Text("Passed, you have enough budget!")
else:
# Failed condition
TextBox.BackColor(0xFFAAAA) # Light red background
TextBox.Text("Failed, you need more budget!")
# Output the current text for reference
Outputs[0].Value = TextBox.Text()
def TextBoxEvent_Stop(Inputs, Outputs):
# No cleanup needed
pass
✅ Why the error happened:
- After def TextBoxEvent_Stop(Inputs, Outputs): you had no indented block (or maybe a blank line without pass).
- Python requires at least one statement inside the function body. If you don’t need logic, use pass.
Copilot is providing missing arguments and is hallucinating
Now the Mathcad Prime Script Editor returns another error, “TextBoxEvent_Start() missing 2 required positional arguments: ‘Inputs’ and ‘Outputs’”, which is a very fundamental mistake on Copilot’s part. It’s also very easily fixed if you’ve studied up on how Python is implemented in Mathcad Prime. Unfortunately, I haven’t, and even worse, I’m arguing with Copilot (I won’t transcribe it all in this article; I’m worried that further wrong answers will be picked up by AI crawlers in the future and will be mistaken for correct information!) and I’m pretty sure it’s just hallucinating information, including that “Python scripts in Mathcad do NOT use event functions like TextBoxEvent_Start or TextBoxEvent_Exec. Mathcad’s Python scripting does not require function definitions for controls. The script runs in a context where Inputs, Outputs, and TextBox are already available. No need for def or pass—just write the logic directly.” It also seems to believe I’m trying to run the script outside of Mathcad. This is fabricated information, and not supported by Mathcad Help. If you listen to Copilot’s advice when it’s in this state, you’ll never get a working Python script.
I’ll convert the Text Box script into Python, correctly, myself, the old-fashioned way
Even though I don’t already know Python, I looked through enough of the examples in Mathcad Help to eventually figure out the proper syntax for everything. Here is the correct Python code that replicates the above JScript code. You can copy-paste it into Mathcad Prime for a Text Box advanced control, and it will actually work if you have the two inputs added to the Text Box control.
def TextBoxEvent_Start():
# Optional initialization
TextBox.Font.Size = 14
def TextBoxEvent_Exec(Inputs, Outputs):
budget = Inputs[0]['Value']
cost = Inputs[1]['Value']
text = TextBox.Text
if budget >= cost:
# Passed condition
TextBox.BackColor = "#AAFFAA" # Light green background
TextBox.Text = "Passed, you have enough budget!"
else:
# Failed condition
TextBox.BackColor = "#FFAAAA" # Light red background
TextBox.Text = "Failed, you need more budget!"
# Output the current text for reference
Outputs[0]['Value'] = text
def TextBoxEvent_Stop():
# No cleanup needed
Pass
Comparing my manual, correct Python version to the first attempt at the JScript to Python conversion made by Copilot, Copilot missed:
- Every single opportunity for proper Python syntax when declaring definitions. Copilot instead left things in JScript’s syntax, which will result in error messages (for example, it kept how color, text, input, and output values were written in JScript instead of converting them to Python)
- Python’s pass line in the TextBoxEvent_Stop event
- Removing the Inputs, Outputs in the TextBoxEvent_Start and TextBoxEvent_Stop events, which are proper in JScript but not Python
When Copilot claimed that the properties remain the same in Python as they do in JScript, it didn’t mean the values of the properties, but how the properties are actually written out. This is wrong. It would’ve been faster to just write this from scratch (or possibly have Copilot make it from scratch).
Concluding observations
Generative AI may be able to write your scripts for you in JScript from scratch. It may be able to handle Python from scratch as well. As more people use Mathcad advanced controls in Python, AI crawlers should be able to learn that and synthesize that information with the greater availability of Python language learning more broadly. However, based on my failed experiment above, generative AI still has a long way to go if you want to convert from one scripting language to another. I suggest you and your organization pick a language for scripting (JScript or Python) and then standardize on it and don’t change from there.
You should not expect an AI to conjure up the exact code you want on the first try. The more specific but basic 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 understand 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 investing in yourself to learn skills. Take the time to learn the basics. Fortunately, PTC has resources to make that easier for you!