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

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

Contributor ,
Aug 06, 2022 Aug 06, 2022

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).

Screenshot 2022-08-06 at 22.43.46.png

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

 

TOPICS
Scripting
720
Translate
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 2 Correct answers

Guide , Aug 07, 2022 Aug 07, 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.sel
...
Translate
Guide , Aug 13, 2022 Aug 13, 2022

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(desti
...
Translate
Adobe
Guide ,
Aug 07, 2022 Aug 07, 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];
}
Translate
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
Contributor ,
Aug 07, 2022 Aug 07, 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.

Translate
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 ,
Aug 07, 2022 Aug 07, 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.

Translate
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
Contributor ,
Aug 07, 2022 Aug 07, 2022

Thanks for taking the time to explain it. That's really helpful. Have a great day!

Translate
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
Contributor ,
Aug 12, 2022 Aug 12, 2022

Hi, femkeblanco,

I've been using the script on and off and have found a small issue. If the source artboard is in one column, and the target artboard is in another, the script takes the correct top-left part of the source logo, but replaces the next object down on the target logo.

So based on the screenshot in my first post, it's taking the red square from the source, and replacing the bottom-left rectangle on the target.

Is that something that can be easily fixed?

Translate
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
Contributor ,
Aug 12, 2022 Aug 12, 2022

UPDATE...and sometimes it will take the second object instead of the first from the source artboard. 

I think this happens with updated items. Do they no longer become 0 in the array if you change their colour, or perhaps copy and paste from another artboard?

Translate
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 ,
Aug 12, 2022 Aug 12, 2022

I can't immediately see why that would happen.  Can you show screenshots of each case? 

Translate
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
Contributor ,
Aug 12, 2022 Aug 12, 2022

Ok, I have 184 artboards made up of 30 rows and 5 columns

Here is a mockup of the top-right section of artboards - the numbers are for clarity.

Screenshot 2022-08-12 at 21.00.22.png

Artboard 151 is yellow and the rest are grey so it's easy to see what object is being moved/replaced.

 

If I put 151,153 as the range, this is the result:

Screenshot 2022-08-12 at 21.02.31.png

Or 151,182:

Screenshot 2022-08-12 at 21.03.37.png

If I delete all items on 151 except the square and number, and put the range again as 151,153, this is the result:

Screenshot 2022-08-12 at 21.06.04.png

If I delete the lower rectangle on 153 and retry, this is the result:

Screenshot 2022-08-12 at 21.08.48.png

 

It looks as though the function is not seeing the top-left object as the first object. Somehow, things have swapped around.

Does that help?

Translate
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 ,
Aug 12, 2022 Aug 12, 2022

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

Translate
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 ,
Aug 13, 2022 Aug 13, 2022

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];
}
Translate
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
Contributor ,
Aug 13, 2022 Aug 13, 2022
LATEST

Thanks ever so much for taking the time to troubleshoot it. From my initial testing, it's working perfectly. Thanks again.

Translate
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