keeweb/app/scripts/comp/auto-type/emitter-impl/auto-type-emitter-impl-darw...

100 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-04-09 17:07:53 +02:00
'use strict';
2016-04-09 20:10:11 +02:00
var Launcher = require('../../launcher');
var spawn = Launcher.req('child_process').spawn;
var KeyMap = {
tab: 48, enter: 36, space: 49,
up: 126, down: 125, left: 123, right: 124, home: 115, end: 119, pgup: 116, pgdn: 121,
ins: 114, del: 117, bs: 51, esc: 53,
win: 55, rwin: 55,
f1: 122, f2: 120, f3: 99, f4: 118, f5: 96, f6: 97, f7: 98, f8: 100, f9: 101,
f10: 109, f11: 103, f12: 111, f13: 105, f14: 107, f15: 113, f16: 106,
add: 69, subtract: 78, multiply: 67, divide: 75,
n0: 82, n1: 83, n2: 84, n3: 85, n4: 86,
n5: 87, n6: 88, n7: 89, n8: 91, n9: 92
};
var ModMap = {
'^': 'command',
'+': 'shift',
'%': 'option',
'^^': 'control'
};
2016-04-09 17:07:53 +02:00
var AutoTypeEmitterImpl = function() {
2016-04-09 20:10:11 +02:00
this.mod = {};
2016-04-10 08:32:18 +02:00
this.pendingScript = [];
2016-04-09 17:07:53 +02:00
};
AutoTypeEmitterImpl.prototype.setMod = function(mod, enabled) {
2016-04-09 20:10:11 +02:00
if (enabled) {
this.mod[mod] = true;
} else {
delete this.mod[mod];
}
2016-04-09 17:07:53 +02:00
};
AutoTypeEmitterImpl.prototype.text = function(text, callback) {
2016-04-09 20:10:11 +02:00
if (!text) {
return callback();
}
text = text.replace(/"/g, '\\"');
2016-04-10 08:32:18 +02:00
this.pendingScript.push('keystroke "' + text + '"'+ this.modString());
callback();
2016-04-09 17:07:53 +02:00
};
AutoTypeEmitterImpl.prototype.key = function(key, callback) {
2016-04-09 20:10:11 +02:00
if (typeof key !== 'number') {
if (!KeyMap[key]) {
return callback('Bad key: ' + key);
}
key = KeyMap[key];
}
2016-04-10 08:32:18 +02:00
this.pendingScript.push('key code ' + key + this.modString());
callback();
};
2016-04-17 13:56:19 +02:00
AutoTypeEmitterImpl.prototype.copyPaste = function(text, callback) {
this.pendingScript.push('delay 0.5');
this.pendingScript.push('set the clipboard to "' + text.replace(/"/g, '\\"') + '"');
this.pendingScript.push('delay 0.5');
2016-04-10 08:32:18 +02:00
this.pendingScript.push('keystroke "v" using command down');
2016-04-10 11:10:48 +02:00
this.pendingScript.push('delay 0.5');
2016-04-17 13:56:19 +02:00
callback();
2016-04-09 17:07:53 +02:00
};
AutoTypeEmitterImpl.prototype.waitComplete = function(callback) {
2016-04-10 08:32:18 +02:00
if (this.pendingScript.length) {
var script = this.pendingScript.join('\n');
this.pendingScript.length = 0;
this.runScript(script, callback);
} else {
callback();
}
2016-04-09 17:07:53 +02:00
};
2016-04-09 20:10:11 +02:00
AutoTypeEmitterImpl.prototype.modString = function() {
var keys = Object.keys(this.mod);
if (!keys.length) {
return '';
}
return ' using {' + keys.map(AutoTypeEmitterImpl.prototype.mapMod).join(', ') + '}';
};
AutoTypeEmitterImpl.prototype.mapMod = function(mod) {
return ModMap[mod] + ' down';
};
AutoTypeEmitterImpl.prototype.runScript = function(script, callback) {
2016-04-10 08:32:18 +02:00
script = 'tell application "System Events" \n' + script + '\nend tell';
2016-04-17 23:38:31 +02:00
var ps = spawn('osascript');
ps.stdin.setEncoding('utf-8');
ps.stdin.write(script);
ps.stdin.end();
2016-04-09 20:10:11 +02:00
ps.on('close', function(code) { callback(code ? 'Exit code ' + code : undefined); });
};
2016-04-09 17:07:53 +02:00
module.exports = AutoTypeEmitterImpl;