Skip to main content
Legend
January 11, 2023
Answered

afterPlace eventListeners - ungroup snippet error

  • January 11, 2023
  • 4 replies
  • 2323 views

Hi,

I would like to ungroup a snippet after dropping it in the page, unfortunately I have an error

"Error: Unable to remove the target of an active script event."

My code :

 

var myListener = app.eventListeners.add("afterPlace", doThisWhenAfterPlace, false);

function doThisWhenAfterPlace(myEvent) {
    mySel = app.selection;
	try{
		mySel[0].ungroup();
	}catch(e) {
		alert(e)
	}
}

 

If anyone has an idea how to solve this problem... 😉

Regards

This topic has been closed for replies.
Correct answer 琥珀 猫太郎

How about using idleTasks together?

#targetengine "session"
var myTargetIDArray = [];
if (app.eventListeners.itemByName("myAfterplace").isValid) {
    app.eventListeners.itemByName("myAfterplace").remove();
}
app.eventListeners.add("afterPlace", function (myEvent) {
    if (myEvent.target instanceof Group) {
        myTargetIDArray.push(myEvent.target.id);
        app.idleTasks.add({name:"myIdleTask", sleep:10})
        .addEventListener(IdleEvent.ON_IDLE, function () {
            for each (myTargetID in myTargetIDArray) {
                if (app.activeDocument.groups.itemByID(myTargetID).isValid) { 
                    app.activeDocument.groups.itemByID(myTargetID).ungroup();
                }
            }
            myTargetIDArray = [];
            app.idleTasks.itemByName("myIdleTask").remove();
        });
    }
 }).name = "myAfterplace";

 

 

4 replies

Community Expert
January 11, 2023

Hi @琥珀 猫太郎 ,

thank you very much for this suggestion!

Very clever and it works very well!

My tests are done with InDesign 2023 version 18.1 on Windows 10.

 

Also, your:

app.activeDocument.groups.itemByID(myTargetID).isValid

will prevent the script to throw an error if a snippet of a group is placed to an insertion point.

 

Thanks,
Uwe Laubender
( Adobe Community Expert )

Inspiring
January 12, 2023

Laubender
>Also, your:
app.activeDocument.groups.itemByID(myTargetID).isValid
will prevent the script to throw an error if a snippet of a group is placed to an insertion point.

 

I had no intention of doing so. It was based on the assumption that multiple Snippets would be placed, but the result was good.

 

I thought it was necessary to initialize the array that remembers the placed Group, so I modified the Script.

琥珀 猫太郎Correct answer
Inspiring
January 11, 2023

How about using idleTasks together?

#targetengine "session"
var myTargetIDArray = [];
if (app.eventListeners.itemByName("myAfterplace").isValid) {
    app.eventListeners.itemByName("myAfterplace").remove();
}
app.eventListeners.add("afterPlace", function (myEvent) {
    if (myEvent.target instanceof Group) {
        myTargetIDArray.push(myEvent.target.id);
        app.idleTasks.add({name:"myIdleTask", sleep:10})
        .addEventListener(IdleEvent.ON_IDLE, function () {
            for each (myTargetID in myTargetIDArray) {
                if (app.activeDocument.groups.itemByID(myTargetID).isValid) { 
                    app.activeDocument.groups.itemByID(myTargetID).ungroup();
                }
            }
            myTargetIDArray = [];
            app.idleTasks.itemByName("myIdleTask").remove();
        });
    }
 }).name = "myAfterplace";

 

 

m1b
Community Expert
Community Expert
January 11, 2023

Very nice! 🙂

Inspiring
January 12, 2023

Even if we speak different languages, it's wonderful that we can understand each other through Script.

rob day
Community Expert
Community Expert
January 11, 2023

Hi @Ronald63 , try adding #targetengine "session" at the top of the script. Also, drag and dropping from a library is not a place command, you might need afterSelectionChange?

 

#targetengine "session"
var myListener = app.eventListeners.add("afterSelectionChanged", doThisWhenAfterPlace, false);

function doThisWhenAfterPlace(myEvent) {
    mySel = app.selection;
	try{
		mySel[0].ungroup();
	}catch(e) {
		alert(e)
	}
    //myListener.remove()
}

?

Ronald63Author
Legend
January 11, 2023

Hi Rob,

  • I try try adding #targetengine "session" but same error
  • I don't use drag & drop from a library, I use placeGuns.loadPlaceGun() method
  • unfortunately I already use after SelectionChanged for something else

Thanks for your help
Ronald

rob day
Community Expert
Community Expert
January 11, 2023

Are you running the script from the InDesign Scripts panel or your code editor? Also, if you are not removing the listener(s) with code, try restarting ID.

 

This alert works for me from the panel:

 

#targetengine "session"
var myListener = app.eventListeners.add("afterPlace", doThisWhenAfterPlace, false);

function doThisWhenAfterPlace(myEvent) {
    alert("Place " + myEvent.target)
    /* mySel = app.selection;
	try{
		mySel[0].ungroup();
	}catch(e) {
		alert(e)
	} */
    myListener.remove()
}

 

Robert at ID-Tasker
Legend
January 11, 2023

Just guessing - do you get this error AFTER it ungroups?

 

Ronald63Author
Legend
January 11, 2023

Robert,

No, it is the ungroups command that generates this error.

Robert at ID-Tasker
Legend
January 11, 2023

Right... Re-read the error message - looks like you can't "destroy" the object that is currently linked with the active event...