• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Close document after background export to PDF

Community Beginner ,
Jan 06, 2012 Jan 06, 2012

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

TOPICS
Scripting

Views

5.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 06, 2012 Jan 06, 2012

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

...

Votes

Translate

Translate
Community Expert ,
Jan 06, 2012 Jan 06, 2012

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 07, 2012 Jan 07, 2012

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 12, 2012 Jan 12, 2012

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 12, 2012 Jan 12, 2012

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 13, 2012 Jan 13, 2012

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 13, 2012 Jan 13, 2012

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"));

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 25, 2012 Jan 25, 2012

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2012 Jan 25, 2012

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 20, 2013 May 20, 2013

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines