3 Replies Latest reply on Apr 27, 2015 5:01 AM by lightwavemac

    how to view the actual code  for actions assigned to buttons,  such as "Go to state"

    lightwavemac Level 1

      How can I view the actual code for actions that I assign to buttons I've created.

        • 1. Re: how to view the actual code  for actions assigned to buttons,  such as "Go to state"
          Marc Autret Level 4

          Hi,

           

          You need to parse the collection myButton.behaviors which collects all kind of behaviors (GotoAnchorBehaviors, GotoFirstPageBehaviors, etc.). Each behavior has a behaviorEvent property that indicates which mouse or focus event triggers the behavior.

           

          There is no "actual code" in terms of event handlers, but you can explore and report sequentially the specific attributes of the behaviors.

          The snippet below is just a way of doing such job:

           

          // Assuming a Button object is selected
          // ---
          var myButton = app.selection[0];
          
          
          // =========================================================
          // FORMATTING UTILITIES
          // =========================================================
          
          var formatBehaviorCode = function F(/*Behavior*/o,  a,k)
          // ---
          // Extract human-friendly 'code' from a Behavior of any kind
          {
              F.SKIP||(F.SKIP={name:1,behaviorEvent:1,id:1,label:1,parent:1,index:1,/*add disregarded properties*/});
              
              k = o.__class__;
              o = o.properties;
              a = [localize("%1(%2)",k,o.enableBehavior?"enabled":"disabled")];
              delete o.enableBehavior;
              for( k in o )
                  {
                  if( !o.hasOwnProperty(k) || F.SKIP[k] ) continue;
                  a.push(localize("%1(%2)",k,o[k]));
                  }
              return a.join('; ');
          };
          
          var formatResults = function F(/*any*/o,/*?uint*/indent,  z,s,a,k)
          // ---
          // Format and prompt the results
          {
              if( 'object' != typeof o ) return ''+o;
          
              // Build the format string
              // ---
              s = '';
              z = (indent||(indent=0));
              while( z-- ) s+='    ';
              s += "[%1]" + (indent?' ':'\r') + "%2";
          
              // Explore the keys
              // ---
              a = [];
              z = 0;
              for( k in o )
                  {
                  if( !o.hasOwnProperty(k) ) continue;
                  a[z++] = localize( s, k, F(o[k],1+indent) );
                  }
              
              // Join
              // ---
              return a.join(indent ? '\r' : '\r\r');
          };
          
          
          // =========================================================
          // MAIN PROCESS
          // =========================================================
          
          var a = myButton.behaviors.everyItem().getElements(),
              r = {},
              s, t, w, u;
          
          // Compute
          // ---
          while( t=a.shift() )
              {
              s = t.behaviorEvent.toString();
              (r[s]||(r[s]=[])).push(formatBehaviorCode(t));
              }
          
          // Display
          // ---
          (w=new Window('dialog',"Behaviors triggered from "+myButton.name))
              .add('edittext',u,formatResults(r),{multiline:true}).parent
              .add('button',u,"Close",{name:'OK'}).parent;
          w.children[0].minimumSize = [600,250];
          w.show();
          

           

          Sample result:

           

          ButtonBehavior.png

           

          @+

          Marc

          • 3. Re: how to view the actual code  for actions assigned to buttons,  such as "Go to state"
            lightwavemac Level 1

            Marc, I have another question. Is it possible to create a button in a interactive document and use the button to launch and external exe. file?