1 Reply Latest reply: Jan 23, 2012 6:12 PM by groove25 RSS

    Fireworks: How to find document index for scripting?

    groove25 Community Member

      Can anyone share a simple method for obtaining the document index of an active Fireworks document, within the array of open documents? Normally, the current document is accessed by using fw.getDocumentDOM(), but I'd like to obtain the actual array index value (e.g., 0, 1, 2, 3, etc.) to use elsewhere in a script.

       

      I've created a function to obtain this index value, but it's ungainly: It compares entire DOMs that have been converted to source. This requires too much memory or processing and nearly brings the script to a halt. I need something simpler.

       

      var dom = fw.getDocumentDOM();

       

      function documentIndex() {

          if (fw.documents.length == 1) {

              return 0;

          }

          else if (fw.documents.length > 1) {

              var i = 0;

              for (i = 0; i < fw.documents.length; i++) {

                  if (fw.documents[i].toSource() == dom.toSource()) {

                      return i;

                      break;

                  }

              }

          }

      }

       

      I've considered using a document property like docTitleWithoutExtension, filePathForRevert, or filePathForSave as a basis for comparison between documents, but these are unreliable: They won't work if the documents in question have have not yet been saved. 

       

      I figure there must be a simple method, I just don't know what it is.

        • 1. Re: Fireworks: How to find document index for scripting?
          groove25 Community Member

          In case anyone's interested, here's the workaround I'm now using to find the position of an active Fireworks document within the array of open documents (a.k.a., the document index). I'd still love to hear from anyone who can suggest a simpler method.

           

          The basic idea is to first look at two properties of the active document—docTitleWithoutExtension and filePathForRevert—and, where possible, compare those values to that of the same properties within each open document. On their own, each property has loopholes, which is why I'm combining them. (For example, it's possible to have two documents with the same title—if they have different extensions or file paths. Likewise, it's possible to open multiple copies of an unsaved document, all with the same filePathForRevert value.)

           

          New or untitled documents demand an entirely different criteria. I'm not crazy about this approach, but it seems like the best option at this point: When you can't find a DOM property to reliably distinguish one document from another, you temporarily write a distinctive value into a property of the document you want to find. Think of it like tagging a wild animal. In this case, when the active document lacks both a docTitle and file path, I write a crazy piece of "alt text" gibberish into the defaultAltText property and use that to identify my active document. (Even though I'm taking care to restore the original "alt text" afterwards, I don't love this method... but it seems to work.)

           

          var dom = fw.getDocumentDOM();

           

          function documentIndex() {

              if (fw.documents.length == 1) {

                  return 0;

              }

              else if (fw.documents.length > 1) {

                  var docTitle = dom.docTitleWithoutExtension;

                  var filePath = dom.filePathForRevert;

                  if ((docTitle != "") && (filePath != null)) {

                      var i = 0;

                      for (i = 0; i < fw.documents.length; i++) {

                          if ((fw.documents[i].docTitleWithoutExtension == docTitle) && (fw.documents[i].filePathForRevert == filePath)) {

                              return i;

                          }

                      }

                  }

                  else {

                      var originalDefaultAlt = dom.defaultAltText;

                      dom.defaultAltText = "toRtoiSeOfThEsLowAcoRnsRejoiCe";

                      var i = 0;

                      for (i = 0; i < fw.documents.length; i++) {

                          if (fw.documents[i].defaultAltText == dom.defaultAltText) {

                              dom.defaultAltText = originalDefaultAlt;

                              return i;

                          }

                      }

                  }

              }

          }