Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

"No document windows are open" error

Return to Forums

  • Author
    Posts
    • #86352
      Anonymous
      Inactive

      Hey everyone,

      I have a startup script running that updates some custom text variables with info about the document (page dimensions, parts of the file name).

      The script runs great, but whenever I close a document I get an alert with “No document windows are open” even if there are other documents open.

      I’ve included the script I’m running below (it’s a little hacked together so it’s probably not the best).

      
      #targetengine "session"
      
      main();
      function main(){
      	var myApplicationEventListener = app.addEventListener("afterSelectionChanged",myDisplaySelectionType);
      }
      
      function myDisplaySelectionType(myEvent){
      	if(app.documents.length != 0){
      		if(app.documents.item(0).selection.length != 0){
      			var doc = app.activeDocument; 
      			doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.inches; 
      			doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.inches; 
      			for(var i =0;i<doc.pages.length;i++)
      			{
      			    pagesize = app.activeDocument.textVariables.itemByName("RE_PageSize"); 
      			    pagesize.variableOptions.contents = doc.pages[i].bounds[3] + '" × ' + doc.pages[i].bounds[2] + '"'
      			    fileNameVariable = app.activeDocument.name.split("_")[0];
      			    app.activeDocument.textVariables.item("RE_Docket").variableOptions.contents = fileNameVariable;
      			    clientNameVariable = app.activeDocument.fullName + '';
      			    app.activeDocument.textVariables.item("RE_Client").variableOptions.contents = clientNameVariable.split("/")[3];
      			}
      		}
      	}
      }
      

      Any help would be greatly appreciated!

      Thanks.

    • #86363
      Anonymous
      Inactive

      Hi,

      It’s likely that when your function is executed InDesign is in a Schrodinger state.The document being closed, still exists for a while but yet window is already closed. So your first control is ok but then you get error because you are trying to edit parameters that belong to the window (viewPreferences).

      In my humble opinion, you should prefer idle events which are far more robust and problem free ;)

      Loic

    • #86364
      Anonymous
      Inactive

      Thanks for the response!

      I commented out the viewPreferences lines because my documents are in inches anyway (this was a leftover line from another script I’d borrowed).

      I’m still running into the “no windows” error though.

      Any chance you could give me an example of using idle events to update the text variables?

      Thanks again!

    • #86458
      Anonymous
      Inactive

      Hi,

      Can I suggest another approach ? Having a second look at your code, a few things are problematic. You are for example changing the PAGE SIZE variable content per page. Doing so, you are just flattening values over and over and only the last one remains. That doesn’t make much sense to me.

      So FWIW

      #targetengine “onApplicationEvent”

      main();
      function main(){

      var myApplicationEventListener = app.idleTasks.item(“onApplicationEvent”);

      if ( !myApplicationEventListener.isValid) {
      myApplicationEventListener = app.idleTasks.add({name:”onApplicationEvent”, sleep:10});
      myApplicationEventListener.addEventListener(IdleEvent.ON_IDLE, onApplicationEventHandler, false);
      }

      }

      function onApplicationEventHandler(idleEvent){

      const IN = MeasurementUnits.inches;

      var doc = app.properties.activeDocument;

      if ( !doc ) return;

      doc.viewPreferences.properties = {
      horizontalMeasurementUnits:IN,
      verticalMeasurementUnits:IN,
      }

      var fileNameVariable = doc.name.replace(/\.indd$/i, “”);

      makeVariable (doc, “RE_Docket”, fileNameVariable);

      //dealing with page variable but that can only host one value for all pages
      makeVariable (doc, “RE_PageSize”, “some value that needs to cbe computed”);

      //doc.fullName may throw error is document is not saved (i.e. converted or new)
      //index [3] may cause undefined value of unadequate value so it’s preferable to circumvent this possibility
      var client = doc.fullName.split(“/”);
      client.length>=4 && client = client[3];
      makeVariable (doc, “RE_Client”, doc.properties.fullName? String(client) : “” );

      }

      function makeVariable (doc, name, contents) {
      var tv = doc.textVariables.item ( name );
      !tv.isValid && tv = doc.textVariables.add ( );

      tv.properties = {
      name:name,
      type:VariableTypes.CUSTOM_TEXT_TYPE,
      }

      tv.variableOptions.contents = contents;
      }

Viewing 3 reply threads
  • You must be logged in to reply to this topic.
Forum Ads