6 Replies Latest reply on Oct 20, 2017 6:12 AM by subieguy2

    Save .eps as .eps (overwrite saving changes)

    subieguy2 Level 1

      Ok so I have been working on a tool that will automate some file changes for me. Thank you to itsBenMason it has been coming along awesome. So now I have 2 tasks left. Task one....

      I am searching folders for any files that contain _matte.eps

       

      var files = dir.getFiles("*_matte.eps");
      

       

      var folderFiles = folder.getFiles("*_matte.eps");
      

       

      How can I make this look for *_matte.eps as well as *_gloss.eps?

       

      Task two....

      These are already .eps files. How do I just save the file overwriting the .eps that I have made changes to?

      I have tried both of the following formats:

      doc.save(SaveOptions.SAVECHANGES);
      
      doc.save();
      

      Both of those save a .ai file with the same file name as the .eps

       

      Here is the code in it's entirety... Any help would be appreciated!

      // Establish variable to count changes made
      var changesMade = 0;
      
      
      // Display the GUI
      try {
          var gui = createGUI();
          gui.show();
      } catch (e) {
          alert(e);
      }
      
      
      // Go through subfolders as well
      function getSubFolders(dir, collection) {
          if (collection === undefined) collection = [];
          var files = dir.getFiles();
          if (files.length > 0) {
              for (var i = 0; i < files.length; i++) {
                  var file = files[i];
                  if (file.constructor == Folder && file.name !== "") {
                      collection.push(file);
                      getSubFolders(file, collection);
                      alert(file.name);
                  }
              }
          }
          return collection;
      }
      
      
      // Panel fields and information
      function createGUI() {
          var gui = new Window("dialog", "CMYK Correction Tool");
          gui.alignChilren = ["fill", "fill"];
          gui.orientation = "column";
          var panelGroup = createGroup(gui, "row");
      
      
          // Wrong CMYK  
          var wrongPanel = createPanel(panelGroup, "Wrong CMYK Values");
          var wrongCyan = createTextField(wrongPanel, "Cyan Value:", "");
          var wrongMagenta = createTextField(wrongPanel, "Magenta Value:", "");
          var wrongYellow = createTextField(wrongPanel, "Yellow Value:", "");
          var wrongBlack = createTextField(wrongPanel, "Black Value:", "");
      
      
          // Right CMYK  
          var rightPanel = createPanel(panelGroup, "Right CMYK Values");
          var rightCyan = createTextField(rightPanel, "Cyan Value:", "");
          var rightMagenta = createTextField(rightPanel, "Magenta Value:", "");
          var rightYellow = createTextField(rightPanel, "Yellow Value:", "");
          var rightBlack = createTextField(rightPanel, "Black Value:", "");
      
      
          var buttons = createGroup(gui, "row");
          // Confirm button
          var confirmBtn = createButton(buttons, "Confirm", function() {
      
      
              try {
                  // Select folder that contains your files  
                  var dir = Folder.selectDialog("Select your file directory...");
                  if (dir != null) {
                      var files = dir.getFiles("*_matte.eps");
                      //Get all sub folders  
                      var folders = getSubFolders(dir, []);
                      //Loop over folders  
                      for (var i = 0; i < folders.length; i++) {
                          var folder = folders[i];
                          // Only look for files with the .ai extension and collect them  
                          var folderFiles = folder.getFiles("*_matte.eps");
                          for (var j = 0; j < folderFiles.length; j++) {
                              files.push(folderFiles[j]);
                          }
                      }
      
      
                      //Loop over files  
                      if (files.length > 0) {
                          for (var i = 0; i < files.length; i++) {
                              // alert(files.length);
                              var file = files[i];
                              if (file.constructor == File) {
                                  var doc = app.open(file);
      
      
                                  // Collect all of the open document pathItems
                                  var allPaths = doc.pathItems;
      
      
                                  // Loop through all of the open document pathItems
                                  for (var z = 0; z < allPaths.length; z++) {
      
      
                                      // Only look for a specific CMYK fillColor
                                      /*
                                      if (Math.round(allPaths[z].fillColor.cyan) == getText(wrongCyan) &&
                                          Math.round(allPaths[z].fillColor.magenta) == getText(wrongMagenta) &&
                                          Math.round(allPaths[z].fillColor.yellow) == getText(wrongYellow) &&
                                          Math.round(allPaths[z].fillColor.black) == getText(wrongBlack)) {
                                          */
                                      if (Math.round(allPaths[z].fillColor.cyan).toFixed(2) == Math.round(getText(wrongCyan)).toFixed(2) &&
                                      Math.round(allPaths[z].fillColor.magenta).toFixed(2) == Math.round(getText(wrongMagenta)).toFixed(2) &&
                                      Math.round(allPaths[z].fillColor.yellow).toFixed(2) == Math.round(getText(wrongYellow)).toFixed(2) &&
                                      Math.round(allPaths[z].fillColor.black).toFixed(2) == Math.round(getText(wrongBlack)).toFixed(2)) {
      
      
                                          // Change the fillColor properties
                                          allPaths[z].fillColor.cyan = getText(rightCyan);
                                          allPaths[z].fillColor.magenta = getText(rightMagenta);
                                          allPaths[z].fillColor.yellow = getText(rightYellow);
                                          allPaths[z].fillColor.black = getText(rightBlack);
                                          changesMade++;
                                          doc.save();
                                      }
                                  }
                                  
                                  // Close the document
                                  doc.close();
      
      
                              }
      
      
                          }
                      }
                  }
              gui.close();
              } catch (e) {
                  alert(e);
              }
      
      
          });
          // Cancel Button
          var cancelBtn = createButton(buttons, "Cancel", function() {
              gui.close();
          });
      
      
          return gui;
      }
      
      
      // Function to collect user input
      function getText(field) {
          return field.text !== undefined ? field.text : "";
      }
      
      
      // Function to build panel
      function createPanel(parent, title) {
          // Create Panel  
          var panel = parent.add("panel", undefined, title);
          panel.orientation = "column";
          // Return panel  
          return panel;
      }
      
      
      // Function to create button
      function createButton(parent, title, onClick) {
          // Create Button  
          var button = parent.add("button", undefined, title);
          if (onClick !== undefined) button.onClick = onClick;
          // Return button  
          return button;
      }
      
      
      // Function to create field in panel
      function createTextField(parent, title, content) {
          // Create a group for title and field  
          var group = createGroup(parent, "column");
          group.alignChildren = 'left';
          // Create title  
          var title = group.add("statictext", undefined, title);
          var field = group.add("edittext", undefined, content);
          field.preferredSize = [200, 23];
          // Return the field as its all you'll use  
          return field;
      }
      
      
      // Function to organize the panel layout
      function createGroup(parent, orientation) {
          // Create Group  
          var group = parent.add("group");
          group.orientation = orientation;
          // Return Group  
          return group;
      }
      
      
      alert(changesMade + " color changes were made.");
      
        • 1. Re: Save .eps as .eps (overwrite saving changes)
          itsBenMason Level 2

          Try this

          function saveEPS(doc, file) {
            //Create save options
            var options = new EPSSaveOptions();
            //Save
            doc.saveAs(file, options);
          }
          function getFiles(folder) {
            return folder.getFiles(/(_matte|_gloss)\.eps/i);
          }
          getFiles(new Folder("Path/To/Files/"));
          
          • 2. Re: Save .eps as .eps (overwrite saving changes)
            subieguy2 Level 1

            so here is the error I get when I run the following script.... what am I doing wrong?? and it isn't saving over the eps file.

            error.JPG

            // Establish variable to count changes made
            var changesMade = 0;
            
            
            // Display the GUI
            try {
                var gui = createGUI();
                gui.show();
            } catch (e) {
                alert(e);
            }
            
            
            // Go through subfolders as well
            function getSubFolders(dir, collection) {
                if (collection === undefined) collection = [];
                var files = dir.getFiles();
                if (files.length > 0) {
                    for (var i = 0; i < files.length; i++) {
                        var file = files[i];
                        if (file.constructor == Folder && file.name !== "") {
                            collection.push(file);
                            getSubFolders(file, collection);
                            alert(file.name);
                        }
                    }
                }
                return collection;
            }
            
            
            // Panel fields and information
            function createGUI() {
                var gui = new Window("dialog", "CMYK Correction Tool");
                gui.alignChilren = ["fill", "fill"];
                gui.orientation = "column";
                var panelGroup = createGroup(gui, "row");
            
            
                // Wrong CMYK  
                var wrongPanel = createPanel(panelGroup, "Wrong CMYK Values");
                var wrongCyan = createTextField(wrongPanel, "Cyan Value:", "");
                var wrongMagenta = createTextField(wrongPanel, "Magenta Value:", "");
                var wrongYellow = createTextField(wrongPanel, "Yellow Value:", "");
                var wrongBlack = createTextField(wrongPanel, "Black Value:", "");
            
            
                // Right CMYK  
                var rightPanel = createPanel(panelGroup, "Right CMYK Values");
                var rightCyan = createTextField(rightPanel, "Cyan Value:", "");
                var rightMagenta = createTextField(rightPanel, "Magenta Value:", "");
                var rightYellow = createTextField(rightPanel, "Yellow Value:", "");
                var rightBlack = createTextField(rightPanel, "Black Value:", "");
            
            
                var buttons = createGroup(gui, "row");
                // Confirm button
                var confirmBtn = createButton(buttons, "Confirm", function() {
            
            
                    try {
                        // Select folder that contains your files  
                        var dir = Folder.selectDialog("Select your file directory...");
                        if (dir != null) {
                            getFiles();
                            //var files = dir.getFiles("*_matte.eps");
                            //Get all sub folders  
                            var folders = getSubFolders(dir, []);
                            //Loop over folders  
                            for (var i = 0; i < folders.length; i++) {
                                var folder = folders[i];
                                // Only look for files with the .ai extension and collect them  
                                // var folderFiles = folder.getFiles("*_matte.eps");
                                for (var j = 0; j < folderFiles.length; j++) {
                                    files.push(folderFiles[j]);
                                }
                            }
            
            
                            //Loop over files  
                            if (files.length > 0) {
                                for (var i = 0; i < files.length; i++) {
                                    // alert(files.length);
                                    var file = files[i];
                                    if (file.constructor == File) {
                                        var doc = app.open(file);
            
            
                                        // Collect all of the open document pathItems
                                        var allPaths = doc.pathItems;
            
            
                                        // Loop through all of the open document pathItems
                                        for (var z = 0; z < allPaths.length; z++) {
            
            
                                            // Only look for a specific CMYK fillColor
                                            /*
                                            if (Math.round(allPaths[z].fillColor.cyan) == getText(wrongCyan) &&
                                                Math.round(allPaths[z].fillColor.magenta) == getText(wrongMagenta) &&
                                                Math.round(allPaths[z].fillColor.yellow) == getText(wrongYellow) &&
                                                Math.round(allPaths[z].fillColor.black) == getText(wrongBlack)) {
                                                */
                                            if (Math.round(allPaths[z].fillColor.cyan).toFixed(2) == Math.round(getText(wrongCyan)).toFixed(2) &&
                                                Math.round(allPaths[z].fillColor.magenta).toFixed(2) == Math.round(getText(wrongMagenta)).toFixed(2) &&
                                                Math.round(allPaths[z].fillColor.yellow).toFixed(2) == Math.round(getText(wrongYellow)).toFixed(2) &&
                                                Math.round(allPaths[z].fillColor.black).toFixed(2) == Math.round(getText(wrongBlack)).toFixed(2)) {
            
            
                                                // Change the fillColor properties
                                                allPaths[z].fillColor.cyan = getText(rightCyan);
                                                allPaths[z].fillColor.magenta = getText(rightMagenta);
                                                allPaths[z].fillColor.yellow = getText(rightYellow);
                                                allPaths[z].fillColor.black = getText(rightBlack);
                                                changesMade++;
                                                //doc.save();
                                                saveEPS();
                                            }
                                        }
            
            
                                        // Close the document
                                        doc.close();
            
            
                                    }
            
            
                                }
                            }
                        }
                        gui.close();
                    } catch (e) {
                        alert(e);
                    }
            
            
                });
                // Cancel Button
                var cancelBtn = createButton(buttons, "Cancel", function() {
                    gui.close();
                });
            
            
                return gui;
            }
            
            
            // Function to collect user input
            function getText(field) {
                return field.text !== undefined ? field.text : "";
            }
            
            
            // Function to build panel
            function createPanel(parent, title) {
                // Create Panel  
                var panel = parent.add("panel", undefined, title);
                panel.orientation = "column";
                // Return panel  
                return panel;
            }
            
            
            // Function to create button
            function createButton(parent, title, onClick) {
                // Create Button  
                var button = parent.add("button", undefined, title);
                if (onClick !== undefined) button.onClick = onClick;
                // Return button  
                return button;
            }
            
            
            // Function to create field in panel
            function createTextField(parent, title, content) {
                // Create a group for title and field  
                var group = createGroup(parent, "column");
                group.alignChildren = 'left';
                // Create title  
                var title = group.add("statictext", undefined, title);
                var field = group.add("edittext", undefined, content);
                field.preferredSize = [200, 23];
                // Return the field as its all you'll use  
                return field;
            }
            
            
            // Function to organize the panel layout
            function createGroup(parent, orientation) {
                // Create Group  
                var group = parent.add("group");
                group.orientation = orientation;
                // Return Group  
                return group;
            }
            
            
            
            
            function saveEPS(doc, file) {
                //Create save options  
                var options = new EPSSaveOptions();
                //Save  
                doc.saveAs(file, options);
            }
            
            
            function getFiles(folder) {
                return folder.getFiles(/(_matte|_gloss)\.eps/i);
            }
            //getFiles(new Folder("Path/To/Files/"));
            
            
            alert(changesMade + " color changes were made.");
            
            • 3. Re: Save .eps as .eps (overwrite saving changes)
              itsBenMason Level 2

              change

              saveEPS(); 

               

              to saveEPS(doc, file); 

              • 5. Re: Save .eps as .eps (overwrite saving changes)
                itsBenMason Level 2

                in the save EPS function add

                alert(doc);

                alert(file);

                 

                and see what it outputs.

                 

                Not able to open atom or anything at the minute so can only guide you through until i can.

                 

                Just noticed you have this

                 

                getFiles();  
                

                but you are not setting any var with the returned files

                 

                and also the getFiles() function takes a folder object

                function getFiles(folder) {  
                    return folder.getFiles(/(_matte|_gloss)\.eps/i);  
                }  
                
                
                1 person found this helpful
                • 6. Re: Save .eps as .eps (overwrite saving changes)
                  subieguy2 Level 1

                  Ok. No worries. Again I appreciate all your help. I am not a programmer... I kinda sorta get by with basics. But I am drowning here in all your efficient functions lol.

                   

                  So what I am working toward is allowing the user to put in the correct and incorrect values.

                  If the file contains the name _gloss.eps it gets the following CMYK values (which I want the user to be able to type in).

                  CORRECT values for CMYK gloss:

                  CMYK Gloss: C 0 M 100 Y 100 K 35

                   

                  If the file contains the name _matte.eps it gets the following CMYK values (which I want the user to be able to type in).

                  CORRECT values for CMYK matte:

                  CMYK Matte: C 0 M 100 Y 100 K 33

                   

                  No matter if it contains _matte.eps or _gloss.eps the incorrect values are the same (which I want the user to be able to type in).

                  INCORRECT values for CMYK gloss AND CMYK matte:

                  C 22.98 M 100 Y 100 K 17.84

                   

                  All files I need to edit will be .eps. I want the changes to be saved to the edited .eps file (with no new file created).

                   

                  All of this I would like to apply to a selected folder and all subfolder/subfiles.

                   

                  What we have here is really close...but it is getting over my head with all the functions.