Skip navigation
Currently Being Moderated

Close document after background export to PDF

Jan 6, 2012 9:20 AM

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

 
Replies
  • Currently Being Moderated
    Jan 6, 2012 4:53 PM   in reply to BreachofMind

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 7, 2012 1:11 AM   in reply to Ten A

    @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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 13, 2012 12:46 AM   in reply to BreachofMind

    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

     
    |
    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Jan 13, 2012 9:59 PM   in reply to Ten A

    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"));
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jan 25, 2012 7:10 PM   in reply to BreachofMind

    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

     
    |
    Mark as:
  • Currently Being Moderated
    May 20, 2013 7:48 AM   in reply to Ten A

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points