5 Replies Latest reply on May 22, 2015 3:56 AM by c.pfaffenbichler

    Script that resize the open image and save it with a dialog box where I can write the name of the file?

    karlkurtti

      Need help with a script that resize the open image to 1500x1500px and then save it in the same folder as it came from. And when I run it I want to have a dialog box where I can write the name of the file. Is that possible? I am working at a webshop so i need to have a better workflow when i work with the product images.

      Hope you understand my bad writing in english.

      Thanks

        • 1. Re: Script that resize the open image and save it with a dialog box where I can write the name of the file?
          c.pfaffenbichler Level 9

          When the image is not square how is it supposed to be handled?

          Which file format?

          Do you really need the dialog or is there a persistent convention about the renaming?

          • 2. Re: Script that resize the open image and save it with a dialog box where I can write the name of the file?
            karlkurtti Level 1

            All the images are square from the beginning so that is not a problem. Jpeg with quality 70.

            Yes the dialog is important because the names are different on every image.

            • 3. Re: Script that resize the open image and save it with a dialog box where I can write the name of the file?
              c.pfaffenbichler Level 9

              Maybe this helps.

              // save jpg;
              // 2015, use it at your own risk;
              #target "photoshop-70.032"
              if (app.documents.length > 0) {
              //////////////////////////////////////////
              var thedoc = app.activeDocument;
              var docName = thedoc.name;
              try {
              var basename = docName.match(/(.*)\.[^\.]+$/)[1];
              var docPath = thedoc.path;
              }
              catch (e) {
              var basename = thedoc.name;
              var docPath = "~/Desktop"
              };
              //////////////////////////////////////////
              ////// create dialog for customer-entry //////
              var dlg = new Window('dialog', "save copy", [500,300,820,753]);
              dlg.filename = dlg.add('edittext', [12,15,308,35], (basename), {multiline:false});
              dlg.filename.active = true;
              dlg.filename.onChange = function () {
              while (this.text.indexOf(":")!=-1) {this.text = this.text.replace(":","_")}
              };
              //////////////////////////////
              dlg.jpg = dlg.add('panel', [12,230,308,285], "save jpg copy");
              dlg.jpg.jpg5 = dlg.jpg.add('button', [10,10,100,40], "jpg 5");
              dlg.jpg.jpg7 = dlg.jpg.add('button', [105,10,185,40], "jpg 7");
              dlg.jpg.jpg10 = dlg.jpg.add('button', [190,10,280,40], "jpg 10");
              dlg.jpg.jpg5.onClick = function () {
                var closeThis = false;
                if(ScriptUI.environment.keyboardState.shiftKey ) {closeThis = true};
                var aCheck = saveJpgCopy (thedoc, docPath, dlg.filename.text, false, 5, "", false)
                if (aCheck != false && closeThis == false) {dlg.close()};
                };
              dlg.jpg.jpg7.onClick = function () {
                var closeThis = false;
                if(ScriptUI.environment.keyboardState.shiftKey ) {closeThis = true};
                var aCheck = saveJpgCopy (thedoc, docPath, dlg.filename.text, false, 7, "", false)
                if (aCheck != false && closeThis == false) {dlg.close()};
                };
              dlg.jpg.jpg10.onClick = function () {
                var closeThis = false;
                if(ScriptUI.environment.keyboardState.shiftKey ) {closeThis = true};
                var aCheck = saveJpgCopy (thedoc, docPath, dlg.filename.text, false, 10, "", false)
                if (aCheck != false && closeThis == false) {dlg.close()};
                };
              //////////////////////////////
              // cancel-button;
              dlg.cancelBtn = dlg.add('button', [13,415,157,445], 'Cancel', {name:'cancel'});
              dlg.okBtn = dlg.add('button', [163,415,307,445], 'OK', {name:'ok'});
              //////////////////////////////
              dlg.center();
              var myReturn = dlg.show ();
              if (myReturn == 1) {saveJpgCopy (thedoc, docPath, dlg.filename.text, false, 7, "", false)}
              };
              ////////////////////////////////////
              /*save jpg copy*/
              function saveJpgCopy (thedoc, docPath, basename, srgb, theQuality, theSuffix, overwrite) {
              /* check for existing file */
              if (overwrite == false && File(docPath+'/'+basename+theSuffix+'.jpg').exists == true) {
                var theConfirm = confirm("overwrite existing file");
                if (theConfirm == false) {return false}
                };
              /* make copy and save */
              if (app.documents.length > 0) {
              try {
              var jpegOptions = new JPEGSaveOptions();
              jpegOptions.quality = theQuality;
              jpegOptions.embedColorProfile = true;
              /* duplicate */
              if (thedoc.mode == DocumentMode.BITMAP) {
                var theCopy = thedoc.duplicate ("thecopy", true);
              var idCnvM = charIDToTypeID( "CnvM" );
                  var desc2 = new ActionDescriptor();
                  var idT = charIDToTypeID( "T  " );
                      var desc3 = new ActionDescriptor();
                      var idRt = charIDToTypeID( "Rt  " );
                      desc3.putInteger( idRt, 1 );
                  var idGrys = charIDToTypeID( "Grys" );
                  desc2.putObject( idT, idGrys, desc3 );
              executeAction( idCnvM, desc2, DialogModes.NO );
                }
              else {
                if (thedoc.mode == DocumentMode.LAB || thedoc.mode == DocumentMode.DUOTONE || thedoc.mode == DocumentMode.INDEXEDCOLOR || thedoc.mode == DocumentMode.MULTICHANNEL) {
                var theCopy = thedoc.duplicate ("thecopy", true);
                var idCnvM = charIDToTypeID( "CnvM" );
                var desc114 = new ActionDescriptor();
                var idT = charIDToTypeID( "T  " );
                var idRGBM = charIDToTypeID( "RGBM" );
                desc114.putClass( idT, idRGBM );
                executeAction( idCnvM, desc114, DialogModes.ALL );
                }
                else {
                var theCopy = thedoc.duplicate("thecopy", true)
                }
                };
              /* convert */
              if (srgb == true) {
                theCopy.convertProfile ("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, true)
                };
              theCopy.saveAs((new File(docPath+'/'+basename+theSuffix+'.jpg')),jpegOptions,true);
              theCopy.close(SaveOptions.DONOTSAVECHANGES)
              }
              catch (e) {docName+" failed"}
              }
              };
              
              

               

               

              Edit: I have not addressed the resizing, but the document’s method "resizeImage" should not be terribly hard to figure out.

              objectModelViewerResizeImage.jpg

              • 4. Re: Script that resize the open image and save it with a dialog box where I can write the name of the file?
                karlkurtti Level 1

                Thank you so much! This made my day way better.

                Yes I have already figured out the resizing.

                 

                Thanks again

                • 5. Re: Script that resize the open image and save it with a dialog box where I can write the name of the file?
                  c.pfaffenbichler Level 9

                  By the way

                  1. dlg.filename.onChange = function () { 
                  2. while (this.text.indexOf(":")!=-1) {this.text = this.text.replace(":","_")} 
                  3. }; 

                  replaces ":" in entered filenames with "_" because on Mac ":" is forbidden in filenames.

                  Another potentially problematic key could be "/", so depending on who uses the Script you might want to add a few checks to avoid issues.