Sg2.9
Error: The item is no longer...
Recent RSS
Error: The item is no longer valid
From:  Chris Malven
Date:  4. March 2010, 20:11

As my first attempt with Scriptographer, I've created a very simple script to run through all selected items in document and randomly delete them.

It works like a charm when I first use it, but if I "Undo" after using the script and try running it on the same items again, I run into this error:

Error: The item is no longer valid, either due to deletion or undoing. Use isValid() checks to avoid this error.

Can somebody please explain to me what is happening behind the scenes that causes this error? Very much appreciated! The full code of my script is posted below:

if(document.selectedItems.length) {
	
	// Set our starting y point
	var originY = 100;
	
	for (var i=0; i < document.selectedItems.length; i++)
	{
		var thisY = document.selectedItems[i].position.y;
	
		// Set our random number
		var randBase = Math.random();
	
		if (randBase > 0.3)
		{
			document.selectedItems[i].remove();
		}
	}
} else {
	Dialog.alert('Please selected at least 1 path first');
}
Re: Error: The item is no longer valid
From:  Chris Malven
Date:  4. March 2010, 20:21

After trying the script more times, I can't even figure out what exactly the circumstances are that cause this error.

Maybe someone could explain the general circumstances that can cause an item to become invalid?

Re: Error: The item is no longer valid
From:  Jürg Lehni
Date:  5. March 2010, 11:25

Hi Chris,

This has to do with the tracking of undo events. Unfortunately Illustrator was crashing in many situations when trying to work with invalid items, which a script still had a reference to, so the undo history tracking code became necessary.

What you are experiencing is a situation where an existing item wrongly gets marked as invalid by Scriptographer's code. It would be great if you could describe in more detail how to reproduce this error. I just encountered a similar situation as well yesterday and will try to fix this as soon as possible.

While there are situations where this exception makes sense, your script is not one of them, you should not encounter the bug.

I will look into this.

Re: Error: The item is no longer valid
From:  Chris Malven
Date:  19. March 2010, 17:48

Thank you for the help. I've been trying to narrow down how to precisely duplicate the error and I haven't been able to figure it out. As best I can tell, the confusion stems from certain combinations of undoing, selecting different objects, and then re-running the same script on objects it has already been run on.

If I get any closer to narrowing it down I will let you know.

Re: Error: The item is no longer valid
From:  Jürg Lehni
Date:  19. March 2010, 18:00

In fact, I believe I managed to resolve this yesterday and will release a new version very soon. So no need to narrowing down further.

Thank you for your help with this!

Re: Error: The item is no longer valid
From:  Chris Malven
Date:  19. March 2010, 18:11

Well done! Thanks for your hard work on such a great project.

Re: Error: The item is no longer valid
From:  Jürg Lehni
Date:  19. March 2010, 18:20

Getting there, one bug at a time ;)

Re: Error: The item is no longer valid
From:  Jürg Lehni
Date:  20. March 2010, 02:25

This should work fine now in the new version 2.7.040.

Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Hexagonal Grid Generator 16.11.11