Skip to main content
Known Participant
October 20, 2024
Question

Comparing array values in loop

  • October 20, 2024
  • 1 reply
  • 310 views
Using ExtendScript to assign Tool Diameter values to paths via Graphics styles ...
 
var toolDiameters = [1, 3, 5];  //number value in pts
var scope = idoc.selection;
 
for (var t=0; t < toolDiameters.length; t++){
   if (scope[i].strokeWidth == toolDiameters[t]){
 
The above works but now I am looking to validate the oposite ... strokeWidth != toolDiameter[t]
 
Thanks a bunch ...
This topic has been closed for replies.

1 reply

m1b
Community Expert
Community Expert
October 20, 2024

Hi @nutradial, I think we may need a bit more information. But here goes:

 

(function () {

    var toolDiameters = [1, 3, 5];  //number value in pts

    var doc = app.activeDocument,
        items = doc.selection;

    for (var i = 0, item; i < items.length; i++) {

        item = items[i];

        if (hasWrongStrokeWidth(item)) {
            // example only!
            item.strokeWidth = 10;
        }

    }

/**
 * Returns true when `item` has a wrong stroke width.
 * @param {PageItem} item - the item to check.
 * @returns {Boolean}
 */
    function hasWrongStrokeWidth(item) {

        if (
            item.hasOwnProperty('strokeWidth')
            && item.strokeWidth === 0
        )
            return false;

        for (var t = 0; t < toolDiameters.length; t++)
            if (toolDiameters[t] === item.strokeWidth)
                return false;

        return true;

    };


})();

This script example sets the stroke width of any selected page item whose strokeWidth is not one of your "toolDiameters". 

- Mark

nutradialAuthor
Known Participant
October 20, 2024

Thanks brother ... first time posting here ... I appreciate the help. Next time I will provide full context from the get go.

 

#target "illustrator"

var toolDiameters = [1, 3, 5];

try {
if (documents.length > 0) {

var idoc = app.activeDocument;

var scope = idoc.selection;

if (scope.length > 0) {
for (var i=0; i < scope.length; i++){

if (scope[i].hidden == false) {

for (var t=0; t < toolDiameters.length; t++){

if (scope[i].strokeWidth == toolDiameters[t]){

// transfer valid
if (scope[i].closed == true){
scope[i].name = "Valid";
scope[i].duplicate(idoc.layers.getByName('Transfer'), ElementPlacement.INSIDE);

// run functions here for valid results

}

// transfer valid
if (scope[i].closed == false){
scope[i].name = "Open";
}
}

} if ( *** HELP HERE *** ) { // when "scope[i].strokeWidth" is not in "toolDiameters" array
scope[i].name = "Invalid";
scope[i].selected = false;
}

} if (scope[i].hidden == true) {
scope[i].name = "Hidden";
scope[i].selected = false;
}

} if (idoc.selection == false) {
throw new Error(scriptName + '\nNo valid paths found.');
}

} else {
throw new Error(scriptName + '\nNo valid paths found.');
}

//==================
// MAIN SCRIPT HERE
//==================

} else {
throw new Error(scriptName + '\nPlease open a document before running this script.');
}
} catch (e) {
showError(e);
}

function deselect() {
selection = null;
}

function showError(err) {
if (confirm(scriptName + ': Script error has occurred.\n' +
'Would you like to see more information?', true, 'Unknown Error')) {
alert(err, 'Script Error', true);
}
}