Copy link to clipboard
Copied
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 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...
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...
Copy link to clipboard
Copied
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];
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks for taking the time to explain it. That's really helpful. Have a great day!
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
I can't immediately see why that would happen. Can you show screenshots of each case?
Copy link to clipboard
Copied
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.
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:
Or 151,182:
If I delete all items on 151 except the square and number, and put the range again as 151,153, this is the result:
If I delete the lower rectangle on 153 and retry, this is the result:
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?
Copy link to clipboard
Copied
Let me have a think about it and get back to you tomorrow.
Copy link to clipboard
Copied
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];
}
Copy link to clipboard
Copied
Thanks ever so much for taking the time to troubleshoot it. From my initial testing, it's working perfectly. Thanks again.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more