Skip to main content
SuperMacGuy
Known Participant
June 11, 2010
Answered

[AS CS5] Exporting PDFs in background

  • June 11, 2010
  • 2 replies
  • 3346 views

So CS5 has background exporting for PDFs. If I have a script that exports multiple PDFs or JPGs etc, like each page individually, or multiple settings sequentially (or now concurrently?), how do I know when it's done? Applescript normally "waits" until the process is done. But now what happens? How do I know that exporting is done and I can close the file, or PDFs are successfully made?

I see there is a "background task" class now, and a "wait for task" command. Any examples on how to work with that? (Sorry I haven't had time to work with it just yet, working on other IDCS5/Snow Leopard things still.)

TIA, Chris

This topic has been closed for replies.
Correct answer brettpolo

If you're script still uses document. exportFile(),  nothing will change. For example, the following code will still work:

t = app.activeDocument.exportFile(ExportFormat.pdfType, File("~/Desktop/Test.pdf"));
alert("Task is done")

If you want to use the 'background tasks'  functionality, you'll need to use document.asynchronousExportFile().  AsynchronousExportFile returns a background task, which has the method waitForTask().  You can use it something like this:

t = app.activeDocument.asynchronousExportFile(ExportFormat.pdfType, File("~/Desktop/Test.pdf"));
t.waitForTask();
alert("Task is done")

This will export your document as a PDF using a background task, wait for the export to complete, and then pop up an alert.  The problem is that you loose the advantages of a background task (namely, you can do other things while the file is exporting in the background).

The backgroundTask object has a property status, which returns the status of the task, including TaskState.COMPLETED.  You could probably add an event listener to check for the state change from TaskState.RUNNING to TaskState.COMPLETED.  When I get a bit more time later, i'll check it out and try to provide some sample code.

Again, if your concern is the behavior of scripts you already have, then nothing should change.  If you're looking to take advantage of the new background tasks, then there's some work to do.

/dan

2 replies

brettpoloCorrect answer
Inspiring
June 11, 2010

If you're script still uses document. exportFile(),  nothing will change. For example, the following code will still work:

t = app.activeDocument.exportFile(ExportFormat.pdfType, File("~/Desktop/Test.pdf"));
alert("Task is done")

If you want to use the 'background tasks'  functionality, you'll need to use document.asynchronousExportFile().  AsynchronousExportFile returns a background task, which has the method waitForTask().  You can use it something like this:

t = app.activeDocument.asynchronousExportFile(ExportFormat.pdfType, File("~/Desktop/Test.pdf"));
t.waitForTask();
alert("Task is done")

This will export your document as a PDF using a background task, wait for the export to complete, and then pop up an alert.  The problem is that you loose the advantages of a background task (namely, you can do other things while the file is exporting in the background).

The backgroundTask object has a property status, which returns the status of the task, including TaskState.COMPLETED.  You could probably add an event listener to check for the state change from TaskState.RUNNING to TaskState.COMPLETED.  When I get a bit more time later, i'll check it out and try to provide some sample code.

Again, if your concern is the behavior of scripts you already have, then nothing should change.  If you're looking to take advantage of the new background tasks, then there's some work to do.

/dan

SuperMacGuy
Known Participant
June 11, 2010

Thanks, this makes sense. A quick test has pretty well confirmed it.

I never make any assumptions about things not changing, when a new application version hits. Maybe for 1 version of ID, things were "the same" and did not require me to overhaul lots of scripts. So at this point I assume everything important I do is broken so I go over it and check everything out. (Guess I'm gun shy from all the changes. Damn you Adobe, Apple!)

Known Participant
October 26, 2010

Anyone know if background exporting to pdf is more memory efficient? Also, do you need to turn it on in a script (AS)?

I've been using scripts to automate pdf exporting for years. But (after fairly recently moving to CS5) I ran into a case where, when exporting what should have been a 99 MB pdf, the finished pdf was smaller than that and was missing a few placed graphics toward the end of the document. On repeated attempts, this was repeated somewhat randomly (sometimes more, sometimes fewer graphics dropped). I was finally able to get it to work by manually exporting.

I noticed that the script export was not in the background, so now I'm thinking, maybe background exporting just works better in CS5?

June 11, 2010

I don't no anything about

the scripting possibilitys nor do i about applescript.

However, if everything else isn't possible:

At least in VBcript it is no problem to check if the job finished. Either by checking the thread or by checking the pdf files.