Question
Why is the script not able to use users colour selection?
Hello
So there are two scripts
A controler and a main.
The script automattically adds rules
the user can select parameters in the controller

which writes to an defaults.xml file
the main uses all parameters in making the rules but cant seem to get the colour
Also would be nice for the menu for colours to show an icon of the colour - any ideas there?
Thanks
Smyth
Controller
var f = File($.fileName).parent + "/defaults.xml";
var xml = readXML(f);
if (xml == "") {
xml = XML("<dialog><values><top>0</top><bottom>0</bottom><stroke>1</stroke><colorSwatch>None</colorSwatch></values></dialog>");
}
var tv = Number(xml.values.top);
var bv = Number(xml.values.bottom);
var sv = Number(xml.values.stroke);
var selectedSwatchName = xml.values.colorSwatch;
var t, b, s, c;
makeDialog();
function makeDialog() {
var d = app.dialogs.add({ name: "autoRule Controller (。◕‿◕。)", canCancel: true });
with (d.dialogColumns.add()) {
staticTexts.add({ staticLabel: "Adjust top height:" });
staticTexts.add({ staticLabel: "Adjust bottom height:" });
staticTexts.add({ staticLabel: "Adjust stroke weight:" });
staticTexts.add({ staticLabel: "Select color swatch:" });
}
with (d.dialogColumns.add()) {
t = measurementEditboxes.add({ editUnits: MeasurementUnits.POINTS, editValue: tv, minWidth: 50 });
b = measurementEditboxes.add({ editUnits: MeasurementUnits.POINTS, editValue: bv, minWidth: 50 });
s = measurementEditboxes.add({ editUnits: MeasurementUnits.POINTS, editValue: sv, minWidth: 50 });
// Get list of swatch names and add dropdown
var swatchNames = getSwatchNames();
c = dropdowns.add({ stringList: swatchNames, selectedIndex: getSwatchIndex(swatchNames, selectedSwatchName), minWidth: 150 });
}
if (d.show() == true) {
t = t.editValue;
b = b.editValue;
s = s.editValue;
var selectedSwatchIndex = c.selectedIndex;
selectedSwatchName = swatchNames[selectedSwatchIndex];
xml.values.top = t;
xml.values.bottom = b;
xml.values.stroke = s;
xml.values.colorSwatch = selectedSwatchName;
writeXML(f, xml.toXMLString());
d.destroy();
}
}
function writeXML(p, s) {
var file = new File(p);
file.encoding = 'UTF-8';
file.open('w');
file.write(s);
file.close();
}
function readXML(p) {
var f = new File(p);
f.open("r");
var t = f.read();
f.close();
return XML(t);
}
// Function to get list of swatch names
function getSwatchNames() {
var swatchNames = ["None"]; // Default option
var myDocument = app.activeDocument;
var swatches = myDocument.swatches;
for (var i = 0; i < swatches.length; i++) {
swatchNames.push(swatches[i].name);
}
return swatchNames;
}
// Function to get index of selected swatch name
function getSwatchIndex(swatchNames, selectedSwatchName) {
for (var i = 0; i < swatchNames.length; i++) {
if (swatchNames[i] === selectedSwatchName) {
return i;
}
}
return 0; // Default to the first swatch if not found
}
Main
// Load user-defined values from XML
var f = File($.fileName).parent + "/defaults.xml";
var xml = readXML(f);
var userDefinedAmountTop = Number(xml.values.top);
var userDefinedAmountBottom = Number(xml.values.bottom);
var userDefinedStrokeWeight = Number(xml.values.stroke);
var selectedSwatchName = xml.values.colorSwatch;
// Main code below
if (app.documents.length > 0) {
var doc = app.activeDocument;
var selectedFrames = doc.selection;
if (selectedFrames.length >= 2) {
selectedFrames.sort(function(a, b) {
return a.geometricBounds[1] - b.geometricBounds[1];
});
app.doScript(function() {
for (var i = 0; i < selectedFrames.length - 1; i++) {
var currentFrame = selectedFrames[i];
var nextFrame = selectedFrames[i + 1];
if (currentFrame.parentPage === nextFrame.parentPage) {
var centerX = (currentFrame.geometricBounds[3] + nextFrame.geometricBounds[1]) / 2;
var ruleHeight = Math.max(
currentFrame.geometricBounds[2] - currentFrame.geometricBounds[0],
nextFrame.geometricBounds[2] - nextFrame.geometricBounds[0]
);
var startHeight = Math.max(
currentFrame.geometricBounds[2],
nextFrame.geometricBounds[2]
);
var endHeight = Math.min(
currentFrame.geometricBounds[0],
nextFrame.geometricBounds[0]
);
var adjustedStartHeight = startHeight + userDefinedAmountBottom;
var adjustedEndHeight = endHeight - userDefinedAmountTop;
var newLine = currentFrame.parentPage.graphicLines.add();
newLine.strokeWeight = userDefinedStrokeWeight;
var selectedSwatch = getSwatchByName(selectedSwatchName, doc);
if (selectedSwatch) {
if (selectedSwatch.colorValue && selectedSwatch.colorValue instanceof Array) {
newLine.strokeColor = selectedSwatch.colorValue;
} else {
// Use a default color (e.g., black) if the color value is not recognized
newLine.strokeColor = doc.swatches.item("Black").colorValue;
}
} else {
// Use a default color (e.g., black) if the swatch is not found
newLine.strokeColor = doc.swatches.item("Black").colorValue;
}
newLine.paths[0].entirePath = [
[centerX, adjustedStartHeight],
[centerX, adjustedEndHeight]
];
newLine.sendToBack();
}
}
}, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "Add Vertical Lines Between Text Boxes");
} else {
alert("Please select at least two text frames.");
}
} else {
alert("Open a document before running this script.");
}
function readXML(p) {
var f = new File(p);
f.open("r");
var t = f.read();
f.close();
return XML(t);
}
// Function to get color swatch by name
function getSwatchByName(name, document) {
var swatch = null;
var swatches = document.swatches;
for (var i = 0; i < swatches.length; i++) {
if (swatches[i].name === name) {
swatch = swatches[i];
break;
}
}
return swatch;
}