Skip to main content
Inspiring
August 6, 2022
Answered

Copy an object from one artboard to another but delete what's already there - using Javascript?

  • August 6, 2022
  • 1 reply
  • 733 views

Hi all,

Wanted to pick your brains as Google isn't coming up with much.

I have an AI document with over 100 artboards.

Each artboard has a logo made up of five elements. 

This screenshot has blocks of colour over the logo (data protection, sorry).

With your help I'd love to create a script that jumps to, say, artboard 87

Copy the top-left object (which is actually made up of three objects, grouped together).

Jump forward to artboard number 88, delete that same part of the logo

Then paste in place.

 

Is any of that possible with Javascript?

Many thanks if anyone can steer me in the right direction.

J

 

This topic has been closed for replies.
Correct answer femkeblanco

Let me have a think about it and get back to you tomorrow.  


See if this solves the problem.

var input = prompt("Enter source and destination artboard numbers, \
in that order, separated by a comma.", "1,2", " ");
var inputs = input.split(",");
var sourceIndex = Number(inputs[0]) - 1;
var destinationIndex = Number(inputs[1]) - 1;
var doc = app.activeDocument;
var ABs = doc.artboards;
ABs.setActiveArtboardIndex(sourceIndex);
doc.selectObjectsOnActiveArtboard();
var replacement = leftTop(sourceIndex);
replacement.duplicate();
ABs.setActiveArtboardIndex(destinationIndex);
doc.selectObjectsOnActiveArtboard();
var original = leftTop(destinationIndex);
var pos = original.position;
replacement.position = pos;
replacement.moveToEnd(original.parent);
original.remove();
function leftTop(index) {
    var array = [];
    for (var i = 0; i < doc.selection.length; i++) {
        array.push(doc.selection[i]);
    }
    doc.selection = null;
    var O = doc.artboards[index].artboardRect;
    array.sort(function(a, b) {
        var A = Math.sqrt(Math.pow(O[0] - a.left, 2) + Math.pow(O[1] - a.top, 2));
        var B = Math.sqrt(Math.pow(O[0] - b.left, 2) + Math.pow(O[1] - b.top, 2));
        return A > B;
    });
    return array[0];
}

1 reply

femkeblanco
Legend
August 7, 2022

This is a quick demo.

var input = prompt("Enter source and destination artboard numbers, \
in that order, separated by a comma.", "1,2", " ");
var inputs = input.split(",");
var sourceIndex = Number(inputs[0]) - 1;
var destinationIndex = Number(inputs[1]) - 1;
var doc = app.activeDocument;
var ABs = doc.artboards;
ABs.setActiveArtboardIndex(sourceIndex);
doc.selectObjectsOnActiveArtboard();
var replacement = leftTop();
replacement.duplicate();
ABs.setActiveArtboardIndex(destinationIndex);
doc.selectObjectsOnActiveArtboard();
var original = leftTop();
var pos = original.position;
replacement.position = pos;
replacement.moveToEnd(original.parent);
original.remove();
function leftTop() {
    var array = [];
    for (var i = 0; i < doc.selection.length; i++) {
        array.push(doc.selection[i]);
    }
    doc.selection = null;
    array.sort(function(a, b) {
        return (Math.abs(a.top) + a.left) > (Math.abs(b.top) + b.left)
    });
    return array[0];
}
JustyRAuthor
Inspiring
August 7, 2022

WOW! That's amazing!

I had goosebumps and a tingle in my head when that instantly replaced the correct part of the logotype.

How on earth you came up with that so fast beats me. 

I can kind of see what you did. Does push move the first found item to the end of the array list?

I'd love to understand a little more as to how your function works.

Many, many thanks for helping out and solving it in one hit.

femkeblanco
Legend
August 7, 2022

push(), as used above, adds all the looped selected items on a particular artboard to an array.  The reason for this is to use the sort() function to sort the array in a particular order.  In this case, they are sorted (more or less) from closest-to to farthest-from the origin at the top left (by adding the top and the left and sorting from least to most).  The first element in the array is the top left item.  

 

The pseudocode is as follows:

 

- Set the source artboard as active, by way of its index (which is the inputted artboard number minus 1).

- Select the top level items on this active (source) artboard.

- Call the function returning the top left item of these selected items.

- Duplicate this top left item (the replacement).

- Repeat the first three steps on the destination artboard to get the top left item which is to be replaced (the original).

- Get the position of the original.

- Move the replacement to the position of the original.

- Move the replacement to the layer of the original.

- Remove the original.