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

unexpected placement of artboard when copying between files via script

Explorer ,
Apr 10, 2023 Apr 10, 2023

Copy link to clipboard

Copied

I'm working on a script that splits a file with many artboards into several smaller files.

 

The problem is happening when the script tries to create a new board in a destination file. The error is consistent with similar errors I've had when something would cause an artboard to fall off the canvas.

 

When I run the script, it is sometimes able to set up a few boards before running into the error. I can see that the boards in the new file are not in the same position as the old file. The document height/width and the artboardRect are coming directly from the source document/artboard. It doesn't make sense how the placement could change. The script also runs into the error at different times depending on whether I've just opened the source file or if I'm running the script a second time. I don't believe the script makes any change to the source file.

//woven is an array of Board objects. each Board is initailized with an artboard object and an index that references that artboard's position in the document.artboards[] array.
if(woven.length > 0){
    var wovenDoc = app.documents.add(DocumentColorSpace.RGB, currentDoc.width, currentDoc.height);
    for (i=0; i<woven.length; i++) {
      app.activeDocument = currentDoc;
      //copy art
      currentDoc.artboards.setActiveArtboardIndex(woven[i].index);
      currentDoc.selectObjectsOnActiveArtboard();
      app.copy();
      app.selection=[];

      //prepare info for new artboard
      var boardRect = woven[i].artboard.artboardRect;
      var boardName = woven[i].artboard.name;
      
      //create new board and paste art
      app.activeDocument = wovenDoc;
      wovenDoc.artboards.add(boardRect);
      wovenDoc.artboards[wovenDoc.artboards.length-1].name = boardName;
      wovenDoc.artboards.setActiveArtboardIndex(wovenDoc.artboards.length-1);
      app.executeMenuCommand("pasteInPlace");
    }
    //a blank artboard was created when the document was added. it can be removed now.
    wovenDoc.artboards.remove(0);

 

TOPICS
Scripting

Views

312

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

correct answers 1 Correct answer

Explorer , Apr 12, 2023 Apr 12, 2023

The problem was the file, not the script. The original file had artboards very close to the edge of the canvas. The new file's canvas coordinates did not match the original file. I think this is because the origin point for the canvas coordinates isn't actaully the center of the canvas. I might be mistaken, but I think it's the center of the inital artboard present when the file is created. My file used various sized artboards. The difference in artboard size was enough that valid coordinates on

...

Votes

Translate

Translate
Adobe
Guide ,
Apr 10, 2023 Apr 10, 2023

Copy link to clipboard

Copied

I've tried this a few times, and I didn't get an error. 

 

var currentDoc = app.activeDocument;
var woven = [];
for (i = 0; i < app.activeDocument.artboards.length; i++) {
    woven.push(app.activeDocument.artboards[i])
}
if (woven.length > 0) {
    var wovenDoc = app.documents.add(
        DocumentColorSpace.RGB, currentDoc.width, currentDoc.height);
    for (i = 0; i < woven.length; i++) {
        app.activeDocument = currentDoc;
        // copy art
        currentDoc.artboards.setActiveArtboardIndex(i);
        currentDoc.selectObjectsOnActiveArtboard();
        app.copy();
        app.selection = [];
        // prepare info for new artboard
        var boardRect = woven[i].artboardRect;
        var boardName = woven[i].name;
        // create new board and paste art
        app.activeDocument = wovenDoc;
        wovenDoc.artboards.add(boardRect);
        wovenDoc.artboards[wovenDoc.artboards.length - 1].name = boardName;
        wovenDoc.artboards.setActiveArtboardIndex(wovenDoc.artboards.length - 1);
        app.executeMenuCommand("pasteInPlace");
    }
    // a blank artboard was created when the document was added
    // it can be removed now.
    wovenDoc.artboards[0].remove();
}

 

Votes

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
Explorer ,
Apr 10, 2023 Apr 10, 2023

Copy link to clipboard

Copied

that code works for copying all artboards, but the woven array isn't supposed to be a 1:1 match of the entire document's artboard array. It's intended to be a selection of boards based on certain criteria. If woven[i] is not the same artboard as currentDoc.artboards[i], these lines:

currentDoc.artboards.setActiveArtboardIndex(i);
currentDoc.selectObjectsOnActiveArtboard();

would activate the wrong artboard and select the wrong art. That's why my code uses an object to store the artboard along with that artboard's index in the document.

 

But in case the object is what's causing the error, I ran your code on the file that was giving me problems and your code threw the same error. So I think the issue is somehow with the file, not the code.

 

Votes

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
Explorer ,
Apr 12, 2023 Apr 12, 2023

Copy link to clipboard

Copied

LATEST

The problem was the file, not the script. The original file had artboards very close to the edge of the canvas. The new file's canvas coordinates did not match the original file. I think this is because the origin point for the canvas coordinates isn't actaully the center of the canvas. I might be mistaken, but I think it's the center of the inital artboard present when the file is created. My file used various sized artboards. The difference in artboard size was enough that valid coordinates on one file fell off the canvas on a different file.

 

I had to place the artboards using values relative to the boundaries of the new file. After some searching, I found this thread:

https://community.adobe.com/t5/illustrator-discussions/create-first-artboard-on-top-left-of-canvas-h... 

@CarlosCantofigured out a way to find the boundaries of any given file. I will probably include his code any time I need to move artboards around via script.

Votes

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