2 Replies Latest reply on Jan 21, 2015 9:37 PM by Kasper Hviid

    (javascript) My script throws an error at one .psd, but not another

    Kasper Hviid Level 1

      I have this old script that re-arange each pixel in an image, save the resulting image on the harddrive and then apply the same re-arangement to the latest image, repeat.

       

      The problem is, sometimes I need to close my pc, and then I have to restart the process, using one of the newest generated files - and then the script throws an error

       

      Using my script on this .psd works fine:

       

      https://dl.dropboxusercontent.com/u/8240354/widescreen-crop-14748750464_94dcb6effe_o.psd

       

      Using my script on this .psd gives an error:

       

      https://dl.dropboxusercontent.com/u/8240354/re-aranged-pixels.psd

       

      My script:

       

      /*
      FILE:     spacewarp-turbo.jsx
      
      WRITTEN:  23 marts 2007
      
      AUTHOR:   kasperhviid@gmail.com
      
      PURPOSE:  Wild, dude! This script will redraw the picture, one pixel at a time, but it will redraw it horizontally instead of vertically. Sort of if an english text had all its letters redrawn in Japanese writing style. That is: vertically first, and then from right to left.
      It does that over and over again. In theory, the image will reappear after several 1000 times. This script is much faster than the previous version, which redraw the image one pixel at a time.
      
      LICENSE:  GNU LGPL (GNU Lesser General Public License)
                Read the file copying.txt for details. Basicly, this license is
            copyleft, which means that it uses copyright to encurage use,
            instead of restricting it. The LGPL gives you the right to
            distribute, modify and use the scripts, as long as you grants other
            people the same right. This applies to any derivative work. This is a
            good thing, right? :-)
      
      ERROR: For some reason, it cannot use a PNG file from the save directory
      For this reason, you will have to copy the latest file to another directory
      when you want to resume
      
      
      */
      
      #target photoshop 
      
      repeats = 20000000;
      
      // The folder where the files will be placed: example: mappe = "/c/billeder/";
      mappe = "/c/billeder/"; 
      
      // The name of the saved images - may not contain space!
      var imgName = "spacewarp_";
      
      // Should we save the copy of the unmodified file beføre the first run?
      // If you have interrupted the script, and want to restart with the last 
      // generated file, turn this one off, since you already have that image.
      saveAtFirst = false;
      
      // Should the png contain transparence?
      var trans = false;
      
      // Should the number contain zeroes?
      nuller = true;
      
      // Number of ciffers, if the number contain zeroes.
      cifre = 5;
      
      
      
      
      
      // Make backup of the settings before they are changed
      var linialBackup = preferences.rulerUnits;
      preferences.rulerUnits = Units.PIXELS;
      
      
         ///////////////////////
        ///   FUNCTIONS  ///
       /////////////////////
      
      
      
      eyedropper = function(docRef,x,y) { // Function which will determinate the color of a pixel
              x2 = x + 1;
                      y2 = y + 1;
                  out = [0,0,0]; 
              docRef.selection.select([[x,y], [x2,y], [x2,y2], [x, y2]], SelectionType.REPLACE, 0, false); // selects the pixel
              for(ch in list = ["Red", "Green", "Blue"]) { 
                  histogram = docRef.channels[list[ch]].histogram; // Chooses the red, green or blue color
                  for (i = 0; i <= 255; i++) { 
                      out[ch] = i; 
                              out[ch] = out[ch].toString(16); 
                      if (histogram[i]) break; 
                  } // i
              if ( i < 16) out[ch] = "0" + out[ch];
              } // ch
          var out1 = out.toString(16);
              out1 = out1.split(",").join(""); // removes all ","
              return out1; 
      }
      
      
      pngForNet = function(colorReduction, antalFarver, fil) {
      
      var id72 = charIDToTypeID( "Expr" );
          var desc14 = new ActionDescriptor();
          var id73 = charIDToTypeID( "Usng" );
              var desc15 = new ActionDescriptor();
              var id74 = charIDToTypeID( "Op  " );
              var id75 = charIDToTypeID( "SWOp" );
              var id76 = charIDToTypeID( "OpSa" );
              desc15.putEnumerated( id74, id75, id76 );
              var id77 = charIDToTypeID( "Fmt " );
              var id78 = charIDToTypeID( "IRFm" );
              var id79 = charIDToTypeID( "PNG8" );
              desc15.putEnumerated( id77, id78, id79 );
              var id80 = charIDToTypeID( "Intr" );
              desc15.putBoolean( id80, false );
              var id81 = charIDToTypeID( "RedA" );
              var id82 = charIDToTypeID( "IRRd" );
              var id83 = charIDToTypeID( "FlBs" );
              desc15.putEnumerated( id81, id82, id83 );
              var id84 = charIDToTypeID( "FBPl" );
              desc15.putString( id84, colorReduction );
              var id85 = charIDToTypeID( "RChT" );
              desc15.putBoolean( id85, false );
              var id86 = charIDToTypeID( "RChV" );
              desc15.putBoolean( id86, false );
              var id87 = charIDToTypeID( "AuRd" );
              desc15.putBoolean( id87, false );
              var id88 = charIDToTypeID( "NCol" );
              desc15.putInteger( id88, antalFarver );
              var id89 = charIDToTypeID( "Dthr" );
              var id90 = charIDToTypeID( "IRDt" );
              var id91 = charIDToTypeID( "None" );
              desc15.putEnumerated( id89, id90, id91 );
              var id92 = charIDToTypeID( "DthA" );
              desc15.putInteger( id92, 100 );
              var id93 = charIDToTypeID( "DChS" );
              desc15.putInteger( id93, 0 );
              var id94 = charIDToTypeID( "DCUI" );
              desc15.putInteger( id94, 0 );
              var id95 = charIDToTypeID( "DChT" );
              desc15.putBoolean( id95, false );
              var id96 = charIDToTypeID( "DChV" );
              desc15.putBoolean( id96, false );
              var id97 = charIDToTypeID( "WebS" );
              desc15.putInteger( id97, 0 );
              var id98 = charIDToTypeID( "TDth" );
              var id99 = charIDToTypeID( "IRDt" );
              var id100 = charIDToTypeID( "None" );
              desc15.putEnumerated( id98, id99, id100 );
              var id101 = charIDToTypeID( "TDtA" );
              desc15.putInteger( id101, 100 );
              var id102 = charIDToTypeID( "Trns" );
              desc15.putBoolean( id102, false );
              var id103 = charIDToTypeID( "Mtt " );
              desc15.putBoolean( id103, false );
              var id104 = charIDToTypeID( "MttR" );
              desc15.putInteger( id104, 101 );
              var id105 = charIDToTypeID( "MttG" );
              desc15.putInteger( id105, 162 );
              var id106 = charIDToTypeID( "MttB" );
              desc15.putInteger( id106, 143 );
              var id107 = charIDToTypeID( "SHTM" );
              desc15.putBoolean( id107, false );
              var id108 = charIDToTypeID( "SImg" );
              desc15.putBoolean( id108, true );
              var id109 = charIDToTypeID( "SSSO" );
              desc15.putBoolean( id109, false );
              var id110 = charIDToTypeID( "SSLt" );
                  var list3 = new ActionList();
              desc15.putList( id110, list3 );
              var id111 = charIDToTypeID( "DIDr" );
              desc15.putBoolean( id111, false );
              var id112 = charIDToTypeID( "In  " );
              desc15.putPath( id112, new File( fil ) );
          var id113 = stringIDToTypeID( "SaveForWeb" );
          desc14.putObject( id73, id113, desc15 );
      executeAction( id72, desc14, DialogModes.NO );
      }
      
      
      
      
      
         ///////////////////
        ///   SCRIPTS   ///
       //////////////////
      
      
      
      
      
      
      
      
      if (documents.length == 0) { alert("Sorry dude! You need to have an image open before running this script!"); } // If no documents are open, display a warning
      else { // else run the script
      
      
      
      
      
      
      if (saveAtFirst) {
          
          var docRef = activeDocument;
      
      
          bVal = "";
          
          for (num = 1; num < 9000000; num++) {
          
                if (num < 10000000000000) c = cifre - 14;
                if (num < 1000000000000) c = cifre - 13;
                if (num < 100000000000) c = cifre - 12;
                if (num < 10000000000) c = cifre - 11;
                if (num < 1000000000) c = cifre - 10;
                if (num < 100000000) c = cifre - 9;
                if (num < 10000000) c = cifre - 8;
                if (num < 1000000) c = cifre - 7;
                if (num < 100000) c = cifre - 6;
                if (num < 10000) c = cifre - 5; // fire cifre
                if (num < 1000) c = cifre - 4;
                if (num < 100) c = cifre - 3;
                if (num < 10) c = cifre - 2;
              
                // C = Antal nuller
              
                for (xxx = 0; xxx <= c; xxx++) bVal = bVal + "0";
                bVal = bVal + num;
              
                if(!nuller) bVal = num; // Ingen nuller
              
                //alert(mappe + imgName + b + exten);
                var fileRef = new File(mappe + imgName + bVal + ".png"); // Temporary reference to check whether the file exists.
              
                if (!fileRef.exists) break;
              
                bVal = "";
          
          } // for num
          
          pngSettings = new PNGSaveOptions();
          pngSettings.interlaced = false;
          pngFile = new File(mappe + imgName + bVal + ".png" ); // ?? (Filtype, Objekt, Save as copy?) ??
          docRef.saveAs(pngFile, pngSettings, true);
      } // saveAtFirst
      
      
      
      
      
      
      
      
          for (b=0; b<repeats; b++) {
              
      
          
              var docRef = activeDocument;
              
              // SOURCE
              orgDocHori = 0;    // We are going to transfer from this pixel and to the left.
              orgDocVert = 0;    // The vertical pixel line we are working at
              orgDocWidth = docRef.width.value;
              orgDocHeight = docRef.height.value;
              
              var newDocRef = documents.add(orgDocHeight, orgDocWidth, 72.0, "File generated by the script spacewarp-turbo.jsx");
              
              // TARGET
              newDocHori = 0;    // We are going to transfer to this pixel and to the left
              newDocVert = 0;    // The vertical pixel line we are working at
              newDocWidth = newDocRef.width.value;
              newDocHeight = newDocRef.height.value;
              
              
              
              
              
              while (newDocHori < newDocWidth && newDocVert < newDocHeight){
              
                  activeDocument = docRef;
                  
                  
                      if ( orgDocWidth == orgDocHori ) {
                          
                          // Press ENTER
                          orgDocHori = 0;
                          orgDocVert++;
                      }
                  
                  // If there is space enough to copypaste the rest of the line
                  if (orgDocWidth-orgDocHori < newDocWidth-newDocHori ) {
                  
                      x1 = orgDocHori + orgDocWidth-orgDocHori; // Select the last piece of the line
                      y1 = orgDocVert + 1;
                      docRef.selection.select([[orgDocHori,orgDocVert], [x1,orgDocVert], [x1,y1], [orgDocHori, y1]], SelectionType.REPLACE, 0, false);
                      docRef.selection.copy();
                      
                      x1 = newDocHori + orgDocWidth-orgDocHori; // select a piece, according to the piece to be inserted.
                      y1 = newDocVert + 1;
                      activeDocument = newDocRef;
                      newDocRef.selection.select([[newDocHori,newDocVert], [x1,newDocVert], [x1,y1], [newDocHori, y1]], SelectionType.REPLACE, 0, false);
                      
                      // Paste the image into the clipboard
                      newLayerRef = newDocRef.paste();
                      newLayerRef.merge();
                      
                      // Move forward in the line
                      newDocHori = newDocHori + orgDocWidth-orgDocHori;
                      
                      
                      // Press ENTER
                      orgDocHori = 0;
                      orgDocVert++;
                      
                  
                      
                  }
                  else { // There is not enough space to copy it all!
                  
                      x1 = orgDocHori + newDocWidth-newDocHori; // Select af piece of the line, according to the space available at the target
                      y1 = orgDocVert + 1;
                      
                      docRef.selection.select([[orgDocHori,orgDocVert], [x1,orgDocVert], [x1,y1], [orgDocHori, y1]], SelectionType.REPLACE, 0, false);
                      
                      // Copy the image into the clipboard
                      try{
                      docRef.selection.copy(); // "The selected area is empty"
                      }
                      catch(err) {
                          alert("X VALUE = " + x1 + ", Y VALUE = " + y1 + ", orgDocHori = " + orgDocHori + ", orgDocVert = " + orgDocVert + ", newDocWidth = " + newDocWidth + ", newDocHori = " + newDocHori    + ", orgDocWidth = " + orgDocWidth);
                          
                      }
                      
                      x1 = newDocWidth; // select the last of the line
                      y1 = newDocVert + 1;
                      activeDocument = newDocRef;
                      newDocRef.selection.select([[newDocHori,newDocVert], [x1,newDocVert], [x1,y1], [newDocHori, y1]], SelectionType.REPLACE, 0, false);
                      
                      // Paste the image into the clipboard
                      newLayerRef = newDocRef.paste();
                      newLayerRef.merge();
                      
                      // Move forward in the line
                      orgDocHori = orgDocHori + newDocWidth-newDocHori;
                      
                      // Press ENTER
                      newDocHori = 0;
                      newDocVert++;    
                  } // else
              
              } // while
              
              activeDocument = docRef;
              docRef.close(SaveOptions.DONOTSAVECHANGES);
          
              activeDocument = newDocRef;
              docRef = activeDocument;
              if (docRef.width.value < docRef.height.value) docRef.rotateCanvas(270);
              docRef.flatten();
              
          
      
      
      
      
          
              bVal = "";
              
              for (num = 1; num < 9000000; num++) {
              
                    if (num < 10000000000000) c = cifre - 14;
                    if (num < 1000000000000) c = cifre - 13;
                    if (num < 100000000000) c = cifre - 12;
                    if (num < 10000000000) c = cifre - 11;
                    if (num < 1000000000) c = cifre - 10;
                    if (num < 100000000) c = cifre - 9;
                    if (num < 10000000) c = cifre - 8;
                    if (num < 1000000) c = cifre - 7;
                    if (num < 100000) c = cifre - 6;
                    if (num < 10000) c = cifre - 5; // fire cifre
                    if (num < 1000) c = cifre - 4;
                    if (num < 100) c = cifre - 3;
                    if (num < 10) c = cifre - 2;
                  
                    // C = Antal nuller
                  
                    for (xxx = 0; xxx <= c; xxx++) bVal = bVal + "0";
                    bVal = bVal + num;
                  
                    if(!nuller) bVal = num; // Ingen nuller
                  
                    //alert(mappe + imgName + b + exten);
                    var fileRef = new File(mappe + imgName + bVal + ".png"); // Temporary reference to check whether the file exists.
                  
                    if (!fileRef.exists) break;
                  
                    bVal = "";
              
              } // for num
              
              pngSettings = new PNGSaveOptions();
              pngSettings.interlaced = false;
              pngFile = new File(mappe + imgName + bVal + ".png" ); // ?? (Filtype, Objekt, Save as copy?) ??
              docRef.saveAs(pngFile, pngSettings, true);
              
              
      
      
          } // for b
      
      
      
      } // End of else
      
      
      
      
      preferences.rulerUnits = linialBackup; // Recal the old settings