Highlighted

Separate all objects in one layer to new layers

New Here ,
Jun 01, 2020

Copy link to clipboard

Copied

Is it possible to separate all objects on one into their own individual layers in Indesign?

Adobe Community Professional
Correct answer by Laubender | Adobe Community Professional

Hi Brian,

I would do this spread by spread and to get stacking order right I would loop the allPageItems array of the spread in reverse. Also note, that items in that array are ignored if parent is not the spread itself.

 

( function()
{
	
	app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
	app.doScript
	(
		moveEveryItemToOwnLayer,
		ScriptLanguage.JAVASCRIPT, 
		[], 
		UndoModes.ENTIRE_SCRIPT, 
		"Move every item to its own layer: Spread by Spread | SCRIPT"
	);
		
	function moveEveryItemToOwnLayer()
	{
		
		if( app.documents.length == 0 ){ return };
		
		var doc = app.documents[0];
		var spreads = doc.spreads.everyItem().getElements();
		
		for( var n=0; n<spreads.length; n++ )
		{
			var currentSpread = spreads[n];
			
			// To keep stacking order of elements, loop the array reversed or reverse the array first:
			var allItemsOnSpread = currentSpread.allPageItems.reverse();
			
			for( var a=0; a<allItemsOnSpread.length; a++)
			{
				// Is this a nested element? If so, do nothing and continue loop:
				if( allItemsOnSpread[a].parent != currentSpread ){ continue };
				
				// NOTE: Layer name is e.g. "spread-1-244-Rectangle" for the second spread and a rectangle with id 244.
				var newLayerName = "spread"+"-"+n+"-"+allItemsOnSpread[a].id  +"-"+ allItemsOnSpread[a].constructor.name ;
				var newLayer = doc.layers.add({ name : newLayerName });
				
				// We do not know what the active layer is, so always move the new layer to the top of the stack:
				newLayer.move( LocationOptions.AT_BEGINNING );
				
				// Finally assign the new layer to the page item:
				allItemsOnSpread[a].itemLayer = newLayer ;
			};
		};

	};
	
}() )

 

@Richard,

that script will ignore items on masters and will not work with items that are locked.

To add the said features is a task especially for you.

 

Cheers,
Uwe Laubender

( ACP )

TOPICS
How to, Scripting

Views

369

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Separate all objects in one layer to new layers

New Here ,
Jun 01, 2020

Copy link to clipboard

Copied

Is it possible to separate all objects on one into their own individual layers in Indesign?

Adobe Community Professional
Correct answer by Laubender | Adobe Community Professional

Hi Brian,

I would do this spread by spread and to get stacking order right I would loop the allPageItems array of the spread in reverse. Also note, that items in that array are ignored if parent is not the spread itself.

 

( function()
{
	
	app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
	app.doScript
	(
		moveEveryItemToOwnLayer,
		ScriptLanguage.JAVASCRIPT, 
		[], 
		UndoModes.ENTIRE_SCRIPT, 
		"Move every item to its own layer: Spread by Spread | SCRIPT"
	);
		
	function moveEveryItemToOwnLayer()
	{
		
		if( app.documents.length == 0 ){ return };
		
		var doc = app.documents[0];
		var spreads = doc.spreads.everyItem().getElements();
		
		for( var n=0; n<spreads.length; n++ )
		{
			var currentSpread = spreads[n];
			
			// To keep stacking order of elements, loop the array reversed or reverse the array first:
			var allItemsOnSpread = currentSpread.allPageItems.reverse();
			
			for( var a=0; a<allItemsOnSpread.length; a++)
			{
				// Is this a nested element? If so, do nothing and continue loop:
				if( allItemsOnSpread[a].parent != currentSpread ){ continue };
				
				// NOTE: Layer name is e.g. "spread-1-244-Rectangle" for the second spread and a rectangle with id 244.
				var newLayerName = "spread"+"-"+n+"-"+allItemsOnSpread[a].id  +"-"+ allItemsOnSpread[a].constructor.name ;
				var newLayer = doc.layers.add({ name : newLayerName });
				
				// We do not know what the active layer is, so always move the new layer to the top of the stack:
				newLayer.move( LocationOptions.AT_BEGINNING );
				
				// Finally assign the new layer to the page item:
				allItemsOnSpread[a].itemLayer = newLayer ;
			};
		};

	};
	
}() )

 

@Richard,

that script will ignore items on masters and will not work with items that are locked.

To add the said features is a task especially for you.

 

Cheers,
Uwe Laubender

( ACP )

TOPICS
How to, Scripting

Views

370

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 01, 2020 0
Adobe Community Professional ,
Jun 01, 2020

Copy link to clipboard

Copied

PageItem has a property, itemLayer, that allows you to set the item's layer. To separate everything out, you could do something like this. 

 

var allItems = app.activeDocument.layers.itemByName("Current Layer").pageItems;
for (var i = 0; i < allItems.length; i++) {
    var aNewLayer = app.activeDocument.layers.add();
    allItems[i].itemLayer = aNewLayer;
}

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2020 0
Adobe Community Professional ,
Jun 08, 2020

Copy link to clipboard

Copied

Hi Brian,

I would do this spread by spread and to get stacking order right I would loop the allPageItems array of the spread in reverse. Also note, that items in that array are ignored if parent is not the spread itself.

 

( function()
{
	
	app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
	app.doScript
	(
		moveEveryItemToOwnLayer,
		ScriptLanguage.JAVASCRIPT, 
		[], 
		UndoModes.ENTIRE_SCRIPT, 
		"Move every item to its own layer: Spread by Spread | SCRIPT"
	);
		
	function moveEveryItemToOwnLayer()
	{
		
		if( app.documents.length == 0 ){ return };
		
		var doc = app.documents[0];
		var spreads = doc.spreads.everyItem().getElements();
		
		for( var n=0; n<spreads.length; n++ )
		{
			var currentSpread = spreads[n];
			
			// To keep stacking order of elements, loop the array reversed or reverse the array first:
			var allItemsOnSpread = currentSpread.allPageItems.reverse();
			
			for( var a=0; a<allItemsOnSpread.length; a++)
			{
				// Is this a nested element? If so, do nothing and continue loop:
				if( allItemsOnSpread[a].parent != currentSpread ){ continue };
				
				// NOTE: Layer name is e.g. "spread-1-244-Rectangle" for the second spread and a rectangle with id 244.
				var newLayerName = "spread"+"-"+n+"-"+allItemsOnSpread[a].id  +"-"+ allItemsOnSpread[a].constructor.name ;
				var newLayer = doc.layers.add({ name : newLayerName });
				
				// We do not know what the active layer is, so always move the new layer to the top of the stack:
				newLayer.move( LocationOptions.AT_BEGINNING );
				
				// Finally assign the new layer to the page item:
				allItemsOnSpread[a].itemLayer = newLayer ;
			};
		};

	};
	
}() )

 

@Richard,

that script will ignore items on masters and will not work with items that are locked.

To add the said features is a task especially for you.

 

Cheers,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 08, 2020 0
Adobe Community Professional ,
Jun 08, 2020

Copy link to clipboard

Copied

Hi Richard,

alright then. My code above will not fit exactly your needs, but it is very close and could be used as a good starting point for you. Why?

 

Currently the code will move all items to a new layer.

Not only the items of a specific layer.

 

But as you can see you simply could add yet another if clause that will ask for a specific item layer.

If a page item does not qualify simply loop on with a continue and you are done.

 

Best,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 08, 2020 0