Search code examples
error-handlingexecuteadobe-indesignextendscript

InDesign CS5 Script: opening a document is unresponsive, and isn't throwing an error?




I am using the method  myFile.execute()  to open an InDesign document programmatically (in this case, it is a blank document with styles applied to it).


The document is sometimes opened, and at other times it is not... It seems as if the script does not have enough time to open the file before it is done.


This occurred to me because upon testing with  alert(template.name)  it does show the file name I'm looking for, and the script does not throw an error when applying  File(template).execute()  (the variable  template  is itself already a  File  object, but simply  template.execute()  is not working either).


Here is the relevant code:

function openStylesTemplate()
{
    var templateFolder = getScriptFolder(); // with the function below
    var fileArray = templateFolder.getFiles("*.indd"); // an array of all InDesign documents in this script's same folder

    try
    {
        var template = fileArray[0]; // the ONLY InDesign document in the folder
        File(template).execute(); // open the InDesign template document
    }
    catch(e)
    {
        alert("Error: " + e.message + "\n\n" +
                 "Make sure the InDesign styles document is in the same folder as this script,\n" +
                  "and that it is the ONLY InDesign document in this folder, and try again.\n\n" +
                 "This script is in this folder: " + templateFolder + ".");

        exit();
     }
} // end of function openStylesTemplate


So, could the script possibly not have enough time to load the document? If so, should I create a timer before this function is called? Or is there a better way to open an InDesign document programmatically?


Solution

  • Opening an indesign document isn't matter of File.execute() which simply opens a file from the os point of view without any reference to the file itself. You do open files by calling app.open ( file );.