-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch-to-light.js
More file actions
86 lines (69 loc) · 2.89 KB
/
Copy pathswitch-to-light.js
File metadata and controls
86 lines (69 loc) · 2.89 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const sketch = require('sketch');
const document = sketch.getSelectedDocument();
const selectedLayers = document.selectedLayers;
/**
* REVERSED Renaming rules configuration.
* Swapping Dark versions back to Light versions.
*/
const RENAMING_RULES = [
{ search: "onDark", replace: "onLight" },
{ search: "DarkUI", replace: "LightUI" }
];
function main() {
// Filter only Artboards from the current selection
const selectedArtboards = selectedLayers.layers.filter(layer => layer.type === 'Artboard');
if (selectedArtboards.length === 0) {
sketch.UI.message("❌ Please select one or more Artboards");
return;
}
let changeCount = 0;
let alreadyLightCount = 0; // Counter for symbols already in Light state
let missingSymbols = new Set();
const allSymbols = document.getSymbols();
// Iterate through each selected Artboard
selectedArtboards.forEach(artboard => {
// Find all symbol instances inside the current artboard
const instances = sketch.find('SymbolInstance', artboard);
instances.forEach(instance => {
const master = instance.master;
if (!master) return;
const currentName = master.name;
// 1. Check if the symbol is ALREADY in Light state
const isAlreadyLight = RENAMING_RULES.some(r => currentName.endsWith(r.replace));
if (isAlreadyLight) {
alreadyLightCount++;
return;
}
// 2. Find a matching rule for the dark version
let ruleFound = RENAMING_RULES.find(r => currentName.endsWith(r.search));
if (ruleFound) {
const newMasterName = currentName.replace(ruleFound.search, ruleFound.replace);
const newMaster = allSymbols.find(s => s.name === newMasterName);
if (newMaster) {
if (instance.symbolId !== newMaster.symbolId) {
instance.symbolId = newMaster.symbolId;
changeCount++;
}
} else {
// Collect the name of the missing "Light" version
missingSymbols.add(newMasterName);
}
}
});
});
// --- Final Notifications ---
const artboardCount = selectedArtboards.length;
let resultMsg = `Processed ${artboardCount} artboard(s). Swapped to Light: ${changeCount}`;
if (alreadyLightCount > 0) {
resultMsg += ` (Skipped ${alreadyLightCount} already light)`;
}
sketch.UI.message(`☀️ ${resultMsg}`);
if (missingSymbols.size > 0) {
const missingList = Array.from(missingSymbols).join('\n• ');
sketch.UI.alert(
"Missing Light Symbols",
`The script found dark symbols, but their Light versions are missing in the document:\n\n• ${missingList}`
);
}
}
main();