Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Explosion-Scratch/ba6f792684da6337593d695ede46536a to your computer and use it in GitHub Desktop.
Save Explosion-Scratch/ba6f792684da6337593d695ede46536a to your computer and use it in GitHub Desktop.
Set the mac accent color to the closest matching color that matches your wallpaper
#!/usr/bin/env deno
import Vibrant from "npm:node-vibrant"
import cd from "npm:color-difference"
const COLORS: { [number | string]: string } = {
4: "#336ED1",
5: "#7F2689",
6: "#B95081",
0: "#AC312D",
1: "#C2682A",
2: "#C49C37",
3: "#569436",
"-1": "#848484",
}
const M1_COLORS = {
3: "#D5B96E",
4: "#5C7C81",
5: "#557087",
6: "#A6585A",
7: "#606286",
8: "#CC8161",
}
const wp_process = Deno.run({
cmd: [
"osascript",
"-e",
'tell app "finder" to get posix path of (get desktop picture as alias)'
],
stdout: "piped"
})
const wp_path = new TextDecoder().decode(await wp_process.output()).trim();
console.log(wp_path);
console.log(cd);
const palette = await Vibrant.from(wp_path).getPalette();
console.log(palette.Vibrant.getHex());
const COLORS_TO_COMPARE = [palette.Vibrant, palette.DarkVibrant]//, palette.LightVibrant, palette.Muted, palette.LightMuted, palette.DarkMuted];
let colors: { type: 'none' | 'm1' | 'default', id: string | number, dist: number }[] = [];
for (const c of COLORS_TO_COMPARE) {
for (const [k, v] of Object.entries(COLORS)) {
colors.push({
type: "default",
id: k,
dist: cd.compare(c.getHex(), v),
})
}
for (const [k, v] of Object.entries(M1_COLORS)) {
colors.push({
type: "m1",
id: k,
dist: cd.compare(c.getHex(), v),
})
}
}
const min = colors.reduce((a, b) => a.dist < b.dist ? a : b);
// if (min.type === 'default') {
// await Deno.run({
// cmd: ["defaults", "write", "-g", "NSColorSimulateHardwareAccent", "-bool", "NO"]
// })
// await Deno.run({
// cmd: ["defaults", "write", "-g", "AppleAccentColor", "-int", min.id.toString()]
// })
// } else if (min.type === 'm1') {
// await Deno.run({
// cmd: ["defaults", "write", "-g", "NSColorSimulateHardwareAccent", "-bool", "YES"]
// })
// await Deno.run({
// cmd: ["defaults", "write", "-g", "NSColorSimulatedHardwareEnclosureNumber", "-int", min.id.toString()]
// })
// }
let p;
if (min.type === 'm1') {
p = Deno.run({
cmd: ["defaults", "write", "-g", "NSColorSimulateHardwareAccent", "-bool", "YES"]
})
await p.status();
p = Deno.run({
cmd: ["defaults", "write", "-g", "NSColorSimulatedHardwareEnclosureNumber", "-int", min.id.toString()]
})
}
try {
p = Deno.run({
cmd: ['killall', "System Settings"]
})
} catch (error) {
}// console.log('Set accent color', min);
await p.status();
const BUTTON_INDEX = min.type === 'default' ? Object.keys(COLORS).indexOf(min.id.toString()) + 6 : 13;
console.log("IDX:", BUTTON_INDEX)
const SCRIPT = `tell application "System Events"
tell application "System Settings"
activate
end tell
tell process "System Settings"
repeat until exists list 1 of window 1
delay 0.5
end repeat
select row 14 of outline 1 of scroll area 1 of list 1 of splitter group 1 of list 1 of window 1
click button ${BUTTON_INDEX} of group 1 of scroll area 1 of group 1 of list 2 of splitter group 1 of list 1 of window "Appearance"
end tell
tell application "System Settings"
quit
end tell
end tell`
const process = Deno.run({
cmd: ['osascript', '-e', SCRIPT],
stdout: "piped",
stderr: "piped"
})
console.log(await process.output())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment