1 Reply Latest reply on Jul 3, 2014 8:36 AM by Eugene Tyson

    Best way to determine if document is truly unsaved

    cchimi Level 2

      Is there a preferred method for determining whether a file is truly unsaved? I'm distinguishing between files that have have been saved and then had some changes made to them (but have a valid name and filePath) and files that have just been created during this session and have not been saved anywhere (other than a temp directory). The only thing I can think of is checking whether an error is thrown by the filePath property; is this really the best way?

       

      (I have looked into both the saved and modified properties, but neither provide this information I am looking for. In theory I could check the name property to see if it starts with "Untitled" but there's nothing stopping a user from actually calling a file that so I'd rather not.)

       

      Quick background: I have a script that lets a user browse to some files and get information about them. Files should be closed after processing, unless of course they were open to begin with. So, I do a check of each file against the documents open when the script starts running and set a flag. Just want to make sure that there isn't a better solution than catching the error.

       

      Brief code:

       

          var openDocs = app.documents.everyItem().getElements();
          for (var i = 0; i < funcDocs.length; i++)
          {
              var funcDoc = funcDocs[i];
              var fileInUse = false;
              if (funcDoc instanceof File)
              {
                  if (openDocs.length == 0) {fileInUse = false;}
                  else 
                  {
                      for (var f = 0; f < openDocs.length; f++)
                      {
                          try{
                               openDocs[f].filePath;
                          }catch(e){continue;}
                          if (openDocs[f].name == funcDoc.name && openDocs[f].filePath == funcDoc.path)
                          {
                              fileInUse = true;
                              break;
                          }
                      }
                  }
                  var openDoc = app.open(funcDoc, false, 1147563124);
              }
              else 
              {
                  var openDoc = funcDoc;
                  fileInUse = true;
              }
             //Do something...
              if (fileInUse == false) {openDoc.close();}
          }
      

       

      I guess I'm looking for a bit of a sanity check that I'm not missing some more straightforward method here. Thanks in advance!