12 Replies Latest reply: Dec 21, 2011 4:58 AM by c.pfaffenbichler RSS

    Adapt this script

    kevinneal01 Community Member

      Does anyone know how to adapt this script so that it would ask the user to enter height and width dimentions at the start instead of hard coding them into the script?

       

      // get a reference to the current (active) document and store it in a variable named "doc"
      doc = app.activeDocument; 

      // change the color mode to RGB.  Important for resizing GIFs with indexed colors, to get better results
      doc.changeMode(ChangeMode.RGB); 

      // these are our values for the end result width and height (in pixels) of our image
      var fWidth = 500;
      var fHeight = 500;

      // do the resizing.  if height > width (portrait-mode) resize based on height.  otherwise, resize based on width
      if (doc.height > doc.width) {
          doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
      }
      else {
          doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
      }

      // call autoContrast and applySharpen on the active layer.
      // if we just opened a gif, jpeg, or png, there's only one layer, so it must be the active one
      doc.activeLayer.autoContrast();
      doc.activeLayer.applySharpen();

      // our web export options
      var options = new ExportOptionsSaveForWeb();
      options.quality = 70;
      options.format = SaveDocumentType.JPEG;
      options.optimized = true;

      var newName = 'web-'+doc.name+'.jpg';

      doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);

        • 1. Re: Adapt this script
          JJMack Community Member

          You have two option add a prompt for the info and logic to validate the response or add a dialog. If you add a dialog you might want to look at Adobe FitImage plugin that ships with Photoshop. FitImage was change from a compiled plugin to a script in CS3.  The advantages of a script plugin is that it comtains code to record the values enter into its dialog when the plugin script is use during action recording. Then when that Action is played no dialog is displayed the values recorded into the action are retrived and used instead of displaying the dialog unless the step display dialog option s turned on.  A Plugin script can be using its dialog and you can pass different hard coded values to it by recoeding the valuse into actions. These action can be used to batch process images.

           

          example prompts

          // these are our values for the end result width and height (in pixels) of our image

          //var fWidth = 500;

          //var fHeight = 500;

          if (!fWidth ) {var fWidth = prompt("Enter number between 0 and 1600 for how wide you want the image", 500); }

          if (!fHeight  ) {var fHeight  = prompt("Enter number between 0 and 1600 for how high you want the image", 500); }

           

          or perhaps

           

          if (!fWidth ) {var fWidth = prompt("Enter number between 0 and 1600 for the image size you want", 500); }

          var fHeight = fWidth;

          • 2. Re: Adapt this script
            Paul Riggott Community Member

            Try this Kevin...

             

             

            #target photoshop
            main();
            function main(){
            if(!documents.length) return;
            var win = new Window( 'dialog', 'Set Size' ); 
            win.alignChildren="row";
            win.g10 = win.add('group');
            win.g10.orientation = "row";
            win.title = win.g10.add('statictext',undefined,'Resize Document');
            win.title.alignment="bottom";
            win.title.graphics.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
            win.p1= win.add("panel", undefined, undefined, {borderStyle:"black"}); 
            win.p1.alignChildren="fill";
            win.g5 =win.p1.add('group');
            win.g5.spacing=10;
            win.g5.st1 = win.g5.add('statictext',undefined,'Width');
            win.g5.et1 = win.g5.add('edittext',undefined,'500');
            win.g5.et1.preferredSize=[50,20];
            win.g5.et1.onChanging = function() { 
              if (this.text.match(/[^\-\.\d]/)) { 
                this.text = this.text.replace(/[^\-\.\d]/g, ''); 
              } 
            };
            win.g5.st2 = win.g5.add('statictext',undefined,'Height');
            win.g5.et2 = win.g5.add('edittext',undefined,'500');
            win.g5.et2.preferredSize=[50,20];
            win.g5.et2.onChanging = function() { 
              if (this.text.match(/[^\-\.\d]/)) { 
                this.text = this.text.replace(/[^\-\.\d]/g, ''); 
              } 
            };
            win.g50 =win.p1.add('group');
            win.g50.spacing=10;
            win.g50.bu1 = win.g50.add('button',undefined,'Resize');
            win.g50.bu1.preferredSize=[100,30];
            win.g50.bu2 = win.g50.add('button',undefined,'Cancel');
            win.g50.bu2.preferredSize=[100,30];
            win.g50.bu1.onClick=function(){
            if(win.g5.et1.text == ''){
                alert("You need to enter a valid width");
                return
                }
            if(win.g5.et2.text == ''){
                alert("You need to enter a valid height");
                return
                }
            win.close(0);
            // get a reference to the current (active) document and store it in a variable named "doc"
            doc = app.activeDocument;  
            // change the color mode to RGB.  Important for resizing GIFs with indexed colors, to get better results
            if (doc.mode != DocumentMode.RGB) doc.changeMode(ChangeMode.RGB);  
            // these are our values for the end result width and height (in pixels) of our image
            var fWidth = Number(win.g5.et1.text);
            var fHeight = Number(win.g5.et2.text);
            // do the resizing.  if height > width (portrait-mode) resize based on height.  otherwise, resize based on width
            if (doc.height > doc.width) {
                doc.resizeImage(undefined,UnitValue(fHeight,"px"),undefined,ResampleMethod.BICUBIC);
            }
            else {
                doc.resizeImage(UnitValue(fWidth,"px"),undefined,undefined,ResampleMethod.BICUBIC);
            }
            // call autoContrast and applySharpen on the active layer.
            // if we just opened a gif, jpeg, or png, there's only one layer, so it must be the active one
            doc.activeLayer.autoContrast();
            doc.activeLayer.applySharpen();
            // our web export options
            var options = new ExportOptionsSaveForWeb();
            options.quality = 70;
            options.format = SaveDocumentType.JPEG;
            options.optimized = true;
            var Name = decodeURI(app.activeDocument.name).replace(/\.[^\.]+$/, '');
            var newName = 'web-'+Name+'.jpg';
            doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);
                }
            win.center();
            win.show();
            }
            
            
            • 3. Re: Adapt this script
              kevinneal01 Community Member

              Paul thats brilliant thanks, just one thing though, If I wanted to use this as a droplet where it only asks for the size once and processes all images is that possible?

              • 4. Re: Adapt this script
                Paul Riggott Community Member

                Sorry but no it wouldn't work as a droplet as each file would still be the first file. The script doesn't know how many files the dropet has.

                For doing batch it might be better using a batch processor such as Image Proccessor or even my Picture Processor.

                http://www.scriptsrus.talktalk.net/PP.htm

                • 5. Re: Adapt this script
                  kevinneal01 Community Member

                  ahh I see thanks for the reply, thats a shame because this script is handy because it allows you to drop a load of portrait or landscape images onto it and it resizes differently depending on orientation, I've not seen another way of doing that.

                   

                  Thanks for your help though

                  • 6. Re: Adapt this script
                    Paul Riggott Community Member

                    For what you have in the script you could do in an action. For the resize you can use File - Automate Fit Image so you could create a few actions that have different sizes and create different droplets.

                     

                    EDIT: you would still require a script to do the filename change and save part though.

                    • 7. Re: Adapt this script
                      Michael L Hale Community Member

                      I'm away from my computer now so I can not test either of these ideas with a droplet.

                       

                      But I think it could be set up so the script checks for a ini file. If the file exists it runs using those settings. If not it displays Paul's dialog for the first image then writes the ini file. It should then be able to process the rest of the files dropped without the dialog.  It could be set up to always run the dialog for the first image or the user could delete the ini file when they want to change the size.

                       

                      If this is for CS5 you could even check ScriptUI.environment.keyboardState.shiftKey and display the dialog regardless of the ini existence when the shift key is held down at the start of the batch.

                       

                      That way you could have an adjustable script that still works in droplet mode.

                      • 8. Re: Adapt this script
                        Paul Riggott Community Member

                        What a brilliant idea Mike!

                         

                        This just might work...

                         

                         

                        #target photoshop
                        main();
                        function main(){
                        if(!documents.length) return;
                        var Prefs ={};
                        Prefs.Width=500;
                        Prefs.Height=500;
                        try{
                          var desc1 = app.getCustomOptions('672df670-f045-11e0-be50-0800200c9a66');
                          Prefs = eval(desc1.getString(0));
                            }catch(e){
                        var desc2 = new ActionDescriptor();
                        desc2.putString(0, Prefs.toSource()); 
                        app.putCustomOptions('672df670-f045-11e0-be50-0800200c9a66', desc2, true );
                                }
                        if(ScriptUI.environment.keyboardState.shiftKey ){
                        var win = new Window( 'dialog', 'Set Size' ); 
                        win.alignChildren="row";
                        win.g10 = win.add('group');
                        win.g10.orientation = "row";
                        win.title = win.g10.add('statictext',undefined,'Resize Document');
                        win.title.alignment="bottom";
                        win.title.graphics.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
                        win.p1= win.add("panel", undefined, undefined, {borderStyle:"black"}); 
                        win.p1.alignChildren="fill";
                        win.g5 =win.p1.add('group');
                        win.g5.spacing=10;
                        win.g5.st1 = win.g5.add('statictext',undefined,'Width');
                        win.g5.et1 = win.g5.add('edittext');
                        win.g5.et1.preferredSize=[50,20];
                        win.g5.et1.text = Prefs.Width.toString();
                        win.g5.et1.onChanging = function() { 
                          if (this.text.match(/[^\-\.\d]/)) { 
                            this.text = this.text.replace(/[^\-\.\d]/g, ''); 
                          } 
                        };
                        win.g5.st2 = win.g5.add('statictext',undefined,'Height');
                        win.g5.et2 = win.g5.add('edittext');
                        win.g5.et2.preferredSize=[50,20];
                        win.g5.et2.text = Prefs.Height.toString();
                        win.g5.et2.onChanging = function() { 
                          if (this.text.match(/[^\-\.\d]/)) { 
                            this.text = this.text.replace(/[^\-\.\d]/g, ''); 
                          } 
                        };
                        win.g50 =win.p1.add('group');
                        win.g50.spacing=10;
                        win.g50.bu1 = win.g50.add('button',undefined,'Resize');
                        win.g50.bu1.preferredSize=[100,30];
                        win.g50.bu2 = win.g50.add('button',undefined,'Cancel');
                        win.g50.bu2.preferredSize=[100,30];
                        win.g50.bu1.onClick=function(){
                        if(win.g5.et1.text == ''){
                            alert("You need to enter a valid width");
                            return
                            }
                        if(win.g5.et2.text == ''){
                            alert("You need to enter a valid height");
                            return
                            }
                        win.close(0);
                        Prefs.Width=win.g5.et1.text;
                        Prefs.Height=win.g5.et2.text;
                        var desc2 = new ActionDescriptor();
                        desc2.putString(0, Prefs.toSource()); 
                        app.putCustomOptions('672df670-f045-11e0-be50-0800200c9a66', desc2, true );
                        Resize(Prefs.Width,Prefs.Height);
                        }
                        win.center();
                        win.show();
                        }else{
                        Resize(Prefs.Width,Prefs.Height);
                        }
                        function Resize(W,H){
                        // get a reference to the current (active) document and store it in a variable named "doc"
                        doc = app.activeDocument;  
                        // change the color mode to RGB.  Important for resizing GIFs with indexed colors, to get better results
                        if (doc.mode != DocumentMode.RGB) doc.changeMode(ChangeMode.RGB);  
                        // these are our values for the end result width and height (in pixels) of our image
                        var fWidth = Number(W);
                        var fHeight = Number(H);
                        // do the resizing.  if height > width (portrait-mode) resize based on height.  otherwise, resize based on width
                        if (doc.height > doc.width) {
                            doc.resizeImage(undefined,UnitValue(fHeight,"px"),undefined,ResampleMethod.BICUBIC);
                        }
                        else {
                            doc.resizeImage(UnitValue(fWidth,"px"),undefined,undefined,ResampleMethod.BICUBIC);
                        }
                        // call autoContrast and applySharpen on the active layer.
                        // if we just opened a gif, jpeg, or png, there's only one layer, so it must be the active one
                        doc.activeLayer.autoContrast();
                        doc.activeLayer.applySharpen();
                        // our web export options
                        var options = new ExportOptionsSaveForWeb();
                        options.quality = 70;
                        options.format = SaveDocumentType.JPEG;
                        options.optimized = true;
                        var Name = decodeURI(app.activeDocument.name).replace(/\.[^\.]+$/, '');
                        var newName = 'web-'+Name+'.jpg';
                        doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);
                        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                            }
                        }
                        
                        
                        • 9. Re: Adapt this script
                          kevinneal01 Community Member

                          AMAZING GUYS!! just done a few tests and it seems to work perfectly

                           

                          I appreciate your help on this, thank you

                          • 10. Re: Adapt this script
                            kevinneal01 Community Member

                            Just one last question, is the customOptions setting written to a file? if so where is it stored on a Mac, I've seached through preferences but can't find it

                            • 11. Re: Adapt this script
                              Paul Riggott Community Member

                              Hi Kevin, the customOptions are in memory and are saved in the Photoshop Preferences when Photoshop closes and they are loaded again when you open Photoshop, they are not saved in a seperate file.

                              • 12. Re: Adapt this script
                                c.pfaffenbichler Community Member

                                Thanks for pointing that out, Michael, and for providing an example, Paul.

                                The shift-key condition might help me save quite some space in a Configurator Panel that I use to save tif- and psd-copies.