Skip to main content
Participant
July 28, 2020
Answered

Javascript create text frame and apply object style on active document

  • July 28, 2020
  • 1 reply
  • 1528 views

Hi all,

 

I'm hoping someone can point me in the right direction to learn some basic scripting.

 

I've followed the indesign scripting basics tutorial pdf to create the basic Helloworld text frame script. (javascript)

 

What I'd like to know is, how do I apply an object style to said text frame within that script (for my purposes, the object style will already exist on the active document).

 

Any guidance is much appreciated.

 

Regards,

 

Chris

This topic has been closed for replies.
Correct answer Manan Joshi

Hi Chris,

Try the following

To run the code make a selection of a the textframe over which you want to apply the object style. If you already have the object then you can replace tf with the object instance that you have. Change the name Obj_Style_To_Apply to whatever is the name of your style

 

var tf = app.selection[0]
var os = app.documents[0].objectStyles.itemByName("Obj_Style_To_Apply")
if(os.isValid)
	tf.applyObjectStyle(os)

 

To learn about the properties and methods available use the following link

https://www.indesignjs.de/extendscriptAPI/indesign-latest/

 

-Manan

1 reply

Manan JoshiCommunity ExpertCorrect answer
Community Expert
July 28, 2020

Hi Chris,

Try the following

To run the code make a selection of a the textframe over which you want to apply the object style. If you already have the object then you can replace tf with the object instance that you have. Change the name Obj_Style_To_Apply to whatever is the name of your style

 

var tf = app.selection[0]
var os = app.documents[0].objectStyles.itemByName("Obj_Style_To_Apply")
if(os.isValid)
	tf.applyObjectStyle(os)

 

To learn about the properties and methods available use the following link

https://www.indesignjs.de/extendscriptAPI/indesign-latest/

 

-Manan

-Manan
cfebvreAuthor
Participant
July 28, 2020

Hi Manan,

 

Thanks so much for your reply. With your help I was able to figure it out (sorry was on my mobile before and probably didn't explain myself very well. I actually needed the object style to be applied to the text frame in the same script, so:

var doc = app.activeDocument;
var textFrame = doc.pages[0].textFrames.add();
var objectStyle = app.documents[0].objectStyles.itemByName("BodyCopy")
textFrame.properties =
{
  geometricBounds : [ 10 , 10 , 100 , 100],
  strokeWidth : 0,
  fillColor : "None",
  contents : "Hello World!"
};

if(objectStyle.isValid)
	textFrame.applyObjectStyle(objectStyle)

 With this, I'll be able to do exactly what I need. Thanks so much 🙂