ArgusJS Examples and Recipes - Part 1
This part covers basic script structure, messages, reports, clicks, gestures, swipes, scrolling, and simple reusable helpers.
These examples are written for copying and adapting. Replace package names, image names, text labels, and coordinates with the values that match your own project.
Basic script structure
Use this as a simple starting point for most scripts.
setReferenceResolution(1080, 1920);
clearReport();
reportStep("Script started");
toast("Running script...");
log("Script started");
// Put your automation steps here.
reportStep("Script finished");
setSuccessMessage("Script completed successfully.");
Show a short message
Use toast(...) for quick user feedback.
toast("Hello from ArgusJS");
Show a longer message
toastLong("This message stays visible longer.");
Write to the log
Use log(...) while testing and debugging.
log("Script started");
log("Checking screen...");
log("Step 1 complete");
Log numbers, booleans, and objects
log(123);
log(true);
log({ step: "login", ok: true });
Add a report step
Use reportStep(...) for important progress events.
reportStep("Opened the app");
Add report details
reportInfo("Button found", {
file: "start_button.png",
similarity: 0.91
});
Report an error
reportError("Start button was not found");
Stop a script intentionally
Use scriptExit(...) when the script should stop cleanly.
var allowed = false;
if (!allowed) {
scriptExit("Required condition was not met.");
}
log("This line will not run.");
Wait safely
sleep(...) uses milliseconds.
log("Waiting...");
sleep(1000);
log("One second passed.");
Wait after a screen transition
A short wait helps after clicks, app changes, popups, or overlays.
click(500, 1200);
sleep(500);
var next = waitText("Continue", 5);
if (next) {
click(next);
}
Click a screen position
Coordinates are absolute screen pixels.
click(500, 1200);
sleep(300);
Check if a click was dispatched
var ok = click(500, 1200);
if (!ok) {
reportError("Click failed. Check Accessibility permission.");
}
Click with a location object
var point = createLocation(500, 1200);
click(point);
Click above a point
var point = createLocation(500, 1200);
click(point.above(80));
Click below a point
var point = createLocation(500, 1200);
click(point.below(80));
Click left of a point
var point = createLocation(500, 1200);
click(point.left(50));
Click right of a point
var point = createLocation(500, 1200);
click(point.right(50));
Create a region
A region is a rectangle: x, y, w, h.
var area = createRegion(100, 500, 800, 300);
Click a region
A region click targets the center of the rectangle.
var buttonArea = createRegion(300, 1000, 500, 160);
click(buttonArea);
Click a region using the region method
var buttonArea = createRegion(300, 1000, 500, 160);
if (!buttonArea.click()) {
reportError("Region click failed");
}
Click the center of a region manually
var area = createRegion(300, 1000, 500, 160);
var center = area.getCenter();
click(center);
Click with random offset
This is useful when you do not want to tap the exact same pixel every time.
var button = createLocation(540, 1400);
click(button, 6);
Click an image match with random offset
var start = waitFor("start_button.png", 5);
if (start) {
click(start, 4);
}
Double click a point
doubleClick(createLocation(540, 1200), 120);
Double click a found image
var item = waitFor("item.png", 5);
if (item) {
doubleClick(item, 150);
}
Long click a point
longClick(createLocation(540, 1200), 800);
Long click a region
var card = createRegion(100, 500, 880, 300);
card.longClick(1000);
Press and hold
longPress(createLocation(540, 1200));
sleep(1500);
stopLongPress();
Swipe up
swipe(540, 1600, 540, 500, 600);
Swipe down
swipe(540, 500, 540, 1600, 600);
Swipe left
swipe(900, 1000, 150, 1000, 600);
Swipe right
swipe(150, 1000, 900, 1000, 600);
Swipe using locations
var from = createLocation(540, 1600);
var to = createLocation(540, 500);
swipe(from, to, 600);
Human-like swipe
Use humanSwipe(...) when you want a more natural-looking movement.
var from = createLocation(540, 1600);
var to = createLocation(540, 500);
humanSwipe(from, to, 800, 12);
Scroll inside a region
var list = createRegion(0, 400, getScreenWidth(), 1200);
scroll(list, "up", 700, 500);
Scroll down inside a region
var list = createRegion(0, 400, getScreenWidth(), 1200);
scroll(list, "down", 700, 500);
Drag and drop between two points
var from = createLocation(300, 1200);
var to = createLocation(800, 1200);
dragDrop(from, to);
Drag one region to another
var source = createRegion(100, 700, 250, 250);
var target = createRegion(700, 700, 250, 250);
dragDrop(source, target);
Adjust drag timing
setDragTiming(400, 800, 200);
setDragStepCount(20);
dragDrop(
createLocation(300, 1200),
createLocation(800, 1200)
);
Continue clicking a target
Click a target multiple times.
continueClick(createLocation(540, 1200), {
times: 10,
interval: 100,
randomize: 4
});
Continue clicking an image
continueClick("attack.png", {
times: 20,
interval: 80,
randomize: 3
});
Continue clicking for a duration
continueClick(createLocation(540, 1200), {
duration: 5000,
interval: 120,
randomize: 5
});
Press Back
pressBack();
Press Home
pressHome();
Press Recents
pressRecents();
Open notifications
openNotifications();
sleep(500);
Hide keyboard
hideKeyboard();
Wake the device
wakeup();
Lock the screen
lockScreen();
Use a global action
globalAction(1);
Get screen size
var width = getScreenWidth();
var height = getScreenHeight();
log("Screen: " + width + "x" + height);
Get app usable screen area
var usable = getAppUsableScreenSize();
if (usable) {
highlight(usable, "Usable area");
}
Create common screen regions
var width = getScreenWidth();
var height = getScreenHeight();
var top = createRegion(0, 0, width, Math.floor(height * 0.25));
var middle = createRegion(
0,
Math.floor(height * 0.25),
width,
Math.floor(height * 0.5)
);
var bottom = createRegion(
0,
Math.floor(height * 0.75),
width,
Math.floor(height * 0.25)
);
Create a bottom area helper
function bottomRegion(percent) {
var h = getScreenHeight();
var w = getScreenWidth();
var regionHeight = Math.floor(h * percent);
return createRegion(0, h - regionHeight, w, regionHeight);
}
var bottom = bottomRegion(0.30);
highlight(bottom, "Bottom area");
Click a bottom button by text
var bottom = createRegion(
0,
Math.floor(getScreenHeight() * 0.70),
getScreenWidth(),
Math.floor(getScreenHeight() * 0.30)
);
var button = text("Continue")
.contains()
.in(bottom)
.wait(5);
if (button) {
click(button);
}
Highlight a region
var area = createRegion(100, 500, 800, 300);
highlight(area, "Target area");
Timed highlight
var area = createRegion(100, 500, 800, 300);
highlight(area, 2);
Timed highlight with text
var area = createRegion(100, 500, 800, 300);
highlight(area, {
text: "Checking here",
duration: 2
});
Non-blocking highlight
var area = createRegion(100, 500, 800, 300);
var handle = highlight(area, {
text: "Loading",
duration: 5,
wait: false,
key: "loading-area"
});
sleep(1000);
highlightUpdate(handle, "Still loading...");
sleep(1000);
highlightOff(handle);
Persistent highlight with key
var area = createRegion(100, 500, 800, 300);
var handle = highlight(area, {
text: "Watching this area",
key: "watch-area"
});
sleep(2000);
highlightUpdate("watch-area", "Updated label");
sleep(2000);
highlightOff("watch-area");
Remove one highlight
var area = createRegion(100, 500, 800, 300);
var handle = highlight(area, "Temporary highlight");
sleep(1000);
highlightOff(handle);
Remove all highlights
highlightAllOff();
Basic app launch script
clearReport();
startApp("com.example.app");
sleep(1000);
reportStep("App launch attempted");
Open an app and wait
clearReport();
startApp("com.example.app");
if (waitApp("com.example.app", 5)) {
reportStep("App is foreground");
} else {
reportError("App did not open");
}
Check the foreground app
var foreground = getForegroundApp();
log("Foreground app: " + foreground);
if (foreground !== "com.example.app") {
reportError("Unexpected foreground app");
}
Check if an app is installed
if (isAppInstalled("com.example.app")) {
log("App is installed");
} else {
reportError("App is not installed");
}
Check if an app can be launched
if (!isAppLaunchable("com.example.app")) {
reportError("App has no launch activity");
}
Close an app
var ok = closeApp("com.example.app");
if (!ok) {
reportError("Close app failed");
}
Wait for an app to close
if (waitAppVanish("com.example.app", 5)) {
reportStep("App vanished");
} else {
reportError("App is still foreground");
}
Device information
log("Android SDK: " + getAndroidVersion());
log("Battery: " + getBatteryLevel() + "%");
log("Charging: " + isCharging());
log("Network: " + getNetworkType());
Stop on low battery
var battery = getBatteryLevel();
if (battery <= 15 && !isCharging()) {
scriptExit("Battery is too low: " + battery + "%");
}
Check Wi-Fi
if (!isWifiConnected()) {
reportError("Wi-Fi is not connected");
}
Set volume
var max = getMaxVolume();
setVolume(Math.floor(max * 0.5));
Vibrate
vibrate(300);
Vibration pattern
vibrate([100, 200, 100, 200]);
Simple click helper
function safeClick(target, label) {
var ok = click(target);
if (!ok) {
reportError("Click failed: " + label);
return false;
}
reportStep("Clicked: " + label);
return true;
}
safeClick(createLocation(540, 1200), "Main button");
Safe function for clicking text
function clickText(label, seconds) {
var match = text(label)
.contains()
.wait(seconds || 5);
if (match) {
return click(match);
}
reportError("Text not found: " + label);
return false;
}
clickText("Continue", 5);
Safe function for clicking an image
function clickImage(fileName, seconds, similarity) {
var pattern = createPattern(fileName).similar(similarity || 0.85);
var match = waitFor(pattern, seconds || 5);
if (match) {
return click(match);
}
reportError("Image not found: " + fileName);
return false;
}
clickImage("start_button.png", 5, 0.9);
Safe function for opening an app
function openTargetApp(packageName) {
startApp(packageName);
if (!waitApp(packageName, 8)) {
reportError("App did not open: " + packageName);
return false;
}
return true;
}
if (!openTargetApp("com.example.app")) {
scriptExit("Cannot continue without target app");
}
Retry click helper
function retryClickText(label, times) {
for (var i = 0; i < times; i++) {
var match = text(label).contains().find();
if (match && click(match)) {
return true;
}
sleep(500);
}
return false;
}
if (!retryClickText("Confirm", 3)) {
reportError("Could not click Confirm");
}
Scroll until text appears
var list = createRegion(0, 400, getScreenWidth(), 1300);
var target = null;
for (var i = 0; i < 8; i++) {
target = text("Target Item")
.contains()
.in(list)
.find();
if (target) {
break;
}
scroll(list, "up", 700, 500);
sleep(500);
}
if (target) {
click(target);
} else {
reportError("Target Item was not found after scrolling");
}
Scroll and click all matching items
var list = createRegion(0, 400, getScreenWidth(), 1300);
for (var page = 0; page < 5; page++) {
var items = text("Claim")
.contains()
.in(list)
.findAll();
for (var i = 0; i < items.length; i++) {
click(items[i]);
sleep(300);
}
scroll(list, "up", 700, 500);
sleep(500);
}
Basic debug snapshot helper
function saveDebug(name) {
createDir("debug");
var full = createRegion(0, 0, getScreenWidth(), getScreenHeight());
saveColor(full, "debug/" + name + "_screen.png");
saveReport("debug/" + name + "_report.json");
}
var ok = waitText("Done", 5, false);
if (!ok) {
reportError("Done was not found");
saveDebug("done_not_found");
}
Simple full starter script
clearReport();
setReferenceResolution(1080, 1920);
reportStep("Starting script");
if (!openTargetApp("com.example.app")) {
scriptExit("Target app did not open");
}
sleep(1000);
var start = text("Start")
.contains()
.wait(5);
if (start) {
click(start);
reportStep("Start clicked");
} else {
reportError("Start text was not found");
saveDebug("start_not_found");
}
reportStep("Script finished");
Practical notes for Part 1
-
Use
sleep(...)in milliseconds. -
Use
click(x, y)only when the coordinate is stable. -
Use
createLocation(...)for reusable points. -
Use
createRegion(...)for areas that you want to click, search, highlight, or scroll. -
Use
reportStep(...),reportInfo(...), andreportError(...)to make runs easier to review. -
Use
scriptExit(...)when the script should stop cleanly. -
Use short waits after clicks, app launches, popups, and screen transitions.
-
Use helpers when you repeat the same action many times.
-
Save debug screenshots and reports when something important is not found.