macos-defaults/record/mac-runner.js

205 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-11-12 21:23:37 +00:00
const aperture = require('aperture')()
const wait = require('delay')
const fs = require('fs')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
class MacRunner {
constructor(commands = []) {
this.commands = commands
}
/**
* Set MacOS defaults system
* @param {*} domain Application domain
* @param {*} key Default key
* @param {*} params Values for the default
2020-11-27 14:01:10 +00:00
* @param {*} expectedResult Expected defaults read result
* @param {*} after Action to perform after setting the default (ex: killall Finder)
*/
2020-11-27 14:01:10 +00:00
setDefault(domain, key, params, expectedResult, after) {
return this.register(async () => {
const defaultCommand = `defaults write ${domain} ${key} ${params}`
2020-11-27 14:01:10 +00:00
// Retry command until it works (sometimes it doesn't...)
for (let i = 10; i--; i > 0) {
try {
await execCommand(defaultCommand, 100)
const result = await execCommand(`defaults read ${domain} ${key}`, 0)
if (expectedResult === result.trim()) {
break
}
} catch (error) {
console.error(error)
}
if (i === 1) {
throw new Error(`[${defaultCommand}] failed (too much trials)`)
}
}
if (after) await execCommand(after)
})
}
/**
* Read MacOS defaults system
* @param {*} domain Application domain
* @param {*} key Default key
*/
readDefault(domain, key) {
return this.register(async () => {
const defaultCommand = `defaults read ${domain} | grep ${key}`
console.log(await execCommand(defaultCommand, 0))
})
}
/**
* Delete MacOS defaults system
* @param {*} domain Application domain
* @param {*} key Default key
* @param {*} after Action to perform after setting the default (ex: killall Dock)
*/
deleteDefault(domain, key, after) {
2020-11-27 14:01:10 +00:00
return this.register(async () => {
const defaultCommand = `defaults delete ${domain} ${key}`
2020-11-27 14:01:10 +00:00
await execCommand(defaultCommand)
if (after) await execCommand(after)
})
}
/**
* Open an application from it's name
* @param {*} appName Application name (ex: Finder)
* @param {*} params Application parameters
*/
openApp(appName, params = '') {
return this.register(() => execCommand(`open -a "${appName}" ${params}`))
}
/**
2020-11-27 12:45:16 +00:00
* Make active a running application
* @param {*} appName Application name
*/
activateApp(appName) {
2020-11-27 12:45:16 +00:00
return this.register(() =>
execCommand(`osascript -e 'tell application "${appName}" to activate'`)
)
}
/**
* Move and resize an application window
* @param {*} appName Application name
* @param {*} x X coordinate (from the left of the screen)
* @param {*} y Y coordinate (from the top of the screen)
* @param {*} width Width of the app window
* @param {*} height Height of the app window
*/
moveAndResizeApp(appName, x, y, width, height) {
const h = { start: x, end: x + width }
const v = { start: y, end: y + height }
2020-11-27 12:45:16 +00:00
return this.register(() =>
execCommand(
`osascript -e 'tell application "${appName}" to set the bounds of the first window to {${h.start}, ${v.start}, ${h.end}, ${v.end}}'`
)
)
}
/**
* Capture the whole screen into a file
* @param {*} output Output file name (png)
*/
captureScreen(output) {
2020-11-27 14:01:10 +00:00
return this.register(async () => {
await this.wait(2000)
execCommand(`screencapture ${output}`)
})
}
/**
* Capture a screen rect into a file
* @param {*} x X coordinate (from the left of the screen)
* @param {*} y Y coordinate (from the top of the screen)
* @param {*} width Width of the capture
* @param {*} height Height of the capture
* @param {*} output Output file name (png)
*/
captureScreenRect(x, y, width, height, output) {
2020-11-27 12:45:16 +00:00
return this.register(() =>
execCommand(`screencapture -R${x},${y},${width},${height} ${output}`)
)
}
/**
* Capture the app window into a file
* @param {*} appName Application name to capture
* @param {*} output Output file name (png)
*/
captureApp(appName, output) {
2020-11-27 12:45:16 +00:00
return this.register(() =>
execCommand(
`screencapture -o -l$(osascript -e 'tell app "${appName}" to id of window 1') ${output}`
)
)
}
/**
* Start the video recording using aperturejs
* @param {*} options aperturejs options
*/
startVideo(options) {
return this.register(async () => {
console.info(' Start video recording...')
2020-11-12 21:23:37 +00:00
await aperture.startRecording(options)
await aperture.isFileReady
})
}
/**
* Stop the video recording
* @param {*} output video file output
*/
stopVideo(output) {
return this.register(async () => {
console.info(' Stop video recording...')
const fp = await aperture.stopRecording()
if (fs.existsSync(output)) {
fs.unlinkSync(output)
}
2020-11-12 21:23:37 +00:00
fs.renameSync(fp, output)
})
}
/**
* Wait for a given delay
* @param {*} delay Delay in ms
*/
wait(delay) {
return this.register(() => wait(delay))
}
/**
* Execute the runner with all given commands
*/
async run() {
await this.commands.reduce((p, fn) => p.then(fn), Promise.resolve())
}
register(command) {
this.commands.push(command)
return new MacRunner(this.commands)
}
}
async function execCommand(command, delay = 1000) {
console.info(` Command: [${command}]`)
2020-11-27 14:01:10 +00:00
const { stderr, stdout } = await exec(command)
if (stderr) {
throw new Error(stderr)
}
await wait(delay)
2020-11-27 14:01:10 +00:00
return stdout
}
2020-11-27 12:45:16 +00:00
module.exports = MacRunner