macos-defaults/record/textedit/RichText/true.js

66 lines
2 KiB
JavaScript
Raw Normal View History

const MacRunner = require('../../mac-runner')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const { compressPngImage } = require('../../utils')
module.exports = {
run: async (outputPath) => {
console.log('> Recording TextEdit opens rich text files to true')
try {
2021-12-20 19:58:38 +00:00
const filename = '/tmp/lorem.rtf'
await manageFile(filename, true)
2021-12-20 19:58:38 +00:00
await addContentToFile(
filename,
2023-08-17 13:54:54 +00:00
'{\\rtf1\\ansi\\ansicpg1252\\cocoartf2577\\cocoatextscaling0\\cocoaplatform0{\\fonttbl}{\\colortbl;\\red255\\green255\\blue255;}{\\*\\expandedcolortbl;;}\\paperw11900\\paperh16840\\margl1440\\margr1440\\vieww11520\\viewh8400\\viewkind0}',
2021-12-20 19:58:38 +00:00
)
const runner = new MacRunner()
await runner
2021-12-20 19:58:38 +00:00
.setDefault('com.apple.TextEdit', 'RichText', '-bool true', '1', '')
.openApp('TextEdit', filename)
2022-08-18 14:12:33 +00:00
.moveAndResizeApp('TextEdit', 0, 0, 740, 400)
.captureApp('TextEdit', `${outputPath}/true.png`)
2021-12-20 19:58:38 +00:00
.deleteDefault('com.apple.TextEdit', 'RichText')
.killApp('TextEdit')
.run()
2021-12-20 19:58:38 +00:00
await manageFile(filename, false)
} catch (runnerError) {
throw new Error(runnerError)
}
try {
await compressPngImage(`${outputPath}/true.png`, outputPath, 'true')
} catch (compressPngImageError) {
throw new Error(compressPngImageError)
}
return { filepath: `${outputPath}/true` }
2021-12-20 19:58:38 +00:00
},
}
async function manageFile(filename, create) {
console.log(` Command: ${create ? 'create' : 'remove'} ${filename}`)
const { stderr: mngFile } = await exec(
2023-08-17 13:54:54 +00:00
`${create ? 'touch' : 'rm -f'} ${filename}`,
)
if (mngFile) {
2021-12-20 19:58:38 +00:00
console.error('An error occured while working with a file')
throw new Error(mngFile)
}
}
async function addContentToFile(filename, content) {
console.log(` Command: add content to ${filename}`)
const { stderr: mngFile } = await exec(
`cat > ${filename} << EOF
${content}
2023-08-17 13:54:54 +00:00
EOF`,
)
if (mngFile) {
2021-12-20 19:58:38 +00:00
console.error('An error occured while working with a file')
throw new Error(mngFile)
}
}