Skip to main content
Inspiring
April 30, 2010
Answered

How to convert textframe to button

  • April 30, 2010
  • 2 replies
  • 3494 views

How to convert textframe to button?

This topic has been closed for replies.
Correct answer Marc Autret

I didn't find a 'clean' way to do this through the DOM. As a workaround you can emulate a "Convert to button" action, provided your script is allowed to select the textframe target:

// convertToButton for ID CS4


function convertToButton(/*PageItem*/obj, /*str*/bName, /*str*/bDesc)
{
     app.selection = [obj];
     var maCreateButton = app.menuActions.item('$ID/$$$/Dialog/CmdName/CreateButton'),
          ret;

     if( !maCreateButton.isValid ) return false;


     maCreateButton.invoke();
     ret = app.selection[0];
          
     ret.name = bName||ret.name;
     ret.description = bDesc||ret.description;
          
     return ret;
};

// sample code

var myTextFrame = app.activeDocument.textFrames[0];

var myButton = convertToButton(myTextFrame);

Note: this approach is used in FlyingButtons:

http://www.indiscripts.com/post/2010/03/flyingbuttons-a-script-to-automate-pdf-portfolios

@+

Marc

2 replies

Community Expert
August 20, 2019

Hi mrinmoy1984 ,

you can do the following without using a menu action:

1. Add a button to the document using buttons.add()

2. Access the first state of the button and use method addItemsToState()

The only argument of that method is an array that contains the item or the items you like to convert to a button.

3. Get rid of a rectangle that was automatically added to the first state when doing buttons.add().

With one element on spread one of a document, the text frame you want to convert to a button, run this script snippet:

var doc = app.documents[0];

var itemYouLikeToConvert = doc.spreads[0].textFrames[0];

var myButton = doc.spreads[0].buttons.add();

var tempRect = myButton.states[0].groups[0].rectangles[0];

myButton.states[0].addItemsToState([itemYouLikeToConvert]);

tempRect.remove();

Regards,
Uwe

sreekarthikeyank
Known Participant
August 21, 2019

Thanks you so much. Can you please suggest me to enable the property of below

Community Expert
August 21, 2019

Hi sreekarthikeyank ,

look into DOM documentation for the Button object:

Adobe InDesign CS6 (8.0) Object Model JS: Button

There you'll find all properties you'll need.

Especiall look into the various behaviors that are offered.

Especially this one:

Adobe InDesign CS6 (8.0) Object Model JS: ShowHideFieldsBehavior

myButton.showHideFieldsBehaviors.add

(

    {

        behaviorEvent : BehaviorEvents.MOUSE_DOWN

    }

);

Properties fieldsToHide and fieldsToShow are of interest as well.

Regards,
Uwe

Marc Autret
Marc AutretCorrect answer
Legend
April 30, 2010

I didn't find a 'clean' way to do this through the DOM. As a workaround you can emulate a "Convert to button" action, provided your script is allowed to select the textframe target:

// convertToButton for ID CS4


function convertToButton(/*PageItem*/obj, /*str*/bName, /*str*/bDesc)
{
     app.selection = [obj];
     var maCreateButton = app.menuActions.item('$ID/$$$/Dialog/CmdName/CreateButton'),
          ret;

     if( !maCreateButton.isValid ) return false;


     maCreateButton.invoke();
     ret = app.selection[0];
          
     ret.name = bName||ret.name;
     ret.description = bDesc||ret.description;
          
     return ret;
};

// sample code

var myTextFrame = app.activeDocument.textFrames[0];

var myButton = convertToButton(myTextFrame);

Note: this approach is used in FlyingButtons:

http://www.indiscripts.com/post/2010/03/flyingbuttons-a-script-to-automate-pdf-portfolios

@+

Marc

Inspiring
April 30, 2010

Thanks mark,

It's working. I've also tried to do it from DOM. But failed to find appropriate method. Thanks again!!!!

sreekarthikeyank
Known Participant
August 20, 2019

Can you please suggest to handle the same in server instead of selection. Your help should be appreciated.