Skip to main content
Participant
June 1, 2020
Answered

Separate all objects in one layer to new layers

  • June 1, 2020
  • 4 replies
  • 1935 views

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

This topic has been closed for replies.
Correct answer Laubender

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 )

4 replies

Participant
February 24, 2023

Hello, I currently have the same problem of wanting to move all objects in my InDesign File into seperate Layers via Script. Unfortunately none of these answers seem to work. I copy & pasted the "correct" Code from Laubender and saved it as a .scpt – but when I try to run the script I get Error -2700 about "uncompiled changes that cannot be run". I'm not the smartest when it comes to coding so I really have no idea what this could even mean. Hope someone can help me!

Community Expert
February 24, 2023

"I copy & pasted the "correct" Code from Laubender and saved it as a .scpt "

 

Hi @Hanke233141517y06 ,

my script code is not for AppleScript.

 

The code is written for ExtendScript (JavaScript).

 

So copy the code to TextEdit and save it without formatting as *.txt file.

After that rename it with suffix *.jsx.

 

How to install and use ExtendScript scripts, see:

https://indiscripts.com/pages/help#hd0sb2

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Community Expert
June 8, 2020

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 )

LaubenderCommunity ExpertCorrect answer
Community Expert
June 8, 2020

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 )

Participant
March 7, 2022

Hello,

 

I came across this in my search for a quick way to seperate out layers in Indesign, anyway someone can advise how to convert this into a script file for InDesign? Complete scripting noob here.

Community Expert
March 7, 2022

Hi Ina,

follow the steps here:

https://www.indiscripts.com/pages/help#hd0sb2

 

Regards,
Uwe Laubender

( ACP )

brian_p_dts
Community Expert
Community Expert
June 1, 2020

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;
}