• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Illustrator script that will select multiple objects with different names?

New Here ,
Nov 29, 2022 Nov 29, 2022

Copy link to clipboard

Copied

I've got this script to select only objects with the name "holes 1."

function selectPageItemsByName(items, name) {

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

        var item = items[i];

        if (item.name === name) {

            item.selected = true;

        }

    }

}

function main() {

    var document = app.activeDocument;

    var name = 'holes 1';

    document.selection = null;

    selectPageItemsByName(document.pageItems, name);

}

main();

 How do I get it to select objects with the name "holes 1" AND objects with the name "backs 1"?

TOPICS
Scripting

Views

454

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 29, 2022 Nov 29, 2022

Copy link to clipboard

Copied

Hi @LuxMarie4310, you could use a regex (grep) for the name variable:

function selectPageItemsByName(items, name) {

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

        var item = items[i];

        if (name.test(item.name)) {

            item.selected = true;

        }

    }

}

function main() {

    var document = app.activeDocument;

    var name = /^(holes 1|backs 1)$/;

    document.selection = null;

    selectPageItemsByName(document.pageItems, name);

}

main();

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 29, 2022 Nov 29, 2022

Copy link to clipboard

Copied

Or you could use an array of names, and loop through those inside your item loop:

function selectPageItemsByNames(items, names) {

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

        var item = items[i];

        for (var j = 0; j < names.length; j++) {

            if (item.name === names[j]) {

                item.selected = true;

            }

        }

    }

}

function main() {

    var document = app.activeDocument;

    var names = ['holes 1', 'backs 1'];

    document.selection = null;

    selectPageItemsByNames(document.pageItems, names);

}

main();

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 29, 2022 Nov 29, 2022

Copy link to clipboard

Copied

One way.

function selectPageItemsByName(items, name1, name2) {
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        if (item.name === name1 || item.name === name2) {
            item.selected = true;
        }
    }
}

function main() {
    var document = app.activeDocument;
    var name1 = 'holes 1', name2 = 'backs 1';
    document.selection = null;
    selectPageItemsByName(document.pageItems, name1, name2);
}
main();

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 29, 2022 Nov 29, 2022

Copy link to clipboard

Copied

LATEST

Or, using the arguments object:

function selectPageItemsByNames(items) {
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        for (var j = 1; j < arguments.length; j++) {
            if (item.name === arguments[j]) {
                item.selected = true;
            }
        }
    }
}
function main() {
    var document = app.activeDocument;
    document.selection = null;
    selectPageItemsByNames(document.pageItems, 'holes 1', 'backs 1');
}
main();

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines