macos-defaults/record/screenshots/disable-shadow/false.js

86 lines
2.4 KiB
JavaScript
Raw Normal View History

const delay = require('delay')
const glob = require('glob-promise')
const robot = require('robotjs')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const { makeAppActive, compressPngImage } = require('../../utils')
module.exports = {
run: async (outputPath) => {
console.log('> Recording screenshot disable-shadow with param set to false')
2020-11-27 12:45:16 +00:00
const { stderr: setEnvError } = await exec(
'defaults write com.apple.screencapture show-thumbnail -bool false && defaults write com.apple.screencapture disable-shadow -bool false'
)
if (setEnvError) {
2020-11-27 12:45:16 +00:00
console.error(
'An error occured while setting up the screenshot disble-shadow command'
)
logRollbackInfo()
throw new Error(setEnvError)
}
// Preparation
2020-11-27 12:45:16 +00:00
robot.moveMouse(
robot.getScreenSize().width / 2,
robot.getScreenSize().height / 2
)
robot.mouseClick()
await makeAppActive('Safari')
robot.keyTap('l', 'command')
await delay(100)
robot.keyTap('h')
robot.typeString('ttps://www.apple.com/macos/')
await delay(100)
robot.keyTap('enter')
await delay(8000)
// Screenshot
robot.keyTap('4', ['command', 'shift'])
await delay(100)
robot.keyTap('space')
await delay(100)
robot.keyTap('enter')
await delay(100)
robot.keyTap('w', 'command')
await delay(1000)
2020-11-27 12:45:16 +00:00
const screenshot = (
await glob(`/Users/${process.env.USER}/Desktop/*.png`)
).pop()
try {
await compressPngImage(screenshot, outputPath, 'false')
} catch (compressPngImageError) {
logRollbackInfo()
throw new Error(compressPngImageError)
}
2020-11-27 12:45:16 +00:00
const { stderr: deleteEnvError } = await exec(
'defaults delete com.apple.screencapture show-thumbnail && defaults delete com.apple.screencapture disable-shadow'
)
if (deleteEnvError) {
2020-11-27 12:45:16 +00:00
console.error(
'An error occured while cleaning the dock autohide-delay environment'
)
logRollbackInfo()
throw new Error(deleteEnvError)
}
return { filepath: `${outputPath}/false`, isVideo: false }
2020-11-27 12:45:16 +00:00
},
}
function logRollbackInfo() {
2020-11-27 12:45:16 +00:00
console.info(
'Please manually run this command to make sure everything is properly reset:'
)
console.info(
'defaults delete com.apple.screencapture show-thumbnail && defaults delete com.apple.screencapture disable-shadow'
)
}