-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.js
63 lines (50 loc) · 1.62 KB
/
process.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import fs from "fs";
import Jimp from "jimp";
import applescript from 'applescript'
import { config } from "./config.js";
import { homedir } from "os";
const generateRandomHEX = () => {
let letters = '0123456789ABCDEF';
return letters.split('').reduce((res) => {
res += letters[Math.floor(Math.random() * 16)];
return res
}, '#').slice(0, 7);
}
const runAppleScript = (fileName) => {
const script = `tell application "Finder" to set desktop picture to file "${fileName}"`
return new Promise((resolve) => {
applescript.execString(script, (error) => {
if (error) {
console.log('error on applescript run', error);
}
resolve();
});
});
}
const process = async (config) => {
const { resolution, timeout, saveSource, pathDist } = config;
const color = generateRandomHEX();
const fileName = color + '.png'
// Creating an image
const image = new Jimp(resolution.width, resolution.height, color, (err) => {
if (err) throw err;
console.log(`${new Date().toLocaleString()} new image. color: ${color}`);
});
// Writing an image
await image.writeAsync(homedir() + pathDist + fileName);
// Setting as wallpaper image
await runAppleScript(fileName)
// Deleting image file
if (!saveSource) {
await fs.unlink(homedir() + pathDist + fileName, (error) => {
if (error) {
console.log('error on image unlink', error)
}
})
}
// Doin again
setTimeout(() => {
process(config)
}, timeout * 1000 * 60)
}
process(config);