Copy link to clipboard
Copied
I'm having trouble with a script... I need to be able to open a .ai or .pdf file in Illustrator and maintain it's current layer structure... I then need to be able to select all and copy paste to another Template file I already have set up... I've found that by checking the "Paste Remeber Layers" option in the layers palette that I can do this successfully if I'm doing it manually... but I'm wanting to build it into a script. I've attached what I have below... but it keeps hanging at the active Document line... I'm sure the code I've pasted below is pretty rough... but It kinda works... I'm building it in ESTK and running from there and after it errors I click stop and the image will then paste... but obviously something is causing it to crash... - Thanks for any help
PS. on the bottom part where it's centering on the artboard... is it possible to have it perform this not matter how few or many layers there are...?
var mFile = File('~/Desktop/Illustrator Hot Folder/11 x 8.5 Zund-Proof-Hi-Res/HI-RES/133119 - 237 Win Patch.tif - HR.pdf');
app.open(mFile);
app.executeMenuCommand('selectall');
app.executeMenuCommand('copy');
app.executeMenuCommand('close');
//Open template File
var PFSTKTemp = File('~/Desktop/Illustrator Hot Folder/11 x 8.5 Proof Sticker/ai_file/Proof Sticker 11 x 8.5.ait');
app.open(PFSTKTemp);
//Fit artboard To window
app.executeMenuCommand ('fitall');
app.executeMenuCommand('paste');
//Center on Artboard [0]
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
var aDoc = app.activeDocument; ///Hangs up here////
var abIdx = aDoc.artboards.getActiveArtboardIndex();
var actAbBds = aDoc.artboards[abIdx].artboardRect;
var obj2move = aDoc.selection[0];
obj2move.position = new Array ((actAbBds[2]-actAbBds[0])/2 - obj2move.width/2, (actAbBds[3]-actAbBds[1])/2 + obj2move.height/2);
Copy link to clipboard
Copied
Ok. I wish i had a really good explanation for what's happening, but unfortunately i don't. But i did figure out way to fix it. The solution is to leave mFile open until you're finished doing whatever you need to in PFSTKTemp. Then when that task is done, close the source file. Try this:
function container()
{
//open the destination file first
var PFSTKTemp = File('~/Desktop/testDir/to.ai');
app.open(PFSTKTemp);
var aDoc = app.activeDocument;
//now open the file from the hot folder
var mFile = File('~/Desktop/testDir/from.ai');
app.open(mFile);
var bDoc = app.activeDocument;
app.executeMenuCommand('selectall');
app.executeMenuCommand('copy');
//artwork has been successfully copied
//so lets activate the destination file
aDoc.activate();
//Fit artboard To window
app.executeMenuCommand ('fitall');
app.executeMenuCommand('paste');
//Center on Artboard [0]
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
var abIdx = aDoc.artboards.getActiveArtboardIndex();
var actAbBds = aDoc.artboards[abIdx].artboardRect;
var obj2move = aDoc.selection[0];
obj2move.position = new Array ((actAbBds[2]-actAbBds[0])/2 - obj2move.width/2, (actAbBds[3]-actAbBds[1])/2 + obj2move.height/2);
//your task has been completed, so let's close the source file.
bDoc.close(SaveOptions.DONOTSAVECHANGES);
}
container();
Copy link to clipboard
Copied
just make a copy of the file (or maybe, I do not understand what you want )) )?
Copy link to clipboard
Copied
O-marat. If i understand the workflow properly, it looks like the information is being pulled from the source file and placed onto a pre-made proof template. The douments aren't the same, so duplicating the document wouldn't work.
Correct me if i'm wrong Babymac08​
Copy link to clipboard
Copied
That's correct... now can I complicate it one more step... any way on the source file to Select all the elements of (ie: Layer 1) and group them, then Subsequent layers... Leaving the layer structure intact... Then do the select all and copy command...
Copy link to clipboard
Copied
No. Sorry. You can't have a group that contains items from multiple layers. A group must be contained within a common layer. You can't have items of a group living on separate layers.
Copy link to clipboard
Copied
Sorry.. I might not have explained it well... when I ran your revised script It worked well with the exception... all the items that were located in my "Artwork" layer were still individual items so when it ran the center on artboard portion of the command, it didn't move all the elements of the artwork layer together, which then destroys the original composition... Same goes for my other layers (ie: Through Cut, Kiss Cut, etc)... What I'd love to do is group all the items on "Artwork" Layer as i item under that group... then go to the next layer (ie: Through Cut) and do the same thing.. continuing on until all layers have the individual items grouped... Then go back to the portion of the script you revised - Select all Layers, Copy and so on.....
Does this make better sense?
Copy link to clipboard
Copied
Assuming you don't have any sublayers inside of your existing top level layers, you could do something like this:
function groupLayers(layer)
{
var newGroup = layers.groupItems.add();
for(var a = layer.pageItems.length-1;a >0; a--)
{
var thisItem = layer.pageItems;
thisItem.moveToBeginning(newGroup);
}
}
for(var b=0;b<aDoc.layers.length;b++)
{
var thisLay = aDoc.layers;
groupLayers(thisLay);
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now