Merge fixed content inside specific groups
Hi everyone.
I've got this script that copy the layer "_fixed obj" inside every other layers (except the one which name start with "dima_". It works fine what i want it to place the "_fixed obj" inside a specif group nested into the layers.
Here's the code:
/*
* Description: An Adobe Illustrator script that combine template layers and content layers
* Usage: Rename all "template" layers with an underscore "_" as the first character. Rename all "content" layers with any suitable names
* This is an early version that has not been sufficiently tested. Use at your own risks.
* License: GNU General Public License Version 3. (http://www.gnu.org/licenses/gpl-3.0-standalone.html)
*
* Copyright (c) 2009. William Ngan.
* http://www.metaphorical.net
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var doc = app.activeDocument;
var origlayers = new Array();
// Record all original content layers
for(var i=0; i<doc.layers.length; i++) {
if ( !(doc.layers[i].name.charAt(0)=='_') ) {
origlayers.push( doc.layers[i].name );
}
}
// Generate dima layers
for(var i=0; i<origlayers.length; i++) {
getContent( origlayers[i] );
}
// Distinguish non-dima layers and dima layers by visibility
origlayers = new Array();
for(var i=0; i<doc.layers.length; i++) {
// non-dima
if (doc.layers[i].name.substr(0,5)!='dima_') {
doc.layers[i].visible = true;
origlayers.push( doc.layers[i].name );
// dima
} else {
doc.layers[i].visible = false;
}
}
// Remove non-dima layers
for(var i=0; i<origlayers.length; i++) {
doc.layers.getByName (origlayers[i]).remove();
}
// show first dima layer
doc.layers[0].visible = true;
/**
* opens a resource file and copy the specific layer to page
* @9397041 layername the name of the layer
*/
function getContent(layername) {
// open resource file
var tempdoc = null;
try {
} catch (err) {
throw new Error( 'Cannot create new document' );
}
// create a new layer
var temp_layer = doc.layers.add();
temp_layer.name = 'dima_'+layername;
temp_layer.visible = true;
var gg = temp_layer.groupItems.add();
// get specified layer. all unrelated layers set to invisible
for(var i=doc.layers.length-1; i>=0; i--) {
if (doc.layers[i].name.charAt(0)=='_') { // persistent layers with "_"
doc.layers[i].visible = true;
group( gg, doc.layers[i].pageItems, true );
} else if (doc.layers[i].name.substr(0,5)!='dima_') { // not dima layer
doc.layers[i].visible = false;
}
}
try {
var content = doc.layers.getByName(layername);
} catch (err) {
throw new Error('Cannot get contents from '+layername+'* layer');
}
// group content
content.visible = true;
group( gg, content.pageItems, true );
}
function group( gg, items, isDuplicate ) {
for(var i=items.length-1; i>=0; i--) {
if (items[i]!=gg) {
if (isDuplicate) {
newItem = items[i].duplicate (gg, ElementPlacement.PLACEATEND);
} else {
items[i].move( gg, ElementPlacement.PLACEATEND );
}
}
}
}
app.activeDocument.layers.getByName('dima_dima_var').remove();
Here's the screenshot of the starting layers composition:

After the script it's like this:

I'd like if those fixed objects will be place inside the relative group.
So the groups of fixed objects named "back" will go into the group named "back", the ones named "front" will go into "front" and so on.
Note: every layers ("dima_top_X") will have the same items structure inside of it, whit the same names.
Is it possible?
Please tell me if i need to provide more info to hele me.
Thanks.
