Script to rotate Items according to a name

Explorer ,
Oct 26, 2020

Copy link to clipboard

Copied

Hey guys,

I got two docs with the same items. The only difference is, that some of the items in file B got a different ending of their names. File A  File B 

Layers.png

Left: File A, Right: File B

If an Item in File B got a number after underscore, the item in File A (that shares the same name before the underscore) should rotate for that exact amount.

E.g. piece "02_" in File A should rotate for 96 degrees , piece "05_" for 180 degrees.

My solution looks like this, but it´s not working.

function main()
{
    var ADoc= app.activeDocument;
    var BDoc = app.activeDocument[1];
    
    for(var i=0; i<ADoc.pageItems.length; i++);
    {
        var AItems = ADoc.pageItems[i];
        var AItems_name = AItems.name.split('_')[0];
        // Part of the PageItems name before the underscore
        for (var k=0; k<BDoc.pageItems.length; k++)
        {
            var BItems = BDoc.pageItems[k];
            if(BItems.name.IndexOf(AItems_name) != -1);
            // Checks if a pageItem from BDoc got the name of a pageItem from ADoc inside its name
            {
                var angle = BItems.name.split('_')[1];
                AItems.rotate(angle);
                }
            }
        }
    }
main();

If anybody got a solution and could explain me why my code doesn´t work I would really really appreciate it 🙂 🙂

 

Furthermore I tried to "import" a swatch from File B to File A. I used a script I found in another thread but it´s not working.

#target Illustrator

var docRef = app.activeDocument[0];

var docRef1 = app.activeDocument[1];

  // Get specific swatch

  var Designcolor = docRef1.swatches.getByName('Fading Sky');

  // Add Swatch to Original Document

  var AddColor = docRef.swatches.add();

  AddColor.name = Designcolor.name;

  AddColor.color = Designcolor.color

 While debugging it says for line var Designcolor = docRef1.swatches.getByName('Fading Sky') "undefined is not an object". Again an explanation why the code is not working would come in very helpful!

 

Thanks in advance for any help guys!!! 🙂

Adobe Community Professional
Correct answer by Charu Rajput | Adobe Community Professional

@DriveDressler 

For swatches issue: There is an issue with your script as below

 

