4 Replies Latest reply on Jan 19, 2016 10:28 AM by pixxxel schubser

    How to modify the save location within a script

    Petros_ Level 1

      I was sad to hear of the passing of Michael Hale; I'm yet another person who benefitted from his work with ps-scripts.com. In an effort to cobble together a script to replace one I think Michael Hale had helped me with I started searching the net and found this thread. Thanks to c.pfaffenbichler I was able to modify the script on that page to create what is pasted below. It takes a source of multiple pngs, pushes each png through a smart object in a master .psd file, and then exports each of those .pngs as a placed image within the containing image as a separate .png. It's great, but I'd like to modify it again and I'm not sure how to do what I have in mind.


      I'd like to set the place where the final .pngs are saved to to match the folder where the source .pngs came from. Better yet if I could create a folder called "Placed" within that folder and put all the exported images in there that would be cool. But I don't know how. Anyone have any pointers? I've tried to wade through documentation without success. Do I need to make a new variable? And then how do I call that variable? I'm definitely not a programmer, but I might have enough skills to do this?




      // replace smart object’s content and save png;
      // 2011, use it at your own risk;
      #target photoshop
      if (app.documents.length > 0) {
      var myDocument = app.activeDocument;
      var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1];
      var thePath = myDocument.path;
      var theLayer = myDocument.activeLayer;
      // png options
      pngOpts = new PNGSaveOptions();
      // check if layer is smart object;
      if (theLayer.kind != "LayerKind.SMARTOBJECT") {alert ("selected layer is not a smart object")}
      else {
      // select files;
      if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog ("please select files", "*.psd;*.tif;*.png;*.jpg;*.jpeg,", true)}
      else {var theFiles = File.openDialog ("please select files", getFiles, true)};
      if (theFiles) {
      // work through the array;
                for (var m = 0; m < theFiles.length; m++) {
      // replace smart object;
                          theLayer = replaceContents (theFiles[m], theLayer);
                          var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
      //save out as png;
                          myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".png")),pngOpts,true);
      ////// get psds, tifs and jpgs from files //////
      function getFiles (theFile) {
           if (theFile.name.match(/\.(psd|tif|jpg|png)$/i) != null || theFile.constructor.name == "Folder") {
                return true
      ////// replace contents //////
      function replaceContents (newFile, theSO) {
      app.activeDocument.activeLayer = theSO;
      // =======================================================
      var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
          var desc3 = new ActionDescriptor();
          var idnull = charIDToTypeID( "null" );
          desc3.putPath( idnull, new File( newFile ) );
          var idPgNm = charIDToTypeID( "PgNm" );
          desc3.putInteger( idPgNm, 1 );
      executeAction( idplacedLayerReplaceContents, desc3, DialogModes.NO );
      return app.activeDocument.activeLayer