Quantcast
Channel: Adobe Community : Popular Discussions - InDesign Scripting
Viewing all articles
Browse latest Browse all 15932

[JS][CS5.5] Interrupting a script via ScriptUI

$
0
0

I have a script that automates the placement of PDFs onto pages based on an input XML file. It works splendidly, but I would like users to be able to interrupt the placement, particularly if they have accidentally passed the wrong XML file to the script!

 

Since the total time taken for the PDF placements can be a while, I put up a simple palette window containing a progress bar to give an idea of progress. I also added a cancel button, but although the window appears to have focus at least some of the time during the placements, it never becomes possible to press the cancel button, nor to close the window. The mouse pointer is an hourglass the whole time.

 

Below is a simplified test script that demonstrates the problem. I've only run it on CS5.5 on Windows XP, but I believe it should work at least as far back as CS4 on Mac and Windows. It asks for a PDF file and then places it in a new document 16 times. My PC is slow enough that it takes enough time overall to give me plenty of time to hit the cancel button, if it were possible to do so. If your machine is really fast, you might want to increase gridSize so that the script has more to do.

 

Is there anything I can do such that my cancel button can be pressed? I don't need to see the PDF import window, so if that can and needs to be turned off, that's fine. I dabbled with chains of IdleTasks, and was able to press my cancel button, but the chain didn't always end, and I ended up locking things up nastily. I probably need to read up more on IdleTasks. There may be potential there, but I would like to support earlier than CS5 if possible, and IdleTasks were introduced in CS5.

 

#target indesign

(function() {
    var gridSize = 4;    // Ask user for a PDF    var pdfFilter =($.os.toLowerCase().indexOf("macintosh") != -1) ?        (function(file) { return file.name.match(/\.pdf$/); }) : '*.pdf';    var pdfFile = File.openDialog("Choose a small PDF to place...", pdfFilter);    if (!pdfFile) { return; }    // Show a simple progress bar    var active = true;    var win = new Window('palette', 'Placing ' + pdfFile.name);    var label = win.add('statictext', undefined, '');    label.preferredSize.width = 300;    label.justify = 'left';    var progressBar = win.add('progressbar', undefined, 0, gridSize * gridSize);    progressBar.preferredSize = [ 300, 12 ];    var cancelButton = win.add('button', undefined, 'Cancel');    cancelButton.onClick = function() { win.close(); };    win.onClose = function() { active = false; };    win.show();    // Make a grid of rectangles and place the PDF in each rectangle,    // advancing the progress bar as we go    var doc = app.documents.add();    var page = doc.pages[0];    var mp = page.marginPreferences;    var originX = page.bounds[1] + mp.left;    var originY = page.bounds[0] + mp.top;    var rectWidth = (page.bounds[3] - mp.right - originX) / gridSize;    var rectDepth = (page.bounds[2] - mp.bottom - originY) / gridSize;    var placePDF = function(n) {        var y = Math.floor(n / gridSize);        var x = n % gridSize;        label.text = 'Placing PDF in [ ' + x + ', ' + y + ' ]';        var rect = page.rectangles.add();        rect.geometricBounds = [            originY + y * rectDepth,            originX + x * rectWidth,            originY + (y + 1) * rectDepth,            originX + (x + 1) * rectWidth        ];        rect.strokeWeight = 0;        var pdfList = rect.place(pdfFile);        pdfList[0].fit(FitOptions.PROPORTIONALLY);        (progressBar.value)++;        // Even this sleep doesn't let me press the cancel button!        //$.sleep(1000);    }    for (var i = 0; active && i < gridSize*gridSize; i++) {        placePDF(i);    }    win.close();
})();

Viewing all articles
Browse latest Browse all 15932

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>