You have used app.activeDocument[0] and app.activeDocument[1, as explained earlier, activeDocument refers to the topmost. activeDocument and app.documents[0], refer to the top-most. So you should use app.documents[index], when you are accessing multiple document.

 

Try following snippet for swatches, also make sure your FileA is open at the top most. By this I mean the artwork for this file should be visible and FileB must be behind it.

#target Illustrator

var docRef = app.documents[0];  // Refer to File A
var docRef1 = app.documents[1]; // Refer to File B
var swatchName = 'Fading Sky';  // Use swatch Name that exists in File B
// Get specific swatch
try {
    var Designcolor = docRef1.swatches.getByName(swatchName);

    // Add Swatch to Original Document

    var AddColor = docRef.swatches.add();
    AddColor.name = Designcolor.name;
    AddColor.color = Designcolor.color;
} catch (e) {
    alert('Swatch with name ' + swatchName + ' does not exists.')
}

 

If your FileA is not at top most, it will not give you desired result because in above script app.documents[1] is the file from which we are copying the swatch 'Fading Sky' and this exists inside FileB. If you make FileB as at topmost document(as an active document) inside the Illustrtor, script will search swatch with name 'Fading Sky' in FileA, which does not exists and give an error that you are getting right now.

 

I hope this gives some clarification.

TOPICS
Scripting

Views

92

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

Script to rotate Items according to a name

Explorer ,
Oct 26, 2020

Copy link to clipboard

Copied

Hey guys,

I got two docs with the same items. The only difference is, that some of the items in file B got a different ending of their names. File A  File B 

Layers.png

Left: File A, Right: File B

If an Item in File B got a number after underscore, the item in File A (that shares the same name before the underscore) should rotate for that exact amount.

E.g. piece "02_" in File A should rotate for 96 degrees , piece "05_" for 180 degrees.

My solution looks like this, but it´s not working.

function main()
{
    var ADoc= app.activeDocument;
    var BDoc = app.activeDocument[1];
    
    for(var i=0; i<ADoc.pageItems.length; i++);
    {
        var AItems = ADoc.pageItems[i];
        var AItems_name = AItems.name.split('_')[0];
        // Part of the PageItems name before the underscore
        for (var k=0; k<BDoc.pageItems.length; k++)
        {
            var BItems = BDoc.pageItems[k];
            if(BItems.name.IndexOf(AItems_name) != -1);
            // Checks if a pageItem from BDoc got the name of a pageItem from ADoc inside its name
            {
                var angle = BItems.name.split('_')[1];
                AItems.rotate(angle);
                }
            }
        }
    }
main();

If anybody got a solution and could explain me why my code doesn´t work I would really really appreciate it 🙂 🙂

 

Furthermore I tried to "import" a swatch from File B to File A. I used a script I found in another thread but it´s not working.

#target Illustrator

var docRef = app.activeDocument[0];

var docRef1 = app.activeDocument[1];

  // Get specific swatch

  var Designcolor = docRef1.swatches.getByName('Fading Sky');

  // Add Swatch to Original Document

  var AddColor = docRef.swatches.add();

  AddColor.name = Designcolor.name;

  AddColor.color = Designcolor.color

 While debugging it says for line var Designcolor = docRef1.swatches.getByName('Fading Sky') "undefined is not an object". Again an explanation why the code is not working would come in very helpful!

 

Thanks in advance for any help guys!!! 🙂

Adobe Community Professional
Correct answer by Charu Rajput | Adobe Community Professional

@DriveDressler 

For swatches issue: There is an issue with your script as below

 

You have used app.activeDocument[0] and app.activeDocument[1, as explained earlier, activeDocument refers to the topmost. activeDocument and app.documents[0], refer to the top-most. So you should use app.documents[index], when you are accessing multiple document.

 

Try following snippet for swatches, also make sure your FileA is open at the top most. By this I mean the artwork for this file should be visible and FileB must be behind it.

#target Illustrator

var docRef = app.documents[0];  // Refer to File A
var docRef1 = app.documents[1]; // Refer to File B
var swatchName = 'Fading Sky';  // Use swatch Name that exists in File B
// Get specific swatch
try {
    var Designcolor = docRef1.swatches.getByName(swatchName);

    // Add Swatch to Original Document

    var AddColor = docRef.swatches.add();
    AddColor.name = Designcolor.name;
    AddColor.color = Designcolor.color;
} catch (e) {
    alert('Swatch with name ' + swatchName + ' does not exists.')
}

 

If your FileA is not at top most, it will not give you desired result because in above script app.documents[1] is the file from which we are copying the swatch 'Fading Sky' and this exists inside FileB. If you make FileB as at topmost document(as an active document) inside the Illustrtor, script will search swatch with name 'Fading Sky' in FileA, which does not exists and give an error that you are getting right now.

 

I hope this gives some clarification.

TOPICS
Scripting

Views

93

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
Oct 26, 2020 0
Most Valuable Participant ,
Oct 26, 2020

Copy link to clipboard

Copied

The strings in your first example likely need to be converted to actual values using parseInt() or whatever. I'm also not clear what you want with indexOf(), as it returns the instance of a string or the number of instances, but doesn't really do anything to process your values. My brain is kinda munch at the moment, but your loop seems to basically do nothing at all and is just a bunch of redundant/ nonsensical string functions cobbled together.

 

Mylenium

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
Reply
Loading...
Oct 26, 2020 0
Engaged ,
Oct 26, 2020

Copy link to clipboard

Copied

I am not at my PC, so I cannot run your code, but before anything else activeDocument is not a collection.  So your first two lines (in both snippets) should contain activeDocument and documents[1], respectively, or documents[0] and documents[1], respectively. 

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
Reply
Loading...
Oct 26, 2020 1
Adobe Community Professional ,
Oct 26, 2020

Copy link to clipboard

Copied

Hi, @DriveDressler 

Try following snippet for rotation, Make sure your FileA is an active document, that means it must be open in front.

 

function main() {
    var ADoc = app.documents[0];
    var BDoc = app.documents[1];

    for (var i = 0; i < ADoc.groupItems.length; i++) {
        var AItem = ADoc.groupItems[i];
        var AItem_name = AItem.name.split('_')[0];
        // Part of the PageItems name before the underscore
        for (var k = 0; k < BDoc.groupItems.length; k++) {
            var angle = 0;
            var BItems = BDoc.groupItems[k];
            if (BItems.name.indexOf(AItem_name) != -1)
            // Checks if a pageItem from BDoc got the name of a pageItem from ADoc inside its name
            {
                angle = BItems.name.split('_')[1];
                if (angle != '')
                    AItem.rotate(angle);
                break;
            }
        }
    }
}
main();

 

As explained by @femkeblanco , activeDocument is not a collection, activeDocument refers to the front most document that is visble at the top. Also, in your code, make sure you never put ; after if or for statement. These are minor mistakes in your script.

Let us know how it works for you.

 

Best regards

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
Reply
Loading...
Oct 26, 2020 3
Explorer ,
Oct 26, 2020

Copy link to clipboard

Copied

Thank you very much @Charu Rajput !! 🙂 Works perfect! And thank you for your explanation, this helped me a lot 🙂

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
Reply
Loading...
Oct 26, 2020 1
Adobe Community Professional ,
Oct 26, 2020

Copy link to clipboard

Copied

You're welcome @DriveDressler .

We are here to help you guys in all possible ways.

Glad it works for you and I am able to make you understand 🙂

Best regards

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
Reply
Loading...
Oct 26, 2020 0
Adobe Community Professional ,
Oct 26, 2020

Copy link to clipboard

Copied

@DriveDressler 

For swatches issue: There is an issue with your script as below

 

You have used app.activeDocument[0] and app.activeDocument[1, as explained earlier, activeDocument refers to the topmost. activeDocument and app.documents[0], refer to the top-most. So you should use app.documents[index], when you are accessing multiple document.

 

Try following snippet for swatches, also make sure your FileA is open at the top most. By this I mean the artwork for this file should be visible and FileB must be behind it.

#target Illustrator

var docRef = app.documents[0];  // Refer to File A
var docRef1 = app.documents[1]; // Refer to File B
var swatchName = 'Fading Sky';  // Use swatch Name that exists in File B
// Get specific swatch
try {
    var Designcolor = docRef1.swatches.getByName(swatchName);

    // Add Swatch to Original Document

    var AddColor = docRef.swatches.add();
    AddColor.name = Designcolor.name;
    AddColor.color = Designcolor.color;
} catch (e) {
    alert('Swatch with name ' + swatchName + ' does not exists.')
}

 

If your FileA is not at top most, it will not give you desired result because in above script app.documents[1] is the file from which we are copying the swatch 'Fading Sky' and this exists inside FileB. If you make FileB as at topmost document(as an active document) inside the Illustrtor, script will search swatch with name 'Fading Sky' in FileA, which does not exists and give an error that you are getting right now.

 

I hope this gives some clarification.

Best regards

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
Reply
Loading...
Oct 26, 2020 4
Explorer ,
Oct 26, 2020

Copy link to clipboard

Copied

Same goes for this code @Charu Rajput , works perfect! Now I understand the difference between app.document and app.activeDocument. Rookie mistakes 😛 Thank you very much!!

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
Reply
Loading...
Oct 26, 2020 1
Resources