Hi, everyone!
I am a beginner in the scripting world and, as I try to study and understand JavaScript, I analyze existing scripts and work a lot with cutting, pasting, and rewriting things.
Recently I put together a functional script to change the color, weight and style of the inner horizontal lines of selected cells of a table. It worked pretty well, but I would like to check if there is more than one cell selected before run the script and I couldn't find a way to do this...
Whenever I try to capture the number of selected cells, the script always returns the value "1", regardless of the quantity. Is there a way to change this behavior? I haven't been able to find any information about it so far, which is why I'm starting my participation here in the community. 🙂
Ps: if I try to change the (selectedCells.length != 0) to (selectedCells.length > 1), it just goes to the second-to-last "else" block.
The code in question is right below.
Thank you in advance for any help!
---
(function () {
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS; // Ensures the unit of measurement
function main() {
// Checks if the selection is valid
if (app.selection != 0 && app.selection[0].constructor.name === "Cell") {
var selectedCells = app.selection;
// Checks if the selection covers more than one cell
if (selectedCells.length != 0) {
for (var i = 0; i < selectedCells.length; i++) {
var cell = selectedCells[i];
var appLanguage = app.locale;
// Apply styles to the selection
if (app.activeDocument.swatches.item("Grafite") == null) {
alert("The swatch \"Grafite\" does not exist.");
break;
} else {
if (appLanguage == Locale.PORTUGUESE_LOCALE) {
cell.innerRowStrokeType = app.activeDocument.strokeStyles.item("Pontilhado japonês");
} else if (appLanguage == Locale.ENGLISH_LOCALE) {
cell.innerRowStrokeType = app.activeDocument.strokeStyles.item("Japanese Dots");
} else {
alert("This script does not work in your language.");
break;
}
cell.innerRowStrokeWeight = 1; // The unit of measurement is in pts
cell.innerRowStrokeColor = app.activeDocument.swatches.item("Grafite");
}
}
} else {
alert("Please select at least two vertically adjacent cells.");
}
} else {
alert("Invalid selection. Please select at least two vertically adjacent cells.");
}
}
}());