5 Replies Latest reply on Nov 12, 2011 4:46 AM by mrFenton

    Getting the filename of a placed image inside a smart object

    mrFenton

      Hi

       

      I'm wondering if I can get any help with a problem I'm trying to overcome for a friend of mine.

       

      My friend is a photographer who does a lot of schhols work (pupil portraits) and wants to create a little document similar to this (very simplified) version:

      pictureLayout.jpg

      I'm trying to help him to create a batch process so that he can make this take up much less of his time!

       

      Here's where I've got to so far. I've created the layout above which is essentially a smart object (from an external file) manipulated in a couple of ways.

       

      The workflow as I see it at the moment (although I'm wide open to suggestions, and I'm having a bit of Friday brain) is this: I generate an action that replaces the content of the external file with the contents of (each file in his incoming list, sequentially).

       

      That file then comes back into the layout above and a new (flattened) copy is saved out from there.

       

      Rinse and rerpeat through the list of incoming files.

       

      I can just about make that bit happen using actions, but here's what I can't get - the filename of the incoming file (the one that's placed in the smart object). Can I can use a modified version of the AddFileName script to generate a text layer based on the filename of the placed contents in the smart object? Does any of that make sense? I'm reading this back to myself and even I'm not sure! ho ho.

       

      My lack of logic/coherent sentence structure here is what probably holds me back in any eforts I have made in getting into scripting.

       

      I'd appreciate any help that could be offered.

       

      Thanks.

       

      Fenton

      layerStructure.jpg

        • 1. Re: Getting the filename of a placed image inside a smart object
          Michael L Hale Level 5

          I am not sure if you need more help than just the function below but if you make a smart object layer active this function will get the placed filename.

          function getSmartlayerFilename(){
              try {
                  var ref = new ActionReference(); 
                  ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
                  var smartDesc = executeActionGet(ref).getObjectValue(stringIDToTypeID( "smartObject" )) 
                  return smartDesc.getString(charIDToTypeID("FilR"));
              } catch (e) {}   
          }
          
          • 2. Re: Getting the filename of a placed image inside a smart object
            mrFenton Level 1

            Thanks for getting back to me so quickly!

             

            I'm a real newbie with scripting, so please bear with me but I know I have to call that function from somewhere. Could I use it in the existing script I have:

             

            * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *

            // set rulers to pixels and save original for resetting

            var originalUnits = app.preferences.rulerUnits

            app.preferences.rulerUnits = Units.PIXELS

             

            // add the pixel position for the left side of your text block below

            var textLeftPosition= 0

             

            // add the pixel position for the bottom of your text block below

            var textBottomPosition= 0

             

            var typeLayer = app.activeDocument.artLayers.add();

             

            typeLayer.name = app.activeDocument.name;

            typeLayer.kind = LayerKind.TEXT;

            typeLayer.textItem.size = 48;

            typeLayer.textItem.position = [textLeftPosition, textBottomPosition];

            typeLayer.textItem.contents = typeLayername;

             

            // return units to original

            app.preferences.rulerUnits = originalUnits

            * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *

             

            with the typeLayerName var for example. (did warn you I'm a real newbie) if so - how?

             

            Also - the placed file in the smart object is a child of the smart object in use in this layout - will this still work?

             

            Thanks for your help!

            • 3. Re: Getting the filename of a placed image inside a smart object
              Michael L Hale Level 5

              With the smart object layer active you could do something like this.

              if(app.activeDocument.activeLayer.kind == LayerKind.SMARTOBJECT){
                  function getSmartlayerFilename(){
                      try {
                          var ref = new ActionReference(); 
                          ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
                          var smartDesc = executeActionGet(ref).getObjectValue(stringIDToTypeID( "smartObject" )) 
                          return smartDesc.getString(charIDToTypeID("FilR"));
                      } catch (e) {}   
                  }
              
                  // set rulers to pixels and save original for resetting
                  var originalUnits = app.preferences.rulerUnits
                  app.preferences.rulerUnits = Units.PIXELS
              
                  // add the pixel position for the left side of your text block below
                  var textLeftPosition= 10;
                  // add the pixel position for the bottom of your text block below
                  var textBottomPosition= 150;
                  
                  var soFilename = getSmartlayerFilename();
              
                  var typeLayer = app.activeDocument.artLayers.add();
                  typeLayer.name = app.activeDocument.name;
                  typeLayer.kind = LayerKind.TEXT;
                  typeLayer.textItem.size = 48;
                  typeLayer.textItem.position = [textLeftPosition, textBottomPosition];
                  typeLayer.textItem.contents = soFilename;
              
                  // return units to original
                  app.preferences.rulerUnits = originalUnits;
              }
              
              • 4. Re: Getting the filename of a placed image inside a smart object
                mrFenton Level 1

                Hey Michael

                 

                Thank you so much for getting abck to me so quickly, again!

                 

                This si so close that I think I can make it work. I've had a chance to think while I've been at Dinner and I'm going to make a modification to my workflow, then report back.

                 

                Thanks!

                 

                Fenton

                • 5. Re: Getting the filename of a placed image inside a smart object
                  mrFenton Level 1

                  Ah - alas it sems not to be. I think I am going to have to come up with a different workflow and by extension, solution. My current thinking is that I need to have an input folder for the images to be used in the layout, and an output folder for the results to be saved into, along with the layout.psd (as above) all housed in the same folder. Then I need (help) to write a script that does the following:

                   

                  Checks that the layout document is open (that bit is easy enough)

                   

                  if(documents.length==0){

                     alert("You need to have your layout template open (-layout.psd-)")

                  }else{

                      // Do next bit here - and what I'm trying to do is described below.

                      // Hopefully by typing it out I might start to get it straight in my head

                      // Any help with any part of this process is greatluy appreciated

                      // I'm trying to learn!

                     }

                   

                  Then what I think the script needs to do is tocheck the contents of the input folder and find out how many files are in it then set this as a variable, to set the length of a loop, maybe? Or should it load the filenames as an array and work through them sequentially?

                   

                  Once that information is established, there is a smart object on the first layer that needs to be updated with the contents of the first file - my thinking s that this is done by the script opening each image, and then copy/pasting into the smart object, merging down (so I don't end up with an enormous multi-layered file). My reason for thinking this would be a good idea would be that I could just run a quick check to make sure that no landscape pics have sneaked in there and if they have, rotate them (which again, is pretty easy as even I could do that bit, too).

                   

                  doc = activeDocument;

                   

                              if (doc.height < doc.width) {

                   

                                doc.rotateCanvas(90);

                   

                              } else {

                   

                                // Carry on

                   

                              }

                   

                  This will update all of the smart object instances throughout the layout template.

                   

                  Then the text underneath the main image needs to be updated with the filename of the file that has been pasted in the smart object, and after that a [flattened] copy saved out to an output folder.

                   

                  Rinse and repeat for the remaining images in the input folder.

                   

                  Any thoughts?

                   

                  Thanks

                   

                  Fenton