Use JavaScript to optimize the AI automation code
Many developers love using aiAct or ai to accomplish automation tasks, even packing long, complex logic into a single natural-language instruction. It feels “smart,” but in practice you may run into unstable reproducibility and slower performance.
This article shares an approach for writing automation scripts with JavaScript and structured APIs.
Write automation scripts with JavaScript and structured APIs
Midscene provides structured API methods like aiBoolean, aiString, and aiNumber to extract state from the UI. Combined with instant action methods like aiTap, aiInput, aiScroll, and aiHover, you can break complex logic into steps to make automation more stable.
A simple example
Take this prompt as an example:
By composing the structured APIs, you can convert the prompt into more reliable, maintainable code:
After changing the coding style, the process becomes more reliable and easier to maintain, and you can use traditional debugging to control the execution flow.
A complex example
Here is another example, shown before rewriting:
Using structured APIs, you can lock this flow into code:
Commonly-used structured API methods
Here are some commonly-used structured API methods:
aiBoolean - conditional decision
- Use Case: Condition Judgment, State Detection
- Advantage: Convert fuzzy descriptions into clear boolean values
Example:
aiString - text extraction
- Use Case: Text Content Retrieval
- Advantage: Avoid Ambiguity in Natural Language Descriptions
Example:
aiNumber - numerical extraction
- Use Case: Counting, Numerical Comparison, Loop Control
- Advantage: Ensure Return Standard Numeric Types
Example:
aiQuery - general data extraction
- Use Case: Extract any data type
- Advantage: Flexible Data Type Handling
Example:
Instant action methods
Midscene provides some instant action methods, like aiTap, aiInput, aiScroll, aiHover, etc., They are also commonly used in the automation code. You can check them in the API page.
Which approach is best: aiAct or structured code?
There is no standard answer. It depends on the model's ability, the complexity of the actual business.
Generally, if you encounter the following situations, you should consider abandoning the aiAct method:
- The success rate of
aiActdoes not meet the requirements after multiple retries - You have already felt tired and spent too much time repeatedly tuning the
aiActprompt - You need to debug the script step by step
Want to write structured code easily?
If you think the JavaScript code above is hard to write, now is the time to use an AI IDE.
Use your AI IDE to index our docs:
- https://midscenejs.com/use-javascript-to-optimize-ai-automation-code.md
- https://midscenejs.com/api.md
To learn how to add Midscene docs to your AI IDE, see this article.

