sendkeys-macos
Advanced tools
Comparing version 1.0.1 to 1.1.0
# Changelog | ||
## [1.1.0](https://www.github.com/socsieng/sendkeys-macos/compare/v1.0.1...v1.1.0) (2020-09-18) | ||
### Features | ||
* add support for sticky pause ([133db1c](https://www.github.com/socsieng/sendkeys-macos/commit/133db1c53a22b7e34160acad73c2ab58ad1947f4)) | ||
* make application name optional ([9e519c4](https://www.github.com/socsieng/sendkeys-macos/commit/9e519c4300b1da03a6331ba93a21e6ed5e286cb2)) | ||
### [1.0.1](https://www.github.com/socsieng/sendkeys-macos/compare/v1.0.0...v1.0.1) (2020-09-17) | ||
@@ -4,0 +12,0 @@ |
{ | ||
"name": "sendkeys-macos", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Sends keystrokes to a given application with customizable delays to simulate typing", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -67,2 +67,4 @@ # SendKeys for macOS | ||
`<P:seconds>` (note upper case `P`) can be used to modify the default delay between subsequent keystrokes. | ||
### Special key combinations | ||
@@ -69,0 +71,0 @@ |
@@ -15,12 +15,6 @@ const fs = require('fs'); | ||
function generate(text, app, delay, initialDelay) { | ||
const lines = text.split(lineEndingExpression); | ||
const commands = lines | ||
.map( | ||
(line, index) => | ||
` type(${JSON.stringify(line + (index === lines.length - 1 ? '' : '\r'))}, ${delay}, { delay, sysevents });`, | ||
) | ||
.join(`\n`); | ||
const commands = `type(${JSON.stringify(text.replace(lineEndingExpression, '\r'))}, ${delay}, { delay, sysevents });`; | ||
return template | ||
.replace(REPLACE_APPLICATION_NAME, JSON.stringify(app)) | ||
.replace(REPLACE_APPLICATION_NAME, app || '') | ||
.replace(REPLACE_INITIAL_DELAY, initialDelay) | ||
@@ -27,0 +21,0 @@ .replace(REPLACE_TYPE_FUNCTION, type.toString()) |
@@ -1,60 +0,17 @@ | ||
const app = Application(/** application-name **/); | ||
const appName = "/** application-name **/"; | ||
const app = appName ? Application(appName) : null; | ||
const sysevents = Application('System Events'); | ||
const KEYS = { | ||
'f1': 122, | ||
'f2': 120, | ||
'f3': 99, | ||
'f4': 118, | ||
'f5': 96, | ||
'f6': 97, | ||
'f7': 98, | ||
'f8': 100, | ||
'f9': 101, | ||
'f10': 109, | ||
'f11': 103, | ||
'f12': 111, | ||
'esc': 53, | ||
'return': 36, | ||
'enter': 76, | ||
'delete': 51, | ||
'space': 49, | ||
'tab': 48, | ||
'up': 126, | ||
'down': 125, | ||
'left': 123, | ||
'right': 124, | ||
'home': 115, | ||
'end': 119, | ||
'pgup': 116, | ||
'pgdown': 121, | ||
}; | ||
const MODIFIERS = { | ||
'⌘': 'command down', | ||
'^': 'control down', | ||
'⌥': 'option down', | ||
'⇧': 'shift down', | ||
'command': 'command down', | ||
'control': 'control down', | ||
'option': 'option down', | ||
'shift': 'shift down', | ||
}; | ||
// matches <p:0.1> | ||
const pauseExpression = /^\<p:([\d.]+)\>/i; | ||
// matches <c:down>, <c:up:command> | ||
const charExpression = /^\<c:(.|[\w]+)(:([,\w⌘^⌥⇧]+))?\>/i; | ||
// matches <\> | ||
const continuationExpression = /^\<\\\>/; | ||
/** type-function **/ | ||
function run(input, parameters) { | ||
app.activate(); | ||
if (app) { | ||
app.activate(); | ||
} | ||
delay(/** initial-delay **/); | ||
/** type-commands **/ | ||
/** type-commands **/ | ||
return input; | ||
} |
107
src/type.js
@@ -1,50 +0,1 @@ | ||
const KEYS = { | ||
f1: 122, | ||
f2: 120, | ||
f3: 99, | ||
f4: 118, | ||
f5: 96, | ||
f6: 97, | ||
f7: 98, | ||
f8: 100, | ||
f9: 101, | ||
f10: 109, | ||
f11: 103, | ||
f12: 111, | ||
esc: 53, | ||
return: 36, | ||
enter: 76, | ||
delete: 51, | ||
space: 49, | ||
tab: 48, | ||
up: 126, | ||
down: 125, | ||
left: 123, | ||
right: 124, | ||
home: 115, | ||
end: 119, | ||
pgup: 116, | ||
pgdown: 121, | ||
}; | ||
const MODIFIERS = { | ||
'⌘': 'command down', | ||
'^': 'control down', | ||
'⌥': 'option down', | ||
'⇧': 'shift down', | ||
'command': 'command down', | ||
'control': 'control down', | ||
'option': 'option down', | ||
'shift': 'shift down', | ||
'cmd': 'command down', | ||
'ctrl': 'control down', | ||
'alt': 'option down', | ||
}; | ||
// matches <p:0.1> | ||
const pauseExpression = /^\<p:([\d.]+)\>/; | ||
// matches <c:down>, <c:up:command> | ||
const charExpression = /^\<c:(.|[\w]+)(:([,\w⌘^⌥⇧]+))?\>/; | ||
// matches <\> | ||
const continuationExpression = /^\<\\\>/; | ||
/** | ||
@@ -57,4 +8,52 @@ * Types text input with a default delay between each character. | ||
*/ | ||
function type(text, defaultDelay, { delay, sysevents }) { | ||
const KEYS = { | ||
f1: 122, | ||
f2: 120, | ||
f3: 99, | ||
f4: 118, | ||
f5: 96, | ||
f6: 97, | ||
f7: 98, | ||
f8: 100, | ||
f9: 101, | ||
f10: 109, | ||
f11: 103, | ||
f12: 111, | ||
esc: 53, | ||
return: 36, | ||
enter: 76, | ||
delete: 51, | ||
space: 49, | ||
tab: 48, | ||
up: 126, | ||
down: 125, | ||
left: 123, | ||
right: 124, | ||
home: 115, | ||
end: 119, | ||
pgup: 116, | ||
pgdown: 121, | ||
}; | ||
const MODIFIERS = { | ||
'⌘': 'command down', | ||
'^': 'control down', | ||
'⌥': 'option down', | ||
'⇧': 'shift down', | ||
'command': 'command down', | ||
'control': 'control down', | ||
'option': 'option down', | ||
'shift': 'shift down', | ||
'cmd': 'command down', | ||
'ctrl': 'control down', | ||
'alt': 'option down', | ||
}; | ||
function type(text, defaultDelay, { delay, sysevents }) { | ||
// matches <p:0.1> | ||
const pauseExpression = /^\<(p):([\d.]+)\>/i; | ||
// matches <c:down>, <c:up:command> | ||
const charExpression = /^\<c:(.|[\w]+)(:([,\w⌘^⌥⇧]+))?\>/; | ||
// matches <\> | ||
const continuationExpression = /^\<\\\>/; | ||
let index = 0; | ||
@@ -65,5 +64,11 @@ | ||
if (pauseMatch) { | ||
const value = parseFloat(pauseMatch[1]); | ||
const value = parseFloat(pauseMatch[2]); | ||
delay(value); | ||
index += pauseMatch[0].length; | ||
// update default delay | ||
if (pauseMatch[1] === 'P') { | ||
defaultDelay = value; | ||
} | ||
return true; | ||
@@ -70,0 +75,0 @@ } |
@@ -5,6 +5,10 @@ const type = require('./type'); | ||
function charactSequenceFromKeystrokes(keystrokeCalls) { | ||
function characterSequenceFromKeystrokes(keystrokeCalls) { | ||
return keystrokeCalls.map(call => call.args[0]).join(''); | ||
} | ||
function getPauseSequenceFromDelays(delayCalls) { | ||
return delayCalls.map(call => call.args[0]); | ||
} | ||
describe('type', () => { | ||
@@ -137,10 +141,31 @@ const delay = sinon.spy(); | ||
it('should ignore upper case pause', () => { | ||
type('<P:1>a', 0.1, { delay, sysevents }); | ||
it('should remember sticky pause', () => { | ||
type('<P:1>abc', 0.1, { delay, sysevents }); | ||
const sequence = charactSequenceFromKeystrokes(keystroke.getCalls()); | ||
const keystrokeSequence = characterSequenceFromKeystrokes(keystroke.getCalls()); | ||
const delaySequence = getPauseSequenceFromDelays(delay.getCalls()); | ||
expect(sequence).to.equal('<P:1>a'); | ||
expect(delay.callCount).to.equal(6); | ||
expect(keystrokeSequence).to.equal('abc'); | ||
expect(delaySequence).to.eql([1, 1, 1, 1]); | ||
}); | ||
it('should remember sticky pause mid sequence', () => { | ||
type('ab<P:1>cd', 0.1, { delay, sysevents }); | ||
const keystrokeSequence = characterSequenceFromKeystrokes(keystroke.getCalls()); | ||
const delaySequence = getPauseSequenceFromDelays(delay.getCalls()); | ||
expect(keystrokeSequence).to.equal('abcd'); | ||
expect(delaySequence).to.eql([0.1, 1, 1, 1]); | ||
}); | ||
it('should remember sticky pause mid sequence with additional change in pause', () => { | ||
type('ab<P:1>cd<p:0.2>ef<P:3>ghi', 0.1, { delay, sysevents }); | ||
const keystrokeSequence = characterSequenceFromKeystrokes(keystroke.getCalls()); | ||
const delaySequence = getPauseSequenceFromDelays(delay.getCalls()); | ||
expect(keystrokeSequence).to.equal('abcdefghi'); | ||
expect(delaySequence).to.eql([0.1, 1, 1, 0.2, 1, 3, 3, 3, 3]); | ||
}); | ||
}); | ||
@@ -173,3 +198,3 @@ | ||
const sequence = charactSequenceFromKeystrokes(keystroke.getCalls()); | ||
const sequence = characterSequenceFromKeystrokes(keystroke.getCalls()); | ||
@@ -182,3 +207,3 @@ expect(sequence).to.equal('p:1>'); | ||
const sequence = charactSequenceFromKeystrokes(keystroke.getCalls()); | ||
const sequence = characterSequenceFromKeystrokes(keystroke.getCalls()); | ||
@@ -185,0 +210,0 @@ expect(sequence).to.equal('c:up:shift>'); |
Sorry, the diff of this file is not supported yet
888634
100
314