Copy link to clipboard
Copied
Hi,
I'm looking for a way to implement a Debounce function (see here), like this one:
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
To be used in ScriptUI.
Problem is that we don't have setTimeout in ExtendScript land, and $.sleep is blocking / not cancelable.
There is a working implementation for InDesign, using app.idleTasks, which, again, we don't have in Photoshop so back to square one.
Suggestions?
Thank you!
Davide Barranca
Copy link to clipboard
Copied
Depends on the reason it no longer gets called.. If you have a main-loop of sorts that keeps on running, you can have
Copy link to clipboard
Copied
Matias, I'm not sure it could work, since $.sleep is blocking, and there's no way to cancel it – while setTimeout leaves the rest running.
Apparently a proper setTimeout implementation has been added in After Effects 2015, so I've requested that feature for Photoshop myself.
Davide
Copy link to clipboard
Copied
I am also trying to find a clean way to to this... I thought to myself:
I don't need to do any processing in the background. I'm trying to insert periodic pause points in my action/script that give Photoshop a chance to catch up on the display and let the user stop the operation if they are happy with the results.
I would like to do this in ScriptUI.
Something along these lines:
var w = new Window("palette");
// Add some buttons/status/etc.
// setTimeout( w.close(), 3000)
w.show ();
$.sleep(3000)
But sleep is not interruptible.
Copy link to clipboard
Copied
Maybe so. The function is interruptible)
wait(3)
alert()
function wait(sec)
{
$.hiresTimer;
var timer = 0;
try {
while(1)
{
step_by_step();
timer += ($.hiresTimer/1000000);
if (timer > sec) break;
}
}
catch(e) {}
accelerated();
}
function accelerated() { set_performance("accelerated"); }
function step_by_step() { set_performance("stepByStep"); }
function set_performance(mode)
{
try {
var d1 = new ActionDescriptor();
var d2 = new ActionDescriptor();
var r1 = new ActionReference();
r1.putProperty( charIDToTypeID( "Prpr" ), charIDToTypeID( "PbkO" ) );
r1.putEnumerated( charIDToTypeID( "capp" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
d1.putReference( charIDToTypeID( "null" ), r1 );
d2.putEnumerated( stringIDToTypeID( "performance" ), stringIDToTypeID( "performance" ), stringIDToTypeID( mode ) );
d1.putObject( charIDToTypeID( "T " ), charIDToTypeID( "PbkO" ), d2 );
executeAction( charIDToTypeID( "setd" ), d1, DialogModes.NO );
}
catch (e) { throw(e); }
}
Copy link to clipboard
Copied
Awesome, r-bin. Thank you very much! The gist seems to be to use a hard loop which continuously invokes a Photoshop call. In particular, it seems you're messing with the action PlayBack options. I hadn't even thought about changing the Playback behavior. My tool has a lot of steps and it would be really, really slow if I let it do step-by-step or pause between steps, so you've given me TWO great tools.
Thanks!
Copy link to clipboard
Copied
Thank you r-bin , I'll surely give it a try!
I don't recall now what I needed it for, but it's surely great to have.
Best,
Davide
Copy link to clipboard
Copied
Is that your twin brother who created this topic? I answered for one of your post in other theard but then found you use 2 accounts and I'm not sure you are still active on that one? Display thumbnail of PSD on ScriptUI dialog
Copy link to clipboard
Copied
Thanks to r-bin, here is a self-contained "pause" script which pops open a dialog, but allows the user to stop the countdown. Would be slightly better if there was a completely innocuous action to execute. Here it is using "Reset Palette". Also note that closing the window with the cancel does NOT stop the countdown... it just becomes invisible.
/* Waits up to 'sec' seconds. Can continue/interrupt the wait with a Continue button
Counts down the time, too. With much thanks to r-bin
*/
function wait(sec, title, description)
{
$.hiresTimer;
// Shave off a half second because we force it to wait at the end.
var timer = sec - 0.6;
var waiting = true
var w = new Window("palette", title );
var txt = w.add('statictext',undefined, description)
var countdown = w.add('statictext',undefined, "start")
var continueBtn = w.add('button', undefined, 'Continue');
continueBtn.onClick = function () { waiting = false; }
// An innocuous Photoshop Action (Reset the fg/bg colors to default)
var idRset = charIDToTypeID( "Rset" );
var resetFgBg = new ActionDescriptor();
var aref = new ActionReference();
aref.putProperty( charIDToTypeID( "Clr " ), charIDToTypeID( "Clrs" ) );
resetFgBg.putReference( charIDToTypeID( "null" ), aref );
try {
w.show ();
while(waiting && timer > 0) {
executeAction( idRset, resetFgBg, DialogModes.NO );
countdown.text = Math.round(timer * 10, 2) / 10
timer -= ($.hiresTimer/1000000);
}
txt.text = " CONTINUING ";
} catch(e) { // Ignore errors
}
$.sleep(600) // Uninterruptible... be sure "Continuing" is seen.
w.close();
}
// Force photoshop to catch up with the display
app.refresh()
wait(10, "Pausing...", "... for 10 seconds ...")
Copy link to clipboard
Copied
Use
while(w.visible && waiting && timer > 0)
UPD.
There is an unpleasant feature in CC2018 on win7. Did you notice how the digits in the info panel flash, if there is an open document on which the mouse is located?
UPD2.
When you use step_by_step() in a loop, then you can move the timer panel, zoom with the mouse wheel and scroll the document with scrollbars and not use app.refresh() at all. )
Copy link to clipboard
Copied
Loll, third brother? DavideBarranca
Copy link to clipboard
Copied
I have been using the old account for years, but this is the one linked to my CC subscription.
I'm the twin brother of myself 🙂
Copy link to clipboard
Copied
hah I'm sorry for the joke. I came here again as I was in other topic now and saw that like here you answered to someone from 2 different accounts, but that time from one I didn't know yet. I thought maybe you use separate accounts for different Adobe products, but when I noticed you use them even to post in range of same topic / same product I was a little confused ). There is another advantage of your another accounts. Each of them presents higher level of abbilities. That Davide from his first account was probably real newbie yet