Copy link to clipboard
Copied
Hi. I'm trying to code up a little animation plugin and I would like to be able to loop through all the layers and select them one by one (mark as an activeLayer) consecutively with a delay. The selection works but the problem is the sleep() function. I use it in a loop in the playAnimation() method (shown below). Basically illustrator only shows the selection when the loop is over, so when I run the script there is a long pause and then we can see the last layer get selected visually. I'm guessing sleep() may not be the right way to do this since it seems to block graphical updates in illustrator. Can someone steer me in the right direction on how to make layers be selected graphically?
// Create button
var animPalette = new Window("palette");
playButton = animPalette .add("button", undefined, "Play");
playButton.onClick = function() {
// Convert function into a string so we can send it as a message
var message = strMethod(playAnimation);
var bt = new BridgeTalk;
bt.target = 'illustrator';
bt.body = message;
bt.send();
}
// Trying to select layer by later with a delay
// This works but it does not update graphically until the loop is over
function playAnimation()
{
var doc = app.activeDocument;
var numLayers = doc.layers.length;
// Check if there is more than one layer
if (doc.layers.length > 1)
{
for(var i=doc.layers.length-1; i >= 0; i--)
{
doc.activeLayer=doc.layers.getByName(doc.layers.name);
$.sleep (500);
}
}
}
// Convert a method into a string
function strMethod (fn) {
return fn.toString() + fn.name + "()";
}
animPalette.show();
// Evaluate methods when they get sent through BridgeTalk
BridgeTalk.onRecieve = function(message)
{
//$.writeln("message recieved");
eval(message.body);
};
Copy link to clipboard
Copied
redraw();
this could make it visual, not tested.
but your still halfway through a program/script that only has 1 thread that is just sitting and ticking.
$.sleep(); is not a very well liked way of dealing with timing.
you are best using setTimeout(function,milliseconds); or setInterval(function,milliseconds);
Copy link to clipboard
Copied
redraw(); seems to work up to a certain point but then AI freezes after a few seconds.
#target illustrator
function WigglePaths(){
var paths = app.activeDocument.pathItems;
var i, j, curPath;
for( i = 0; i < 250; i++ ) {
for( j = 0, jj = paths.length; j < jj; j++ ) {
curPath = paths
; curPath.top += Math.random() * 10-5;
curPath.left += Math.random() * 10-5;
}
$.sleep(50);
redraw();
}
}
WigglePaths();
Copy link to clipboard
Copied
@zertle Oh damn it freezes up on you? I'm not seeing that in my code but I'm just getting started with my plugin.
Copy link to clipboard
Copied
Hey, there's not setTimeouts in extendscript, are there?
Copy link to clipboard
Copied
I haven't seen a setTimeout. That would be awesome if it existed!
Copy link to clipboard
Copied
Thanks Qwertyfly...​! app.redraw() seems to do the job. I don't think we have access to setTimeout in extendscript (I could be wrong). Maybe take that part out of your answer so I could mark it correct?
Copy link to clipboard
Copied
Appy polly loggies,
And I just finished telling someone that ECMA3 does not know Json etc...
I have been playing with HTML Panels, and was using SetTimeout in Javascript.
you could run your script as smaller sections sent to extendscript through bridgetalk from a HTML panel, but that sounds like a lot more work then $.Sleep()...
So redraw() or app.redraw() seems to be the best solution.
I'm not surprised illy crashed, you would be slamming the javascript engine quite hard.
adding try/catch statement may keep it running longer.
also, do you need it to refresh that fast.
$.Sleep(1000); = 1 second
you have 50 in there. so it trying to redraw the screen 20 times per second.
can you get away with half that?