28 Replies Latest reply on Jul 10, 2013 10:06 AM by StanStill

    Copy text for filename

    StanStill

      I've looked around for this but haven't found much. I have a server with thousands of print proof sheets, and since the beginning, long before me, the files were saved with verbal descriptive names. It would be a lot better if they were saved according to the PO number. All the proof sheets have a po number listed in pretty much the same place on the sheet... is there a way to copy that PO number from the field within the pdf, and save the file to a new folder using that number as the filename using a script?

       

      Thanks in advance.

        • 1. Re: Copy text for filename
          CarlosCanto Adobe Community Professional & MVP

          if there's something unique in the PO text frame that the script can identify, yes.

          • 2. Re: Copy text for filename
            StanStill Level 1

            Unfortunately, none of the text frames are named, but what about if they all begin with the same 2 or 3 digits?

            • 3. Re: Copy text for filename
              pixxxel schubser Level 5

              Show (or give) us an example, please.

               

              Some questions:

              - The documents were created with different versions of Illustrator?

              - With which?

              - Is the text always text (and not paths)?

              • 4. Re: Copy text for filename
                StanStill Level 1

                The documents were made with Illustrator versions going back to CS2. Currently we're using CS6, but most were done with 5 and 4. Text is always text, and it is roughly (but not exactly) in the same place on all documents. Also, the text field is just a PO number, from one of a number of customers we sell to, so if I could pull all the proof sheets with the PO beginning with "345" I would have a folder of all proofs for a single customer, renamed according to PO number, which would be awesome.

                • 5. Re: Copy text for filename
                  CarlosCanto Adobe Community Professional & MVP

                  post screen shot of such text frame selected, and showing in the layers palette too.

                  • 6. Re: Copy text for filename
                    pixxxel schubser Level 5

                    regardless of this you can try:

                     


                    // Find345_NumberInDoc.jsx
                    // http://forums.adobe.com/thread/1221811
                    // regards pixxxelschubser
                    
                    var aDoc = app.activeDocument;
                    var aTFrame = aDoc.textFrames;
                    var searchString = /^345\d/; //how many digits has your PO-number?
                    var count = 0;
                    
                    for (i=aTFrame.length-1; i>=0; i--) {
                        contentString = aTFrame[i].textRange.contents;
                        result = contentString.match (searchString);
                        if (result) {
                        alert (contentString + "_" + aDoc.name);
                        count++;
                        }
                    }
                    if (count == 0) {
                        alert ("Nothing found");
                        } else {
                            if (count > 1) {
                                alert ("found too much");
                                }
                            }
                    
                    • 7. Re: Copy text for filename
                      StanStill Level 1

                      Well I couldn't get the "insert image" function to work, but if you are really interested, I uploaded it here:

                       

                      http://www.filedropper.com/screenshot_1

                       

                      Anyway, the field that begins with 645XXXX is the one that I want to use as the filename of all the "saved as" files. While I was fumbling around with the insert image, it looks like pixxxel shubser found something.

                      • 8. Re: Copy text for filename
                        StanStill Level 1

                        You'll have to forgive me because I'm not a programmer, but I subbed in 645XXXX for 345, and got it to alert the new filename...

                         

                        Two things: can I lop off everything after the 7th digit (no biggie if not, just curious but I forgot that we put which proof this is on that line, and it's not really useful in the filename) and also, how do I get the script to run on a folder full of these and "save as" them all as "contentString" + ".pdf"?

                        • 9. Re: Copy text for filename
                          pixxxel schubser Level 5

                          Step by step!

                           

                          At first you should say, if this "update" works better for you:

                           

                          // Find645_NumberInDoc.jsx
                          // http://forums.adobe.com/thread/1221811
                          // regards pixxxelschubser
                          var aDoc = app.activeDocument;
                          var aTFrame = aDoc.textFrames;
                          //var searchString = /^345\d/; //how many digits has your PO-number?
                          var searchString = /^645\d{4}/; // update
                          var count = 0;
                          
                          for (i=aTFrame.length-1; i>=0; i--) {
                              contentString = aTFrame[i].textRange.contents;
                              result = contentString.match (searchString);
                              if (result) {
                              alert (contentString.slice (0, 7) + "_" + aDoc.name); //update
                              count++;
                              }
                          }
                          if (count == 0) {
                              alert ("Nothing found");
                              } else {
                                  if (count > 1) {
                                      alert ("found too much");
                                      }
                                  }
                          
                          • 10. Re: Copy text for filename
                            StanStill Level 1

                            This update returns exactly what I want the filename to be in the alert... so how do we get to the next step?

                            • 11. Re: Copy text for filename
                              pixxxel schubser Level 5

                              Did you have tested this "update" with files from different Illustrator versions?

                               

                              Sometimes (when open an older file in a newer version) the textlines are broken and Regex doesn't find a result. Please test it exactly with more files before the next step is coming.

                               

                              You always have PDF with PO-number and you will save the new-named PDF in the same folder or in another place? I hope so you won't save directly in network.

                               

                               

                              By the way – you can mark helpful or correct answers as such, you know?

                              1 person found this helpful
                              • 12. Re: Copy text for filename
                                StanStill Level 1

                                On the really old files the text needs to be updated from legacy text, but it seems to work fine on all the files created since 2008 or so, which is a lot!

                                 

                                I guess I would rather they save in a different folder, but it doesn't matter so much to me since I can easily move them to a different folder afterwards. This is all on my machine, not the network.

                                • 13. Re: Copy text for filename
                                  StanStill Level 1

                                  Okay, try though I may... I'm still stuck with trying to get the returned filename that is alerted to become the filename for the new file. The best I have been able to do so far is get Illustrator to open a new blank file. Any ideas?

                                  • 14. Re: Copy text for filename
                                    pixxxel schubser Level 5

                                    It's time for the next step.

                                     

                                     

                                    // Find645_NumberInDoc.jsx
                                    // http://forums.adobe.com/thread/1221811
                                    // regards pixxxelschubser
                                    var aDoc = app.activeDocument;
                                    var DocName = aDoc.name;
                                    var aDocPath = aDoc.path.fsName;
                                    var aTFrame = aDoc.textFrames;
                                    var searchString = /^645\d{4}/;
                                    var searchString2 = /^645\d{4}_/;
                                    
                                    for (i=aTFrame.length-1; i>=0; i--) {
                                        contentString = aTFrame[i].textRange.contents;
                                        result = contentString.match (searchString);
                                        DocName = DocName.replace (searchString2,'');
                                        if (result) {
                                        var SaveName = aDocPath + "/" + result + "_" + DocName;
                                        i=0;  // find now only the first result!
                                        }
                                    }
                                    
                                    var saveOptions = new IllustratorSaveOptions();
                                    saveOptions.compatibility = Compatibility.ILLUSTRATOR13; // or your version
                                    saveOptions.fontSubsetThreshold = 0.0;
                                    app.activeDocument.saveAs( new File(SaveName), saveOptions );
                                    

                                     

                                    Ask, if you have questions.

                                     

                                    • 15. Re: Copy text for filename
                                      StanStill Level 1

                                      Thanks! I didn't want to beg... I'll probably have questions later today.

                                      • 16. Re: Copy text for filename
                                        StanStill Level 1

                                        Wow! That worked so beautifully it took me a few seconds to figure out that it had already worked! As I've started looking though the folder of these files, I've noticed that the PO number that I am trying to capture sometimes starts with other digits, but it is always in the same basic location. Is there a way to make the searchString be the numbers in that location on the artboard? I don't want the script to be confused by other number fields that are also in the document.


                                        Even if I have to do a bunch of searches and change the three digit prefix a few dozen times, this is going to be a lifesaver... that is, provided I can do this magic to a whole folder.

                                         

                                        Thanks!

                                        • 17. Re: Copy text for filename
                                          StanStill Level 1

                                          I've come up with a further complication. I'd like these to save as pdf. I played around with the SaveAsPDF script that comes with Illustrator, and tried to combine the best of both worlds, so that it saves the file as "645xxxx_filename.pdf" but am so far unable to get the SaveName to be the the name of the pdf.

                                           

                                          Thanks for all your help so far! I had no idea what I was getting into, but I'm learning a lot.

                                          • 18. Re: Copy text for filename
                                            pixxxel schubser Level 5
                                            var SaveName = aDocPath + "/" + result + "_" + DocName;
                                            // add the following line and add the PDF-saveOptions
                                            SaveName = SaveName.replace(/\.ai$/,'.pdf');
                                            
                                            • 19. Re: Copy text for filename
                                              StanStill Level 1

                                              I've been away for a while, so of course I had to re-teach myself all the stuff I had only barely learned a couple weeks ago. But it works! I set up a pdf preset with all the settings I need and call it in the pdf save options.

                                               

                                              here's what I have so far:

                                               

                                               

                                              var aDoc = app.activeDocument;

                                              var DocName = aDoc.name;

                                              var aDocPath = aDoc.path.fsName;

                                              var aTFrame = aDoc.textFrames;

                                              var searchString = /^646\d{4}/;

                                              var searchString2 = /^646\d{4}_/;

                                               

                                              for (i=aTFrame.length-1; i>=0; i--) {

                                                  contentString = aTFrame[i].textRange.contents;

                                                  result = contentString.match (searchString);

                                                  DocName = DocName.replace (searchString2,'');

                                                  if (result) {

                                                  var SaveName = aDocPath + "/" + result + "_" + DocName;

                                                      SaveName = SaveName.replace(/\.ai$/,'.pdf');

                                                      i=0;  // find now only the first result!

                                                  }

                                              }

                                               

                                              var saveOptions = new PDFSaveOptions();

                                                  saveOptions.pDFPreset = "ProofCopy";

                                               

                                              app.activeDocument.saveAs( new File(SaveName), saveOptions );

                                              • 20. Re: Copy text for filename
                                                StanStill Level 1

                                                So I tried to cobble together something to get it to perform this function on the current folder, but it only does one and then stops. I still can't get it to repeat the rename function on other files in the folder. It just does the open document then stops.

                                                 

                                                This is what I have:

                                                 

                                                 

                                                // Find645_NumberInDoc.jsx

                                                // http://forums.adobe.com/thread/1221811

                                                // regards pixxxelschubser

                                                 

                                                 

                                                var destFolder, sourceFolder, files, fileType, sourceDoc;

                                                sourceFolder = Folder.current;

                                                if ( sourceFolder != null )

                                                {

                                                    files = new Array();

                                                    fileType = "*.pdf";

                                                    files = sourceFolder.getFiles( fileType );

                                                    if ( files.length > 0 )

                                                    {

                                                        destFolder = sourceFolder;

                                                        for ( f = 0; f < files.length; f++ )

                                                        {

                                                            sourceDoc = app.open(files[f]); // returns the document object

                                                 

                                                 

                                                }

                                                }

                                                }

                                                 

                                                 

                                                var aDoc = app.activeDocument;

                                                var DocName = aDoc.name;

                                                var aDocPath = aDoc.path.fsName;

                                                var aTFrame = aDoc.textFrames;

                                                var searchString = /^6\d{6}/;

                                                var searchString2 = /^6\d{6}_/;

                                                 

                                                for (i=aTFrame.length-1; i>=0; i--) {

                                                    contentString = aTFrame[i].textRange.contents;

                                                    result = contentString.match (searchString);

                                                    DocName = DocName.replace (searchString2,'');

                                                    if (result) {

                                                    var SaveName = aDocPath + "/" + result + "_" + DocName;

                                                        SaveName = SaveName.replace(/\.ai$/,'.pdf');

                                                        i=0;  // find now only the first result!

                                                    }

                                                }

                                                 

                                                var saveOptions = new PDFSaveOptions();

                                                    saveOptions.pDFPreset = "ProofCopy";

                                                 

                                                 

                                                app.activeDocument.saveAs( new File(SaveName), saveOptions );

                                                • 21. Re: Copy text for filename
                                                  pixxxel schubser Level 5

                                                  The replace-part is outside of your file open loop. Only the last file will be changed.

                                                   

                                                  You should bring the part into the loop and add file.close before the next file will be open.

                                                  • 22. Re: Copy text for filename
                                                    StanStill Level 1

                                                    Hmmm... not sure which part and where. I've been trying but missing.

                                                    • 23. Re: Copy text for filename
                                                      pixxxel schubser Level 5

                                                      Perhaps so:

                                                       

                                                       

                                                      // Find645_NumberInDoc.jsx
                                                      // http://forums.adobe.com/thread/1221811
                                                      // regards pixxxelschubser
                                                      
                                                      //var destFolder, sourceFolder, files, fileType, sourceDoc;   // "your" sourceDoc is in the other part always defined as aDoc
                                                      var destFolder, sourceFolder, files, fileType;
                                                      var aDoc, DocName, aDocPath, aTFrame;
                                                      var searchString = /^6\d{6}/;
                                                      var searchString2 = /^6\d{6}_/;
                                                      var saveOptions = new PDFSaveOptions();
                                                      saveOptions.pDFPreset = "ProofCopy";
                                                      
                                                      //sourceFolder = Folder.current;
                                                      sourceFolder = Folder.selectDialog();
                                                      if ( sourceFolder != null )
                                                      {
                                                          files = new Array();
                                                          fileType = "*.ai";
                                                          files = sourceFolder.getFiles( fileType );
                                                          if ( files.length > 0 )
                                                          {
                                                              destFolder = sourceFolder;
                                                              //for ( f = 0; f < files.length; f++ )
                                                              for ( f = 0; f < 4; f++ )   // --------------- only 4 files for your testing
                                                              {
                                                                  aDoc = app.open(files[f]);
                                                                  
                                                                  
                                                                  
                                                                  //alert(aDoc + " opened");
                                                                  // --------------- now was opened a document
                                                                  // --------------- and here should be the rest
                                                                  DocName = aDoc.name;
                                                                  aDocPath = aDoc.path.fsName;
                                                                  aTFrame = aDoc.textFrames;
                                                                  for (i=aTFrame.length-1; i>=0; i--) {
                                                                      contentString = aTFrame[i].textRange.contents;
                                                                      result = contentString.match (searchString);
                                                                      DocName = DocName.replace (searchString2,'');
                                                                      if (result) {
                                                                          var SaveName = aDocPath + "/" + result + "_" + DocName;
                                                                          SaveName = SaveName.replace(/\.ai$/,'.pdf');
                                                                          i=0;
                                                                          }
                                                                      }
                                                                  
                                                                  if (result) {
                                                                      alert("SaveName: "+SaveName);   // --------------- only for your test
                                                                      aDoc.saveAs( new File(SaveName), saveOptions );
                                                                      // --------------- now close the active Document and go to the next in the loop
                                                                      aDoc.close (SaveOptions.DONOTSAVECHANGES);
                                                                      } else {
                                                                          alert ("No match found - document remains open")
                                                                          }
                                                                      
                                                                      
                                                                      
                                                      }
                                                      }
                                                      }
                                                      
                                                      • 24. Re: Copy text for filename
                                                        StanStill Level 1

                                                        I owe you, pixxxel. Do you like beer?

                                                         

                                                        Perfect.

                                                        • 25. Re: Copy text for filename
                                                          pixxxel schubser Level 5

                                                          My pleasure.

                                                          You only should clean up the script a little.

                                                           

                                                          • 26. Re: Copy text for filename
                                                            StanStill Level 1

                                                            Everything is working great. I cleaned out some of the script that wasn't doing anything. Still working great, but there is one thing I have noticed that is odd and I'm wondering if there is a way around it. All the newly saved files lose their "Date Created" information. I was expecting the "Date Modified" info to change, but the "Date Created" is changed too. Is there a way to save that info so that one can see how old the file is without opening it?

                                                             

                                                            Is it because I am using a different pdf setting?

                                                            • 27. Re: Copy text for filename
                                                              pixxxel schubser Level 5

                                                              Hmmh. You open an *ai-file and saveAs *.pdf

                                                              This way always create a new file and therefore you get a new creation date.

                                                               

                                                              But perhaps this helps a little bit:

                                                               

                                                               

                                                              // you need an older file on you desktop
                                                              // replace in the subsequent line the name "test.ai" with the name of your older file
                                                              
                                                              var yourOldFileOnDesktop = "test.ai";
                                                              var aFile = File("~/Desktop/"+yourOldFileOnDesktop);
                                                              
                                                              alert(aFile.created)
                                                              // do something with this information
                                                              
                                                              

                                                               

                                                               

                                                               

                                                              Otherwise you can only copy the entire folder and work with this copy. Then use "save" instead of "saveAs" But you can only save in the same format the file has.

                                                              • 28. Re: Copy text for filename
                                                                StanStill Level 1

                                                                Well, I figured out what's up. As it turns out, because I was scared to run this script on the actual folder of files, I was copying the files into a new folder and running the script on those copies. When I copied the files into the new folder the creation date was changed to when they were copied (even though the modified date stayed the same) and then when the script ran, the modified date changed.

                                                                 

                                                                So now that I'm fairly confident in the script, I guess I should run it on the actual files (which really isn't that risky because there are already extra copies of these files elsewhere)

                                                                 

                                                                Also, and this is just an afterthought, is there a way to change the file Title (in the metadata)?