Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mouse-drag 和 ocr 支持多cpu架构 #29

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# 1. clean dist
rm -rf ./dist

# 2. tsc compile
# 2. tsc compile
`npm bin`/tsc

# 3. move resouce
# 3. move resource
cp -r ./resource ./dist/resource

# 4. delete unused resource files
rm -rf ./dist/resource/swift/src
rm -rf ./dist/resource/swift/build.sh

# 4. replace ts-node to node
grep -rl 'ts-node' ./dist/bin | xargs sed -i '' 's/ts-node/node/g'
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "macaca-macos",
"version": "0.2.32",
"version": "0.3.0",
"description": "Macaca MacOS driver",
"keywords": [
"macos",
Expand Down Expand Up @@ -37,7 +37,6 @@
"macaca-ecosystem": "1",
"mocha": "8",
"nyc": "^13.1.0",
"power-assert": "^1.6.1",
"ts-node": "^10.9.1",
"typescript": "4"
},
Expand Down
File renamed without changes.
Binary file added resource/swift/mouse-drag-x64
Binary file not shown.
File renamed without changes.
Binary file added resource/swift/ocr-x64
Binary file not shown.
1 change: 0 additions & 1 deletion resource/swift/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ bash ./build.sh
- mouse-drag
- mouse drag to (+x,+y) relate to current point
- mouse-drag delay x y

2 changes: 1 addition & 1 deletion src/core/jxa/osaUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const scptDir = `${Helper.getResourcePath()}/applescript/scpt`;
// 直接执行 AppleScript 脚本/脚本包
export const osaUtil = {

async execAppleScriptStr(funcStr): Promise<any> {
async execAppleScriptStr(funcStr: string): Promise<any> {
return new Promise((resolve, reject) => {
applescript.execString(funcStr, (err, rtn) => {
if (err) {
Expand Down
7 changes: 6 additions & 1 deletion src/driver/mouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { EDriver } from '../core/enums';
import { osaUtil } from '../core/jxa/osaUtil';
import { jxaUtil } from '../core/jxa/jxaUtil';
import ScreenDriver from './screen';
import os from 'os';
import assert from 'assert';
import fs from 'fs';

export default class MouseDriver {
mouseMoveTo(x: number, y: number) {
Expand Down Expand Up @@ -92,7 +95,9 @@ export default class MouseDriver {
}
// default swift
const curr_pos = this.mouseGetPos();
shell.exec(`${Helper.getResourcePath()}/swift/mouse-drag 10 ${curr_pos.x} ${curr_pos.y} ${x} ${y}`, { silent: true });
const cmdFile = `${Helper.getResourcePath()}/swift/mouse-drag-${os.arch()}`;
assert(fs.existsSync(cmdFile), `不支持的架构: ${os.arch()}`);
shell.exec(`${cmdFile} 10 ${curr_pos.x} ${curr_pos.y} ${x} ${y}`, { silent: true });
}

mouseGetPos() {
Expand Down
21 changes: 18 additions & 3 deletions src/driver/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import shell from 'shelljs';
import robot from 'robotjs';
import fs from 'fs';
import { Helper } from '../core/helper';
import os from 'os';
import assert from 'assert';

export default class ScreenDriver {

Expand All @@ -11,10 +13,17 @@ export default class ScreenDriver {
* 暴露ocr方法,支持通过重写使用三方能力替代
*/
async fileOcr(imgFile: string): Promise<{
rect: { left, top, height, width };
rect: {
left: number;
top: number;
height: number;
width: number;
};
word: string;
}[]> {
const resStr = shell.exec(`${Helper.getResourcePath()}/swift/ocr ${imgFile}`, { silent: true }).stdout;
const cmdFile = `${Helper.getResourcePath()}/swift/ocr-${os.arch()}`;
assert(fs.existsSync(cmdFile), `不支持的架构: ${os.arch()}`);
const resStr = shell.exec(`${cmdFile} ${imgFile}`, { silent: true }).stdout;
return JSON.parse(resStr);
}

Expand Down Expand Up @@ -100,7 +109,13 @@ export default class ScreenDriver {
return resultList;
}

screenGetSize() {
/**
* 含头部的系统状态栏
*/
screenGetSize(): {
width: number;
height: number;
} {
return robot.getScreenSize();
}

Expand Down
2 changes: 1 addition & 1 deletion test/jxaUtil.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jxaUtil } from '../src/core/jxa/jxaUtil';
import MacacaMacOS from '../src/macaca-macos';
import assert from 'power-assert';
import assert from 'assert';

describe.skip('jxaUtil unit testing', function() {

Expand Down
4 changes: 2 additions & 2 deletions test/macaca-macos.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import MacacaMacOS from '../src/macaca-macos';
import { Helper } from '../src/core/helper';
import { EDriver } from '../src/core/enums';
import assert from 'power-assert';
import assert from 'assert';

describe('macaca-macos unit testing', function() {
this.timeout(0);
process.env.MACACA_MACOS_DEBUG = 'true';
const driver = new MacacaMacOS();
let res;
let res: any;

it.skip('isAppRunning should be ok', async () => {
this.timeout(0);
Expand Down
2 changes: 1 addition & 1 deletion test/osaUtil.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { osaUtil } from '../src/core/jxa/osaUtil';
import MacacaMacOS from '../src/macaca-macos';
import assert from 'power-assert';
import assert from 'assert';

describe('osaUtil unit testing', function() {

Expand Down