Highlighted

Match positions of same named pieces in two different files via script

Explorer ,
Jul 01, 2020

Copy link to clipboard

Copied

Hey guys,

 

I´m using a script (big kudos to CarlosCanto and Charu Rajput) that moves all pieces in File A to the same place as pieces with the same name in File B. But some changes and additional commands would come in handy, so I once again reach out for your help 🙂

 

That´s the script right now:

function main() {
    var newDesign = app.documents[0];
    var nestedDesign = app.documents[1];

  for(var i=0;i<app.activeDocument.pageItems.length;i++){
        
        var _pageItem = app.activeDocument.pageItems[i];
       
        if(_pageItem.name.indexOf('_') != -1){
          
          var angle = _pageItem.name.split('_')[1];
          _pageItem.rotate(angle);
        }
    }
    try {
        var layerToNest = newDesign.layers['pieces'];
    }
    catch (e) {
        alert('"pieces" Layer was not found. Try again.');
        return;
    }

    var piecesToNest = layerToNest.pageItems;

    var items = [], p;

    for (var a=0; a<piecesToNest.length; a++) {
        p = piecesToNest[a];
        
        try {
            p.left = nestedDesign.pageItems[p.name].left;
            p.top = nestedDesign.pageItems[p.name].top;
        }
        catch (e) {
            alert('piece: ' + p.name + ' was not found in Nested Design.');
        }
    }
}
main();

 

At the moment I need to put all my pieces in a new layer called “pieces”. I would prefer this part to be skipped. It should just go through all my existing layers and sublayers from File A and match the pieces with them from File B (as you can see in my example files). If that´s not possible or would be too complex, there is another way. The script then should 1. Copy all visible pieces 2. Create a new layer called “pieces” 3. Delete all old layers (regardless of their name) 4. Paste all into the “pieces” layer.

 

After any of these two methods the script as it is now should start (at first rotate the pieces according to names and then match File A with File B). After this all pieces need to be selected and the artboard should be set to “Fit to selected Art”.

 

I know it´s a lot and probably not easy but it would be amazing if anybody knows how to write this script!! Big thanks in advance 🙂 🙂

File A 

File B 

TOPICS
Scripting

Views

61

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

Match positions of same named pieces in two different files via script

Explorer ,
Jul 01, 2020

Copy link to clipboard

Copied

Hey guys,

 

I´m using a script (big kudos to CarlosCanto and Charu Rajput) that moves all pieces in File A to the same place as pieces with the same name in File B. But some changes and additional commands would come in handy, so I once again reach out for your help 🙂

 

That´s the script right now:

function main() {
    var newDesign = app.documents[0];
    var nestedDesign = app.documents[1];

  for(var i=0;i<app.activeDocument.pageItems.length;i++){
        
        var _pageItem = app.activeDocument.pageItems[i];
       
        if(_pageItem.name.indexOf('_') != -1){
          
          var angle = _pageItem.name.split('_')[1];
          _pageItem.rotate(angle);
        }
    }
    try {
        var layerToNest = newDesign.layers['pieces'];
    }
    catch (e) {
        alert('"pieces" Layer was not found. Try again.');
        return;
    }

    var piecesToNest = layerToNest.pageItems;

    var items = [], p;

    for (var a=0; a<piecesToNest.length; a++) {
        p = piecesToNest[a];
        
        try {
            p.left = nestedDesign.pageItems[p.name].left;
            p.top = nestedDesign.pageItems[p.name].top;
        }
        catch (e) {
            alert('piece: ' + p.name + ' was not found in Nested Design.');
        }
    }
}
main();

 

At the moment I need to put all my pieces in a new layer called “pieces”. I would prefer this part to be skipped. It should just go through all my existing layers and sublayers from File A and match the pieces with them from File B (as you can see in my example files). If that´s not possible or would be too complex, there is another way. The script then should 1. Copy all visible pieces 2. Create a new layer called “pieces” 3. Delete all old layers (regardless of their name) 4. Paste all into the “pieces” layer.

 

After any of these two methods the script as it is now should start (at first rotate the pieces according to names and then match File A with File B). After this all pieces need to be selected and the artboard should be set to “Fit to selected Art”.

 

I know it´s a lot and probably not easy but it would be amazing if anybody knows how to write this script!! Big thanks in advance 🙂 🙂

File A 

File B 

TOPICS
Scripting

Views

62

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
Jul 01, 2020 0

Have something to add?

Join the conversation