Skip to main content
Known Participant
April 23, 2015
Question

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

  • April 23, 2015
  • 1 reply
  • 631 views

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

This topic has been closed for replies.

1 reply

Marc Autret
Legend
April 23, 2015

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 ) continue;

        a.push(localize("%1(%2)",k,o));

        }

    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,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||(r=[])).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:

@+

Marc

Known Participant
April 23, 2015

Thanks Marc