文章附件下载:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:3941
这个Auto.js脚本实现了以下功能:
完整的UI界面,包含录制控制按钮和状态显示
屏幕点击动作录制功能,记录点击坐标和时间间隔
图像识别功能,可以匹配预设的模板图像
颜色识别工具,长按屏幕获取指定位置的颜色值
操作日志记录和显示功能
自动生成可执行的脚本代码并保存到文件
配置选项控制是否显示提示和保存日志
// 全能脚本录制器 v1.0
// 功能:屏幕录制+图像识别+颜色识别+点击操作
"ui";
ui.layout(
);
// 全局变量
let recording = false;
let actions = [];
let startTime = 0;
let lastActionTime = 0;
const config = {
saveLog: true,
showToast: true
};
// 主功能函数
function startRecording() {
if (recording) return;
recording = true;
actions = [];
startTime = Date.now();
lastActionTime = startTime;
ui.statusText.setText("状态: 录制中...");
ui.recordBtn.setText("录制中");
toast("开始录制脚本");
log("开始录制脚本");
// 注册全局事件监听
events.on("click", function(point) {
if (!recording) return;
const now = Date.now();
actions.push({
type: "click",
x: point.x,
y: point.y,
time: now - lastActionTime
});
lastActionTime = now;
log(`记录点击: (${point.x}, ${point.y})`);
});
}
function stopRecording() {
if (!recording) return;
recording = false;
events.removeAllListeners();
ui.statusText.setText("状态: 录制完成");
ui.recordBtn.setText("开始录制");
toast("停止录制");
log("停止录制");
// 生成脚本代码
generateScript();
}
function captureAndRecognize() {
toast("请选择截图区域");
log("开始截图识别");
threads.start(function() {
let img = captureScreen();
let template = images.read("/sdcard/template.png");
if (template) {
let result = findImage(img, template);
if (result) {
log(找到匹配图像,位置: (${result.x}, ${result.y})
);
toast(找到图像 (${result.x}, ${result.y})
);
} else {
log("未找到匹配图像");
toast("未找到匹配图像");
}
} else {
log("请先保存模板图像到/sdcard/template.png");
toast("缺少模板图像");
}
});
}
function colorPicker() {
toast("长按屏幕获取颜色");
log("启动取色工具");
threads.start(function() {
while (true) {
let point = clickable().waitFor();
let color = images.pixel(point.x, point.y);
let hex = colors.toString(color);
log(获取颜色: ${hex} 位置: (${point.x}, ${point.y})
);
toast(颜色: ${hex}
);
}
});
}
// 辅助函数
function generateScript() {
let script = "// 自动生成的脚本\n";
script += "// 录制时间: " + new Date(startTime).toLocaleString() + "\n\n";
script += "auto();\n";
script += "setScreenMetrics(1080, 1920); // 根据实际设备修改\n\n";
for (let action of actions) {
if (action.type === "click") {
script += `sleep(${action.time});\n`;
script += `click(${action.x}, ${action.y});\n`;
}
}
script += "\n// 脚本结束";
log("生成的脚本:\n" + script);
// 保存到文件
files.write("/sdcard/auto_script.js", script);
toast("脚本已保存到/sdcard/auto_script.js");
}
function log(message) {
let now = new Date().toLocaleTimeString();
ui.logText.setText(ui.logText.getText() + \n[${now}] ${message}
);
}
function toast(message) {
if (config.showToast) {
ui.post(() => {
android.widget.Toast.makeText(context, message, 1).show();
});
}
}
// 事件绑定
ui.recordBtn.click(startRecording);
ui.stopBtn.click(stopRecording);
ui.captureBtn.click(captureAndRecognize);
ui.colorBtn.click(colorPicker);
ui.saveLog.on("check", function(checked) {
config.saveLog = checked;
});
ui.showToast.on("check", function(checked) {
config.showToast = checked;
});