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

First line of each text frame

Explorer ,
Oct 31, 2023 Oct 31, 2023

Copy link to clipboard

Copied

Is there a way to specify the number of words or characters in the first line of each text frame that has a given Object style applied?

 

I want to limit the first line to about 3-5 words, for each text frame that has a specific object style applied to it, then every subsequent line to have text as per normal.

TOPICS
How to

Views

790

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 , Nov 05, 2023 Nov 05, 2023

Hi @Theo331728749dq2 , A scripting solution might be this—expects a text frame with the applied object style to be selected:

 

 

 

var min = 3
var max = 5
//a selected text frame
var s = app.activeDocument.selection[0];
//random number for forced line break
var rn = Math.floor(Math.random() * (max - min + 1) ) + min;

if (s.constructor.name == "TextFrame") {
    //the selected frame’s object style
    var os = s.appliedObjectStyle;
    var api = app.activeDocument.allPageItems;
    var tf, ii;
 
...

Votes

Translate

Translate
Explorer ,
Nov 14, 2023 Nov 14, 2023

Copy link to clipboard

Copied

It can start on any text frame whether or not it is a new paragrph. I am new to scripts and cant figure out how to run this script, if anyone can provide any additional info on how to get it working.

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
Community Expert ,
Nov 15, 2023 Nov 15, 2023

Copy link to clipboard

Copied

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
Explorer ,
Nov 15, 2023 Nov 15, 2023

Copy link to clipboard

Copied

I tried that, nothing happens when i try to run the script. Other scripts that I have run.

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
Community Expert ,
Nov 15, 2023 Nov 15, 2023

Copy link to clipboard

Copied

Can you attach a sample document where the script doesn't work?

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
Community Expert ,
Nov 15, 2023 Nov 15, 2023

Copy link to clipboard

Copied

Do you have a text frame selected when you run the script?

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
Explorer ,
Nov 15, 2023 Nov 15, 2023

Copy link to clipboard

Copied

I tried it on a new document and it worked, but with regards to the document I am working on I get the following error:

Error no: 45

Error String : Object string is invalid

Line 18:

Source: if (tf.lines[0].characters[-1].contents == SpecialCharacters.FORCED_LINE_BREAK) {

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
Community Expert ,
Nov 15, 2023 Nov 15, 2023

Copy link to clipboard

Copied

quote

I tried it on a new document and it worked, but with regards to the document I am working on I get the following error:

Error no: 45

Error String : Object string is invalid

Line 18:

Source: if (tf.lines[0].characters[-1].contents == SpecialCharacters.FORCED_LINE_BREAK) {


By @Theo331728749dq2

 

Looks like you have an empty TextFrame?

 

Empty as with no contents or empty because of the overset text.

 

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
Community Expert ,
Nov 15, 2023 Nov 15, 2023

Copy link to clipboard

Copied

@Robert at ID-Tasker is right, any empty text frame in the doc would throw that error. Try this:

 

var min = 3
var max = 5
//a selected text frame
var s = app.activeDocument.selection[0];
//random number for forced line break
var rn = Math.floor(Math.random() * (max - min + 1) ) + min;

if (s.constructor.name == "TextFrame") {
    //the selected frame’s object style
    var os = s.appliedObjectStyle;
    var api = app.activeDocument.allPageItems;
    var tf, ii;
    for (var i = 0; i < api.length; i++){
        //get all text frames with the same style
        if (api[i].constructor.name == "TextFrame" && api[i].appliedObjectStyle == os){
            tf = api[i];
            try {
                //clear any existing force line breaks
                if (tf.lines[0].characters[-1].contents == SpecialCharacters.FORCED_LINE_BREAK) {
                    tf.lines[0].characters[-1].contents = ""
                }
                //insert line break
                var ip = tf.lines[0].words[rn].insertionPoints[0]
                ip.contents = SpecialCharacters.FORCED_LINE_BREAK;
            }catch(e) {}     
        }
    };   
} 

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
Explorer ,
Nov 15, 2023 Nov 15, 2023

Copy link to clipboard

Copied

LATEST

Hi Rob, thanks, this script worked.

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