2 Replies Latest reply on Nov 7, 2016 2:03 AM by jakec42735283

    Rename a document

    jakec42735283

      Hi,

       

      I'm trying to check the document name and ask the user to rename it if it doesn't conform to this format xxxxxx Issxx.

       

      I'm having trouble renaming the file. I understand you have to close the document before you can rename it.

       

      Can anyone offer any advice for me code?

       

       

      var doc = app.activeDocument;
      function renameMe () {
          var p = prompt( "Please enter a valid file name" );
          checkInput(p);
          var file = doc.filePath,
          myFileName = doc.name.substr( 0, doc.name.indexOf( "." ) ),
          myNewFileName = myFileName.replace( myFileName, p + ".indd" );
          doc.close();
          //need to close the doc and find the folder to rename
          var newFile = file.rename( myNewFileName );
          app.open( file );
          }

      function checkInput (check) {
           if ( !check.match( "\\d{6}\\sIss\\d+" ) ) {
          alert( "File name should follow convention 000000 Iss0" );
          renameMe();
          }
      }

      if (!doc.name.match( "\\d{6}\\sIss\\d+" ) ) {
          renameMe();
          }

       

      Thanks,

       

      Jake