8 Replies Latest reply on Mar 18, 2014 9:32 AM by Chuck Uebele

    Cropping canvas based on file name suffix (_large, _medium, _small)

    in.ma

      I have this repetitive task of cropping pictures, on a daily basis, each in 3 different versions, a 630x515 *_large, a 274x134 *_medium and a 173x106 *_small.

       

      I'm currently working with 3 different actions (Select Canvas > Copy > Resize > Paste in place) one for each size, since I need to manually free-transform it and choose an appropriate area as the visible one (source images' aspect ratio varies greatly).

       

      Still, I need to check the file name and then use the correct action, which given the amount of times I do it, it is not only slow but also prone to mistakes.

       

      I'd love to have one button I can use that does the action mentioned (even if it just calls the right action) based on the file name suffix.

        • 1. Re: Cropping canvas based on file name suffix (_large, _medium, _small)
          Chuck Uebele Adobe Community Professional & MVP

          Are different files named with the "_large," "_medium," and "_small;" or are these the names you give the files after you run your action?  Using scripting can do whatever you want to do.  You can use the scriptListener to record playing an action, then just change the name of the action in the generated code to switch to whatever action you want.  If you want all three sized from one file, you can do this too, by creating a snapshop of the file before anything is done to it and revert back to it for each action.

          • 2. Re: Cropping canvas based on file name suffix (_large, _medium, _small)
            in.ma Level 1

            Thank you for the quick reply.

             

            Yes the files already have names ending with "_large", "_medium" and "_small". My problem is how do I make the script check what the name of the currently opened image ends at, and then use the correct action based on that.

             

            For example, when I use the script on an image named "7843_i_large" it will call the action "ResizeLarge". When I use the script on an image named "9985_small" it will call the action "ResizeSmall", and so on. The file name before the suffix should be irrelevant to the script.

            • 3. Re: Cropping canvas based on file name suffix (_large, _medium, _small)
              Chuck Uebele Adobe Community Professional & MVP

              Somthing like this might work.  You would need to edit this code and put in your actions names and the action set where indicated.

               

              #target photoshop

              if(app.documents.length>0){

                    var docRef = activeDocument

                    var docName = docRef.name.split('.')[0];

                    var docSize = docName.split('_');

                   

                    switch (docSize){

                          case 'large':

                                runAction('large Action Name');//enter action name

                                break;

                          case 'medium':

                                runAction('medium Action Name');//enter action name

                                break;  

                          case 'small':

                                runAction('Small Action Name');//enter action name

                                break;                 

                          }

                   

                   

                    };//end if

               

              function runAction(actionName){

                    var idPly = charIDToTypeID( "Ply " );

                        var desc2 = new ActionDescriptor();

                        var idnull = charIDToTypeID( "null" );

                            var ref1 = new ActionReference();

                            var idActn = charIDToTypeID( "Actn" );

                            ref1.putName( idActn, actionName );

                            var idASet = charIDToTypeID( "ASet" );

                            ref1.putName( idASet, "Your Action Set Name" );//Change this

                        desc2.putReference( idnull, ref1 );

                    executeAction( idPly, desc2, DialogModes.ALL );

                    app.displayDialogs = DialogModes.NO;

                   

                    };//end funcrion runAction

              1 person found this helpful
              • 4. Re: Cropping canvas based on file name suffix (_large, _medium, _small)
                in.ma Level 1

                Oh, thanks a ton for writing that. I just got home and tested this.

                 

                Unfortunately, in Photoshop nothing happens, while in the  ExtendScript Toolkit I seem to be getting Result: Undefined right after "switch (docSize)". Does that mean I messed up adding my actions' names in there?

                 

                My actions are (quite aptly) named "Large", "Medium" and "Small", under the "Resize" set, so I replaced them as noted in those 4 lines, yet I can't seem to make it work.

                • 5. Re: Cropping canvas based on file name suffix (_large, _medium, _small)
                  in.ma Level 1

                  Um, ok I don't know anything about scripting but doing a bit of googling on the .split command I managed to fix it by adding [1] at the end of var docSize = docName.split('_'). Hope this didn't mess anything else up!

                   

                  So I assume that number tells it to select the 2nd part of the split. Is there any simple way to make it always select the last part of the split? So as to have it work regardless of how many underscores the file name might have.

                   

                  Still, code works great, thank you very much csuebele.

                  • 6. Re: Cropping canvas based on file name suffix (_large, _medium, _small)
                    Chuck Uebele Adobe Community Professional & MVP

                    I made a goof in the code.  Use these two lines to replace lines 4 & 5:

                     

                    var docName = docRef.name.split('.')[0].split('_');

                    var docSize = docName[docName.length-1]

                     


                    • 7. Re: Cropping canvas based on file name suffix (_large, _medium, _small)
                      in.ma Level 1

                      I might not understand how it works, but it's absolutely perfect. Thank you very much!