RobotJSはデスクトップ操作をNode.jsで自動化してくれます。
マウス操作自動化
サンプルのままですがこんなコードでマウス制御できます。
app.js
'use strict';
// Move the mouse across the screen as a sine wave.
const robot = require("robotjs");
// Speed up the mouse.
robot.setMouseDelay(2);
const twoPI = Math.PI * 2.0;
const screenSize = robot.getScreenSize();
const height = (screenSize.height / 2) - 10;
const width = screenSize.width;
for (let x = 0; x < width; x++){
const y = height * Math.sin((twoPI * x) / width) + height;
robot.moveMouse(x, y);
}
Raspberry Piへのインストールでエラーでました
$ npm i --save robotjs
> robotjs@0.4.7 install /home/pi/n0bisuke/splatoon_audio/robottest/node_modules/robotjs
> prebuild-install || node-gyp rebuild
prebuild-install info begin Prebuild-install version 2.4.1
prebuild-install info looking for local prebuild @ prebuilds/robotjs-v0.4.7-node-v59-linux-arm.tar.gz
prebuild-install info looking for cached prebuild @ /home/pi/.npm/_prebuilds/https-github.com-octalmage-robotjs-releases-download-v0.4.7-robotjs-v0.4.7-node-v59-linux-arm.tar.gz
prebuild-install http request GET https://github.com/octalmage/robotjs/releases/download/v0.4.7/robotjs-v0.4.7-node-v59-linux-arm.tar.gz
prebuild-install http 404 https://github.com/octalmage/robotjs/releases/download/v0.4.7/robotjs-v0.4.7-node-v59-linux-arm.tar.gz
prebuild-install WARN install No prebuilt binaries found (target=9.2.0 runtime=node arch=arm platform=linux)
make: Entering directory '/home/pi/n0bisuke/splatoon_audio/robottest/node_modules/robotjs/build'
CXX(target) Release/obj.target/robotjs/src/robotjs.o
cc1plus: warning: command line option '-Wbad-function-cast' is valid for C/ObjC but not for C++
In file included from ../src/keypress.h:6:0,
from ../src/robotjs.cc:7:
../src/keycode.h:103:23: fatal error: X11/Xutil.h: No such file or directory
#include <X11/Xutil.h>
^
compilation terminated.
robotjs.target.mk:112: recipe for target 'Release/obj.target/robotjs/src/robotjs.o' failed
make: *** [Release/obj.target/robotjs/src/robotjs.o] Error 1
make: Leaving directory '/home/pi/n0bisuke/splatoon_audio/robottest/node_modules/robotjs/build'
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/home/pi/.nodebrew/node/v9.2.0/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:258:23)
gyp ERR! stack at ChildProcess.emit (events.js:159:13)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:209:12)
gyp ERR! System Linux 4.4.21-v7+
gyp ERR! command "/home/pi/.nodebrew/node/v9.2.0/bin/node" "/home/pi/.nodebrew/node/v9.2.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/pi/n0bisuke/splatoon_audio/robottest/node_modules/robotjs
gyp ERR! node -v v9.2.0
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
npm WARN robottest@1.0.0 No description
npm WARN robottest@1.0.0 No repository field.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! robotjs@0.4.7 install: `prebuild-install || node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the robotjs@0.4.7 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/pi/.npm/_logs/2017-12-13T08_00_26_277Z-debug.log
たぶんX11/Xutil.h: No such file or directory
のところが怪しい
X11/Xutil.h: No such file or directory
https://stackoverflow.com/questions/5299989/x11-xlib-h-not-found-in-ubuntu
ここによると
$ sudo apt-get install libx11-dev
でOKらしい
再実行したらまたエラーが
../src/mouse.c:12:35: fatal error: X11/extensions/XTest.h: No such file or directory
#include <X11/extensions/XTest.h>
X11/extensions/XTest.h: No such file or directory
https://stackoverflow.com/questions/5073040/how-to-find-x11-extensions-xtest-h
ここを参考に以下を順番に実行
$ sudo apt-get install apt-file
$ sudo apt-file update
$ apt-file search "X11/extensions/XTest.h"
$ sudo apt-get install libxtst-dev
これで再度インストールをするとうまくいきました。