Sg2.9
More questions about isValid()
Recent RSS
More questions about isValid()
Date:  11. November 2010, 12:08

Is object deletion handled in different ways by AI?

The script I'm writing keeps track of the number of objects in a particular layer. Whenever I delete any object from that layer with the delete key, Scriptographer throws a not valid error message suggesting I should make use of the isValid() command.
However, if I instead throw said object away by manually draging it from the layer palette down to the trashcan, no such error occur and the script reports the correct number of objects left in the layer.

Is "key shortcut deletion" not registered, in some way, as a means of deleting objects in either AI or Sg?

Also, just how is the isValid() command used?

Re: More questions about isValid()
From:  Jürg Lehni
Date:  24. November 2010, 17:02

Hi Håkan,

isValid() is a complicated matter... Basically Scriptographer needs to keep track of the lifetime of an object, because items can be (in)validated by undo/redo/deletion, etc. So a script can have a reference to an item that is long gone from the document, or currently gone until the user hits undo.

You can call isValid() to check an item's validity:

if (item.isValid()) {
    // It's safe to work with this item
}

Scriptographer should track all deletion commands and update its internal tracking structures accordingly. But what you report sounds like in one of the cases, the deletion does not get tracked. Could you try to execute the Edit -> Clear menu command to see how it acts in this case?

Re: More questions about isValid()
From:  Jürg Lehni
Date:  29. November 2010, 16:10

Håkan,

I looked into this a bit, and found out that the deleting of items through the layer palette trashcan gets unnoticed in Scriptographer, so that's why the item thinks it is still valid, although it is not...

I am not really sure what your script does though: Does it keep track of the items in the layer, or just the number? If it's just the number, I do not understand where the iValid() exception would come from?

Could you elaborate more? I would really like to fix this.

Re: More questions about isValid()
Date:  29. November 2010, 20:45

My script use the setInterval timer to check the length of children (sublayers) in a layer. These sublayers in turn contains objects.

The length of the sublayers (number of objects) are not important in the interval check as this is just a method to control if a sublayer exists or not, or a new sublayer has been added.

setsArray = [];
numberOfSets = tileSetsLayer.children.length;

if (numberOfSets > 0){
			// Store all sets in array
			for (i = 0; i < numberOfSets; i++){
						setsArray[i] = (tileSetsLayer.children[i].name);
			}						
} else {
			setsArray = [];
}

The odd thing is that if I use either backspace or, as you suggested, the Edit -> Clear menu command to delete an object I get this message:

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

This does not happen when I drag an object to the trashcan.
This is the code I use to get the names of all the objects in the active sublayer which is NOT called at all in the interval check.

activeSet = tileSetsLayer.children[activeLayer];

objects = activeSet.children.length;

for(i = 0; i < objects; i++){
	...
	activeSet.children[i].name;
	...
};

Does this help?

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
Stroke 3D 16.06.09