Node v6系で動かないOSX向けモジュールを動かせるようにした際に見つけたrobotjs
おはようございます。本日、海外出張から奥さんが帰国するに伴い、実家から、子供達と引き上げます。心配していた便秘の次男のウンチ問題も、実家滞在中にも複数回、トイレで出せて、ホッとしています。@kjunichiです。
背景
screenresというnode.jsでディスプレイの解像度を変更するモジュールを見つけたものの、 nodeのv6ではnpm installしたらビルドエラー
nanを最新化して、ネイティブコードの該当箇所を対応して、.nodeモジュールを作れたが、 実行すると
dyld: lazy symbol binding failed: Symbol not found: _CGMainDisplayID Referenced from: /Users/kjw_junichi/work/nodejs/screenres/node_modules/screenres/build/Release/screenres.node Expected in: flat namespace dyld: Symbol not found: _CGMainDisplayID Referenced from: /Users/kjw_junichi/work/nodejs/screenres/node_modules/screenres/build/Release/screenres.node Expected in: flat namespace Trace/BPT trap: 5
とシンボル参照エラー
そこで、Cocoaのframeworkを使ったnpmモジュールのリンク方法を参考にすれば、 問題が解決の糸口が見つかるとの期待で、Cocoa APIをつかったnpmモジュールを探した。
robotjsというマウス操作がjsで行えるモジュールを発見
マウスを矩形波の軌跡を描くように動かす
//Move the mouse across the screen as a sine wave. const robot = require("robotjs"); function myFunc(x,width) { const twoPI = Math.PI * 2.0; const xx = (twoPI * x) / width; let ans=0; for(let k =1; k < 16; k++) { ans = ans + Math.sin((2*k-1)*(2*Math.PI*xx*0.5))/(2*k-1) } return (2/Math.PI)*ans; } //Speed up the mouse. robot.setMouseDelay(2); const screenSize = robot.getScreenSize(); const height = (screenSize.height / 2) - 10; const width = screenSize.width; for (let x = 0; x < width; x++) { y = height * myFunc(x,width) + height; robot.moveMouse(x, y); }
元々の作業進捗
node v6対応に加えてRetina対応もできた。あとはコードの清書。 / “GitHub - kjunichi/screenres at node-v6” https://t.co/BfG8wPxwsn
— kjunichi (@kjunichi) 2016年6月24日