7 Replies Latest reply on Oct 17, 2016 2:43 AM by JJMack

    Script to Import Files

    BidoofsCrazyVideos

      I've been getting a little bit into Premiere and After Effects scripting recently and now I'm interested in extending that application to Photoshop, but I'm not quite sure where to start.  I'm trying to make a script that does the following:

       

      Import a file (the directory and filename is hardcoded into the script) into my current active document AS A SMART OBJECT with the center of that imported file at a certain coordinate specified in the script.  Furthermore, the smart object should be resized to have a height of 360 pixels while maintaining the same aspect ratio as the original file (so the width should be determined automatically by the script).

       

      So for example, if I have a 1920x1080 resolution active document, then a png file that is 1080x1440 should be imported into my document. That png file should then be converted to a smart object and then resized to be 270x360 with the number 270 being determined by keeping the same aspect ratio from changing the height to 360.  Finally, the center of the new smart object layer should then be relocated at 260 pixels horizontally and 810 pixels vertically in the document (260,810) after the previous transformations have been applied.

       

      This script should be able to function regardless of the resolution of the imported file and should have no prompts.  Once again, the name of the file (including the extension) should be hardcoded into the script.

       

      I'm not sure how to get started with this.  Can somebody please give me some example scripts that would perform something similar to this or at least point me in the right direction?

        • 1. Re: Script to Import Files
          JJMack Most Valuable Participant

          You work with units set to pixels, place in the png file and get the size in pixels. You calculate what percentage the high need to be resized by to resize to 360 pixels. You than transform both the height and width by that percent.  You align the layer center to your position.

          • 2. Re: Script to Import Files
            SuperMerlin Level 5

            This should give you a good start...

             

            #target photoshop;
            app.bringToFront();
            
            main();
            function main(){
            if(!documents.length) return;
            try{
            var doc = app.activeDocument;
            app.displayDialogs = DialogModes.NO;
            var strtRulerUnits = app.preferences.rulerUnits;
            var strtTypeUnits = app.preferences.typeUnits;
            app.preferences.rulerUnits = Units.PIXELS;
            app.preferences.typeUnits = TypeUnits.PIXELS;
            /////////////////////////////////////////amend to suit
            var fileObj = File("/f/folder1/folder2/filename.jpg");
            if(!fileObj.exists){
                alert(fileObj.name  + " does not exist!");
                return;
                }
            placeFile(fileObj);
            activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER); 
            var SB = activeDocument.activeLayer.bounds;
            var layerHeight = SB[3] - SB[1];
            var pix300 = (100/layerHeight)*360;
            activeDocument.activeLayer.resize(pix300 ,pix300,AnchorPosition.MIDDLECENTER); 
            SB = activeDocument.activeLayer.bounds;
            activeDocument.activeLayer.translate(-SB[0].value,-SB[1].value);
            var halfLayerWidth = (SB[2].value - SB[0].value)/2;
            var halfLayerHeight = (SB[3].value - SB[1].value)/2;
            activeDocument.activeLayer.translate((260 - halfLayerWidth),(810 - halfLayerHeight));
            }catch(e){
            }
            finally{
            app.preferences.rulerUnits = strtRulerUnits;
            app.preferences.typeUnits = strtTypeUnits;
            };
            };
            function placeFile(placeFile) {
                var desc21 = new ActionDescriptor();
                desc21.putPath( charIDToTypeID('null'), new File(placeFile) );
                desc21.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
                    var desc22 = new ActionDescriptor();
                    desc22.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );
                    desc22.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );
                desc21.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc22 );
                executeAction( charIDToTypeID('Plc '), desc21, DialogModes.NO );
            };
            
            
            
            
            2 people found this helpful
            • 3. Re: Script to Import Files
              BidoofsCrazyVideos Level 1

              This script works wonderfully.  Thank you very much.  I didn't realize that it would be so complicated initially, but I understand most of the code you have provided so I know how to change it accordingly.  Thank you!

              • 4. Re: Script to Import Files
                BidoofsCrazyVideos Level 1

                Actually, I still have another question.  I'm not quite sure how the placefile function works.  If I wanted the imported file to be placed into a newly created group (layerSet), how would I have to alter the placefile function?

                • 5. Re: Script to Import Files
                  JJMack Most Valuable Participant

                  Adobe Photoshop Scripting DOM does not cover all of Photoshop Function.  Adobe also has a Plug-in named Scriptlistener.  This plug-in is somewhat like the Action Palettes action recorder.  Any thing the can be recorded in an action will e recorded by the Plug-in in script code ast Action Managers steps.  Basic hard coded action steps. All settings are hard coded like action steps.  The code is not very readable.  However the Hard coded setting can be replace with script variables and you can code Script functions you pass variable to use in the function. That PlaceFile function was created using action manager code. Setting are set step type sets and passed to the Action Manager "executeAction(....);" You nay need to create a layer group using action manager code. If the group exists if you target a layer in that grout then place in the file it will be added into the layer stack above the current target layer.

                  Adobe Photoshop Scripting | Adobe Developer Connection

                  Installing and using the ScriptingListener plug-in

                   

                  I commented his code for you.

                  Capture.jpg

                  • 6. Re: Script to Import Files
                    SuperMerlin Level 5

                    This example shows how to create/name a new layerset then the placed layer is moved into the layerset after the resisize/translate.

                     

                    #target photoshop;
                    app.bringToFront();
                    
                    main();
                    function main(){
                    if(!documents.length) return;
                    try{
                    var doc = app.activeDocument;
                    app.displayDialogs = DialogModes.NO;
                    var strtRulerUnits = app.preferences.rulerUnits;
                    var strtTypeUnits = app.preferences.typeUnits;
                    app.preferences.rulerUnits = Units.PIXELS;
                    app.preferences.typeUnits = TypeUnits.PIXELS;
                    /////////////////////////////////////////amend to suit
                    var fileObj = File("/f/folder1/folder2/filename.jpg");
                    if(!fileObj.exists){
                        alert(fileObj.name  + " does not exist!");
                        return;
                        }
                    //create new layerset
                    var newGroup = activeDocument.layerSets.add();
                    newGroup.name = "Fred";
                    placeFile(fileObj);
                    activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER); 
                    var SB = activeDocument.activeLayer.bounds;
                    var layerHeight = SB[3] - SB[1];
                    var pix300 = (100/layerHeight)*360;
                    activeDocument.activeLayer.resize(pix300 ,pix300,AnchorPosition.MIDDLECENTER); 
                    SB = activeDocument.activeLayer.bounds;
                    activeDocument.activeLayer.translate(-SB[0].value,-SB[1].value);
                    var halfLayerWidth = (SB[2].value - SB[0].value)/2;
                    var halfLayerHeight = (SB[3].value - SB[1].value)/2;
                    activeDocument.activeLayer.translate((260 - halfLayerWidth),(810 - halfLayerHeight));
                    //move layer inside new group
                    activeDocument.activeLayer.move( newGroup, ElementPlacement.INSIDE );
                    }catch(e){
                    }
                    finally{
                    app.preferences.rulerUnits = strtRulerUnits;
                    app.preferences.typeUnits = strtTypeUnits;
                    };
                    };
                    function placeFile(placeFile) {
                        var desc21 = new ActionDescriptor();
                        desc21.putPath( charIDToTypeID('null'), new File(placeFile) );
                        desc21.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
                            var desc22 = new ActionDescriptor();
                            desc22.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );
                            desc22.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );
                        desc21.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc22 );
                        executeAction( charIDToTypeID('Plc '), desc21, DialogModes.NO );
                    };
                    
                    
                    
                    1 person found this helpful
                    • 7. Re: Script to Import Files
                      JJMack Most Valuable Participant

                      It would also be very easy to make this script that was coded for a particular document size and a file that would resize to a height of 360 with some aspect ration know to you so it would position on canvas centered at 260x 810y.  Into a script that would for example resize a watermark png file for any size document relative to the document height and position the resized watermark into the image lower right corner using that DOC object set in the script.

                      Capture.jpg