Skip to main content
Participating Frequently
April 1, 2023
Question

Stop a layer from being delted in Indesign 2020 using extend script

  • April 1, 2023
  • 2 replies
  • 419 views

Hi Guys,

 

I am trying to stop a layer from being deletedin Indesign 2020 but i am unable to do so. Basically i have been tagging the layers on some condtion and then if user ties to delete the tagged layer i want to show alert message and then stop the event from executing.

I tried adding "beforeDelete" event listner to layer but it did not work as it was not called. Tried with "beforeLayerDelete". I am not sure whcih event to be use. Use it on Individual layer or layer object. I have been stuck on this since yesterday.
I need some guidence as to which event to use ?? I am trying to add the event listners inside the afterOpen event.
Hirerarchy is like this.

1) BeforeOpen
2) AfterOpen
  a) Inside adding event listner for doc [AfterOpen]. This event listner when triggered after the doc is opened and calls my tagging function. Inside this tagging function , which is tagging some of the layers, i want to add the deleteEventListner for the layer here as tags needs to be added for certian layer hence only tag layes should be stopped from deletion.

function tagStoryLayers(app,containerID) {
	
	try{
		var doc = app.activeDocument;	
				
		// get all stories for the container
		var stories = GetContainerStories(containerID, doc);
		for(var i=0; i< stories.length; i++){
			var storyName = stories[i];
			for (var j = 0; j < doc.layers.length; j++) {
				var layer = doc.layers[j];
				if (layer.name == storyName) {
				    layer.label = "IsStoryLabel: True";
     layer.addEventListener("beforeDelete",OnBeforeDelete);				
				}
			}
		}
				
	}
	catch(ex){
		alert("Error from tagStoryLayer : " + ex.message);
	}
	
}



 

 

This topic has been closed for replies.

2 replies

Robert at ID-Tasker
Legend
April 3, 2023

Can't you just lock the layer? 

 

Community Expert
April 1, 2023

I don't think if we have any events exposed on the Layer object. Where did you find this beforeDelete event described?

-Manan

-Manan
Ujjwal_u2Author
Participating Frequently
April 1, 2023

Chat GPT  😛
Couldn't find anything anywhere else so turned to GPT.

brian_p_dts
Community Expert
Community Expert
April 1, 2023

You'd have to check/add handlers by pageitem, and if itemLayer.name == "layer to save"