Copy link to clipboard
Copied
Hello all,
I've javascripting InDesign for about a year. My job is almost completely automated now.
However, something is eluding me. When I export a document to a PDF, I have a script that adds the export to a queue (aka a background task). It exports to 3 different places (two back-up folders and the desktop). However, one server is slower than the others and when I use myDocument.close() at the end of the script, InDesign gives me a message that says it can't close the document because it is being used by a background task. Here's the code:
myDocument.asynchronousExportFile (
ExportFormat.pdfType,
File (desktopFolder.fsName+"/"+pdfFile),
false
)
myDocument.close();
Ideally, what I'd like to do is add some sort of event listener to wait for the background tasks to complete before closing the document. But, not affect the ability to move on to other tasks while the export is happening. How do I do that??
-Mike
Hi Mike.
Here is an example of capture export event:
#targetengine session
(function() {
app.addEventListener("afterExport", function(evnt) {
var task, listener;
task = app.idleTasks.add({ name: "exportPDF", sleep: 1000});
listener = task.addEventListener(IdleEvent.ON_IDLE,
function(ev) {
listener.remove();
task.remove();
if (/Adobe\sPDF/.test(evnt.format)){
app.activeDocument.close
...Copy link to clipboard
Copied
Hi Mike.
Here is an example of capture export event:
#targetengine session
(function() {
app.addEventListener("afterExport", function(evnt) {
var task, listener;
task = app.idleTasks.add({ name: "exportPDF", sleep: 1000});
listener = task.addEventListener(IdleEvent.ON_IDLE,
function(ev) {
listener.remove();
task.remove();
if (/Adobe\sPDF/.test(evnt.format)){
app.activeDocument.close (SaveOptions.YES);
}
});
}).name = "exportPDF";
alert("custom exporter installed.");
}());
If you want stop listener, run below:
#targetengine "session"
myListener = app.eventListeners.itemByName("exportPDF");
if (myListener.isValid) {
myListener.remove();
alert ("custom exporter removed." )
}
...or you can disable background export. You can read as reference below:
http://forums.adobe.com/message/3689090#3689090
Ten.
Copy link to clipboard
Copied
@Ten: the "old" exportFile method is still available.
So, if you want to export to PDF without a background task you could do it like this:
myDocument.exportFile (
ExportFormat.pdfType,
File (desktopFolder.fsName+"/"+pdfFile),
false
)
myDocument.close();
But I think Mike did deliberately choose the "asynchronously" variety…
Uwe
Copy link to clipboard
Copied
Heres my code:
#targetengine session
function Listen () {
app.addEventListener("afterExport", function(evt) {
var task = app.idleTasks.add ({name:"exportPDF", sleep: 1000});
var listener = task.addEventListener (IdleEvent.ON_IDLE, function(e) {
listener.remove();
task.remove();
if (/Adobe\sPDF/.test(evt.format)) {myDoc.close(SaveOptions.YES)};
});
}).name = "exportPDF";
alert ("Custom exporter installed.");
}
Listen();
myDoc.asynchronousExportFile(
ExportFormat.pdfType,
File (dirFolder.fsName + "/" + pdfFile),
false
)
I was concerned that the "afterExport" class is not in the object model for InDesign CS5. I think that may be why the document is not closing after the export completes. Other than that, there are no errors...
Copy link to clipboard
Copied
Actually I did get it to work! Nice. However, a warning appears after the document was closed saying "An attached script says an object was invalid". Why is that?
Copy link to clipboard
Copied
Hi Mike.
Your code works fine in my environment. But I think better to kill Listener each time automatically.
#targetengine session
function Listen () {
app.addEventListener("afterExport", function(evt) {
var task = app.idleTasks.add ({name:"exportPDF", sleep: 1000});
var listener = task.addEventListener (IdleEvent.ON_IDLE, function(e) {
listener.remove();
task.remove();
if (/Adobe\sPDF/.test(evt.format)) {
myDoc.close(SaveOptions.YES);
app.eventListeners.itemByName("exportPDF").remove(); //kill listener.
//alert ("custom exporter removed.")
};
});
}).name = "exportPDF";
//alert ("Custom exporter installed.");
}
//my test parameters.
var myDoc = app.activeDocument;
pdfFile = "/test10.pdf";
Listen();
myDoc.asynchronousExportFile(
ExportFormat.pdfType,
File (pdfFile),
false
)
Event classes dropped from Object Model Viewer. But we can correct props ourself like below :
var str ="";
for (var a in Event) {
str += a + "\n";
}
alert(str);
But I think best way of reading Object Model, download and read Jongware's CHM file. Check below link.
http://forums.adobe.com/thread/668578
@Uwe : Thank you for your information.
Ten
Copy link to clipboard
Copied
Event classes dropped from Object Model Viewer. But we can correct props ourself like below :
var str ="";
for (var a in Event) {
str += a + "\n";
}
alert(str);
Eww!
Just use reflection:
alert(Event.reflect.properties.join("\n"));
Copy link to clipboard
Copied
This is really helpful.. I've been using it for awhile now. But, I'd like to make it better.
How would I go about creating a queue? Right now, when I try to export multiple documents at once, the script will only close the last document exported, and not the others. So, if I run the script each time on 3 documents, only the last one will close.
Ideally, I'd like to add these documents to a queue that will close them each as they finish. How would you go about doing that?
Cheers,
Mike
Copy link to clipboard
Copied
Hi Mike,
We can do that, simply counting finished time. But regular variables lost value when functions finished.
So I try to use closure and it works, see below:
#targetengine session
function Listen (num) {
var flg = (function (){ //closure
var _f = 1;
return function(){return _f++;}
})();
app.addEventListener("afterExport", function(evt) {
var task = app.idleTasks.add ({name:"exportPDF", sleep: 1000});
var listener = task.addEventListener (IdleEvent.ON_IDLE, function(e) {
listener.remove();
task.remove();
if (/Adobe\sPDF/.test(evt.format)&&flg()>num) {
myDoc.close(SaveOptions.YES);
app.eventListeners.itemByName("exportPDF").remove();
}
});
}).name = "exportPDF";
}
//test
var myDoc = app.activeDocument;
var svLen = 3; //document length
Listen(svLen-1);
for (i=0;i<svLen;i++)
myDoc.asynchronousExportFile(ExportFormat.pdfType,File ("/test" + i + ".pdf"),false);
Ten
Copy link to clipboard
Copied
Hi Ten,
I can't get this to work....
Script is able to close only one document after exporting to pdf.
Also - when I open 3 different documents, script generates 3 the same pdfs from one of opened docs.