1 Reply Latest reply on Mar 2, 2017 6:00 AM by mauricior6328708

    Script does not execute through an action

    Ps-Design Level 1

      I found this great script on this topic: Defining the stroke size by slider

      I tried to create an action to add "borders" and a 3D type shadow, the size of the border would be configured through that of the Script, the problem is that the dialog does not open, making it impossible to configure the border size. It only works as the last step of the Action, and I do not want to end the action like it, I still need at least 3 steps to finish. I'm going to make the action and script available so maybe someone can explain this problem to me:

      01.jpg

      Action:File sharing and storage made simple

      Script:

      #target photoshop;
      app.bringToFront();
      if(documents.length) main();
      function main(){
          function WinObject() { 
              try{
      var strokeSize = getStrokeSize();
      if(strokeSize == null) return;          
            /*Resource String for 'palette' Window */
            var windowResource = "palette {orientation: 'column',alignChildren: ['fill', 'top'],preferredSize:[200, 130],"+
            "text: 'Set Stroke Effect Size',margins:15,sliderPanel: Panel {orientation: 'row',alignChildren: 'right',"+
            "margins:15,text: ' Add Frame Size ',st: StaticText { text: 'Value:' },sl: Slider{"+
            "minvalue: 1, maxvalue: 255, value: 0, size:[100,20] }, te: EditText { text: '30', characters: 5, justify: 'left'}}"+
            "bottomGroup: Group{cancelButton: Button {"+
            "text: 'Cancel', properties:{name:'cancel'}, size: [70,24], alignment:['right', 'center'] },"+
            "applyButton: Button { text: 'Apply', properties:{name:'ok'}, size: [70,24], alignment:['right', 'center'] },}}"; 
            var win = new Window(windowResource);
            win.sliderPanel.te.text=strokeSize;
            win.sliderPanel.sl.value=strokeSize;
            win.sliderPanel.te.onChanging = function(){
                win.sliderPanel.sl.value = Number(win.sliderPanel.te.text);
                setStrokeSize(win.sliderPanel.sl.value.toFixed(0));
                }
           win.sliderPanel.sl.onChanging = function(){
                win.sliderPanel.te.text=win.sliderPanel.sl.value.toFixed(0);
                setStrokeSize(Math.round(this.value));
                }
            win.bottomGroup.cancelButton.onClick = function() {
              win.close(2);
              setStrokeSize(strokeSize);
                }; 
            win.bottomGroup.applyButton.onClick = function() {
                win.close(0);
                setStrokeSize(win.sliderPanel.sl.value.toFixed(0));
                };    
            // Show the Window 
            win.show(); 
           
      // helper function for working with descriptors by Mike Hale
      function getActiveLayerProperty( psKey, psType ) {
          var ref = new ActionReference();
          ref.putProperty( charIDToTypeID( 'Prpr' ), psKey );
          ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
          if( undefined == psType ){
            return executeActionGet( ref ).getObjectValue( psKey );
          }else{
              return executeActionGet( ref );
          }
      };
      function duplicateDescriptor( descriptor ) {
          var newDescriptor = new ActionDescriptor;
          newDescriptor.fromStream( descriptor.toStream() );
          return newDescriptor;
      };
      function localizeDescriptor( desc ) {
          var stream, pointer, zStringLength, zstring, localized_string, newZStringLength, previousStream, followingStream, newDesc;
          stream = desc.toStream();
          while( true ) {
              pointer = stream.search(/TEXT....\x00\$\x00\$\x00\$/);
              if( pointer === -1 ) {
                  break;
              }
              zStringLength = getLongFromStream( stream, pointer + 4 );
              zstring = readUnicode( stream.substr( pointer + 8, ( zStringLength - 1 ) * 2) );
              localized_string = ( localize( zstring ) ) + '\u0000';
              newZStringLength = localized_string.length;
              previousStream = stream.slice( 0, pointer);
              followingStream = stream.slice( pointer + 8 + zStringLength * 2);
              stream = previousStream.concat( 'TEXT', longToString( newZStringLength ), bytesToUnicode( localized_string ), followingStream );
          }
          newDesc = new ActionDescriptor();
          newDesc.fromStream( stream );
          return newDesc;
      };
      function getShortFromStream( stream, pointer ) {
          var hi, low;
          hi = stream.charCodeAt( pointer ) << 8 ;
          low = stream.charCodeAt( pointer + 1 );
          return hi + low;
      };
      function getLongFromStream( stream, pointer ) {
          var hi, low;
          hi = getShortFromStream( stream, pointer) << 16;
          low = getShortFromStream( stream, pointer + 2);
          return hi + low;
      };
      function readUnicode( unicode ) {
          var string = "";
          for( i = pointer = 0; pointer < unicode.length; i = pointer += 2) {
              string +=String.fromCharCode( getShortFromStream( unicode, pointer ) );
          }
          return string;
      };
      function longToString( longInteger ) {
          var string;
          string = String.fromCharCode( longInteger >>> 24 );
          string += String.fromCharCode( longInteger << 8 >>> 24 );
          string += String.fromCharCode( longInteger << 16 >>> 24 );
          string += String.fromCharCode( longInteger << 24 >>> 24 );
          return string;
      };
      function bytesToUnicode( bytes ) {
          var unicode = "", char_code, charIndex;
          for( charIndex  = 0; charIndex < bytes.length; charIndex ++ ) {
              char_code = bytes.charCodeAt( charIndex );
              unicode += String.fromCharCode(char_code >> 8 ) +  String.fromCharCode( char_code & 0xff );
          }
          return unicode;
      };
      function setStrokeSize(Size ) {
          var layerEffects, newLayerEffects, currentDesc, newDesc, colorDesc, newLayerEffects, layerDesc, targetDesc, setDesc;
          layerEffects = getActiveLayerProperty( charIDToTypeID( 'Lefx' ) );
          newLayerEffects = duplicateDescriptor( layerEffects );
          currentDesc = layerEffects.getObjectValue( stringIDToTypeID( 'frameFX') );
          newDesc = duplicateDescriptor( currentDesc );  
          newDesc.putUnitDouble( charIDToTypeID('Sz  '), charIDToTypeID('#Pxl'), Size );
          newLayerEffects.putObject(  stringIDToTypeID( 'frameFX'),  stringIDToTypeID( 'frameFX'), newDesc );  
          newLayerEffects = localizeDescriptor( newLayerEffects );
          layerDesc = new ActionDescriptor();
          layerDesc.putObject( charIDToTypeID('Lefx'), charIDToTypeID('lfxv'), newLayerEffects);
          targetDesc = new ActionReference();
          targetDesc.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
          setDesc = new ActionDescriptor;
          setDesc.putObject( charIDToTypeID('T   '), charIDToTypeID('Lyr '), layerDesc );
          setDesc.putReference( charIDToTypeID('null' ), targetDesc );
          executeAction(charIDToTypeID('setd'), setDesc, DialogModes.NO);
      };
      function getStrokeSize(){
      var ref = new ActionReference();
      ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
      var desc = executeActionGet(ref);
      if(desc.hasKey(stringIDToTypeID( 'layerEffects' ))){
      if(!desc.getBoolean (stringIDToTypeID( 'layerFXVisible'))) return undefined;
      desc = desc.getObjectValue(stringIDToTypeID('layerEffects'));
      if(!desc.hasKey(stringIDToTypeID( 'frameFX'))) return null;
      desc = desc.getObjectValue(stringIDToTypeID('frameFX'));
      return desc.getUnitDoubleValue (stringIDToTypeID( 'size' ));
          }
      return null;
      };
            }catch(e){alert(e + "\n" + e.line);}
          };       
          // String message for BridgeTalk 
          var message = WinObject.toString();       
          // construct an anonymous instance and add it to the string 
          message += "\nnew WinObject();" 
          // $.writeln(message); // check it in the ESTK Console, just in case    
          var bt = new BridgeTalk(); 
          bt.target = "photoshop"; 
          bt.body = message; 
          bt.send(); 
      };
      
        • 1. Re: Script does not execute through an action
          mauricior6328708 Level 1

          Hi Ps-Design. I tested the action here and actually saw that the dialog does not open!

          I tried with that first version and it worked however the slider navigation is kinda heavy.

          Maybe this will help!

          #target photoshop;
          app.bringToFront();
          main();
          function main(){
          var strokeSize = getStrokeSize();
          if(strokeSize == null) return;
          var d= new Window('dialog','Style'); 
          d.grpOpacity = d.add('panel', undefined,'Stroke');   
          d.grpOpacity.orientation = 'row';   
          d.grpOpacity.alignChildren = ['fill','top'];   
          d.grpOpacity.grpSlider = d.grpOpacity.add('group');   
          d.grpOpacity.grpSlider.spacing = 0;   
          d.grpOpacity.grpSlider.orientation = 'row';   
          d.grpOpacity.grpSlider.st1 =  d.grpOpacity.grpSlider.add('statictext',undefined,'Stroke-Size');   
          d.grpOpacity.grpSlider.st1.alignment = 'left';   
          d.grpOpacity.grpSlider.grpSlider = d.grpOpacity.grpSlider.add('group');   
          d.grpOpacity.grpSlider.grpSlider.alignChildren = ['left','center'];   
          d.grpOpacity.grpSlider.grpSlider.spacing = 0;   
          d.slOpacity = d.grpOpacity.grpSlider.grpSlider.add('slider',undefined,0,0,255);   
          d.slOpacity.value = strokeSize; 
          d.slOpacity.preferredSize.width =100;   
          d.etOpacityValue = d.grpOpacity.grpSlider.grpSlider.add('edittext');   
          d.etOpacityValue.text = strokeSize; 
          d.etOpacityValue.addEventListener ('keydown', InitEditKeyboardHandler );   
          d.etOpacityValue.preferredSize.width = 40;   
          d.grpOpacity.grpSlider.grpSlider.stUnit = d.grpOpacity.grpSlider.grpSlider.add('statictext',undefined,' Pixels');   
          
          d.grpOpacity.grpSlider.grpSt = d.grpOpacity.grpSlider.add('group');   
          d.grpOpacity.grpSlider.grpSt.orientation = 'row';   
          d.grpOpacity.grpSlider.grpSt.alignment = 'fill';   
          d.grpOpacity.grpSlider.grpSt.spacing = 0;   
          d.grpOpacity.grpSlider.grpSt.margins = [5,0,0,0];   
          d.grpOpacity.grpSlider.grpSt.grpLeft = d.grpOpacity.grpSlider.grpSt.add('group');   
          //d.grpOpacity.grpSlider.grpSt.grpLeft.st1 = d.grpOpacity.grpSlider.grpSt.grpLeft.add('statictext',undefined,'0');   
          //d.grpOpacity.grpSlider.grpSt.grpLeft.st1.alignment = ['left','center'];   
          //d.grpOpacity.grpSlider.grpSt.grpLeft.st1.preferredSize.width = 200;   
          //d.grpOpacity.grpSlider.grpSt.grpLeft.st3 = d.grpOpacity.grpSlider.grpSt.grpLeft.add('statictext',undefined,'255');   
          //d.grpOpacity.grpSlider.grpSt.grpLeft.st3.alignment = ['right','center'];   
          //d.grpOpacity.grpSlider.grpSt.grpLeft.st3.preferredSize.width = 50;   
          
          d.grpButtons = d.add('group');   
          d.grpButtons.alignment = "right";   
          d.grpButtons.btnCanel = d.grpButtons.add('button', { x: 240,y: 125,  width: 75, height: 25 }, 'Cancel', {name: 'cancel'});  
          d.grpButtons.btnOK = d.grpButtons.add('button', { x: 90, y: 125,  width: 75, height: 25 }, 'Ok', { name: 'ok' }); 
          
          d.etOpacityValue.onChanging = d.etOpacityValue.onChange = function(){   
          var d = FindDialog(this);   
          d.slOpacity.value = Number(this.text);   
          d.slOpacity.onChange();   
                                                                                                                                
                                                                                                                             }   
              d.slOpacity.onChanging  = d.slOpacity.onChange = function(){   
               var d = FindDialog(this);   
               d.etOpacityValue.text = Math.round(this.value);   
               }   
                     d.slOpacity.onChange = function(){   
               var d = FindDialog(this);   
               setStrokeSize(Math.round(this.value));
             //  app.activeDocument.activeLayer.opacity = Math.round(this.value);   
               app.refresh();   
               }   
          d.grpButtons.btnCanel.onClick=function(){
              d.close(2);
              setStrokeSize(strokeSize);
              }
              d.show();   
                 
              function FindDialog( inItem ) {   
                   var w = inItem;   
                   while ( 'dialog' != w.type ) {   
                        if ( undefined == w.parent ) {   
                             w = null;   
                             break;   
                        }   
                        w = w.parent;   
                   }   
                   return w;   
              };
          function KeyHasModifier (event) {
              return event.shiftKey || event.ctrlKey || event.altKey || event.metaKey;
          }
          function KeyIsNumeric (event) {
              return  (event.keyName >= '0') && (event.keyName <= '9') && ! KeyHasModifier (event);
          }
          function KeyIsDelete (event) {
              //    Shift-delete is ok
              return (event.keyName == 'Backspace') && ! (event.ctrlKey);
          }
          function KeyIsLRArrow (event) {
              return ((event.keyName == 'Left') || (event.keyName == 'Right')) && ! (event.altKey || event.metaKey);
          }
          function KeyIsTabEnterEscape (event) {
              return event.keyName == 'Tab' || event.keyName == 'Enter' || event.keyName == 'Escape';
          }
              function InitEditKeyboardHandler (event) {   
                  try {   
                      var keyIsOK = KeyIsNumeric(event) ||   
                                                 KeyIsDelete(event) ||    
                                                 KeyIsLRArrow(event) ||   
                                                 KeyIsTabEnterEscape(event);                    
                      if (! keyIsOK) {   
                          event.preventDefault();   
                          app.beep();   
                      }   
                  }   
                  catch (e) {   
                  }   
              };  
          };
          // helper function for working with descriptors by Mike Hale
          function getActiveLayerProperty( psKey, psType ) {
              var ref = new ActionReference();
              ref.putProperty( charIDToTypeID( 'Prpr' ), psKey );
              ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
              if( undefined == psType ){
                return executeActionGet( ref ).getObjectValue( psKey );
              }else{
                  return executeActionGet( ref );
              }
          };
          function duplicateDescriptor( descriptor ) {
              var newDescriptor = new ActionDescriptor;
              newDescriptor.fromStream( descriptor.toStream() );
              return newDescriptor;
          };
          function localizeDescriptor( desc ) {
              var stream, pointer, zStringLength, zstring, localized_string, newZStringLength, previousStream, followingStream, newDesc;
              stream = desc.toStream();
              while( true ) {
                  pointer = stream.search(/TEXT....\x00\$\x00\$\x00\$/);
                  if( pointer === -1 ) {
                      break;
                  }
                  zStringLength = getLongFromStream( stream, pointer + 4 );
                  zstring = readUnicode( stream.substr( pointer + 8, ( zStringLength - 1 ) * 2) );
                  localized_string = ( localize( zstring ) ) + '\u0000';
                  newZStringLength = localized_string.length;
                  previousStream = stream.slice( 0, pointer);
                  followingStream = stream.slice( pointer + 8 + zStringLength * 2);
                  stream = previousStream.concat( 'TEXT', longToString( newZStringLength ), bytesToUnicode( localized_string ), followingStream );
              }
              newDesc = new ActionDescriptor();
              newDesc.fromStream( stream );
              return newDesc;
          };
          function getShortFromStream( stream, pointer ) {
              var hi, low;
              hi = stream.charCodeAt( pointer ) << 8 ;
              low = stream.charCodeAt( pointer + 1 );
              return hi + low;
          };
          function getLongFromStream( stream, pointer ) {
              var hi, low;
              hi = getShortFromStream( stream, pointer) << 16;
              low = getShortFromStream( stream, pointer + 2);
              return hi + low;
          };
          function readUnicode( unicode ) {
              var string = "";
              for( i = pointer = 0; pointer < unicode.length; i = pointer += 2) {
                  string +=String.fromCharCode( getShortFromStream( unicode, pointer ) );
              }
              return string;
          };
          function longToString( longInteger ) {
              var string;
              string = String.fromCharCode( longInteger >>> 24 );
              string += String.fromCharCode( longInteger << 8 >>> 24 );
              string += String.fromCharCode( longInteger << 16 >>> 24 );
              string += String.fromCharCode( longInteger << 24 >>> 24 );
              return string;
          };
          function bytesToUnicode( bytes ) {
              var unicode = "", char_code, charIndex;
              for( charIndex  = 0; charIndex < bytes.length; charIndex ++ ) {
                  char_code = bytes.charCodeAt( charIndex );
                  unicode += String.fromCharCode(char_code >> 8 ) +  String.fromCharCode( char_code & 0xff );
              }
              return unicode;
          };
          function setStrokeSize(Size ) {
              var layerEffects, newLayerEffects, currentDesc, newDesc, colorDesc, newLayerEffects, layerDesc, targetDesc, setDesc;
              layerEffects = getActiveLayerProperty( charIDToTypeID( 'Lefx' ) );
              newLayerEffects = duplicateDescriptor( layerEffects );
              currentDesc = layerEffects.getObjectValue( stringIDToTypeID( 'frameFX') );
              newDesc = duplicateDescriptor( currentDesc );  
              newDesc.putUnitDouble( charIDToTypeID('Sz  '), charIDToTypeID('#Pxl'), Size );
              newLayerEffects.putObject(  stringIDToTypeID( 'frameFX'),  stringIDToTypeID( 'frameFX'), newDesc );  
              newLayerEffects = localizeDescriptor( newLayerEffects );
              layerDesc = new ActionDescriptor();
              layerDesc.putObject( charIDToTypeID('Lefx'), charIDToTypeID('lfxv'), newLayerEffects);
              targetDesc = new ActionReference();
              targetDesc.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
              setDesc = new ActionDescriptor;
              setDesc.putObject( charIDToTypeID('T   '), charIDToTypeID('Lyr '), layerDesc );
              setDesc.putReference( charIDToTypeID('null' ), targetDesc );
              executeAction(charIDToTypeID('setd'), setDesc, DialogModes.NO);
          };
          function getStrokeSize(){
          var ref = new ActionReference();
          ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
          var desc = executeActionGet(ref);
          if(desc.hasKey(stringIDToTypeID( 'layerEffects' ))){
          if(!desc.getBoolean (stringIDToTypeID( 'layerFXVisible'))) return undefined;
          desc = desc.getObjectValue(stringIDToTypeID('layerEffects'));
          if(!desc.hasKey(stringIDToTypeID( 'frameFX'))) return null;
          desc = desc.getObjectValue(stringIDToTypeID('frameFX'));
          return desc.getUnitDoubleValue (stringIDToTypeID( 'size' ));
              }
          return null;
          };