• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Javascript create text frame and apply object style on active document

New Here ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 27, 2020 Jul 27, 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 fol

...

Votes

Translate

Translate
Community Expert ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

LATEST

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 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines