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

<InDesign>Script to change font partly with JavaScript

Community Beginner ,
Mar 13, 2020 Mar 13, 2020

Copy link to clipboard

Copied

I want to program a script that enables me to change font partly with JavaScript, and I'm having a little trouble.

Here's what I want to do.

 

<input(within a edit box in a dialog)>

He picked up trash in **<his spare time>** to dump in his neighbor's yard. I am counting my calories, yet I want dessert. **<The beauty>** of the African sunset disguised the danger lurking nearby.

 

(all sentences are the same font.)

 

<output(within a text frame in Indesign Application)>

He picked up trash in his spare time to dump in his neighbor's yard. I am counting my calories, yet I want dessert. The beauty of the African sunset disguised the danger lurking nearby.

 

(Only the underlined parts are set in a different font.)

 

I summarized this process in the following 3steps, and I managed to program the first 2steps.

 

1. find "**<" and ">**", get those indexes.

2. remove "**<" and ">**" and get original sentences.

3. process the font changes with data collected in step1.

 

And this is the code I programmed.

 

var textFrames_All  = app.activeDocument.textFrames;
var myPH=0;
main();
function main(){
    var selected=app.activeDocument.selection;

    alert (selected[0].contents);
    var dlg = app.dialogs.add({name: "Text Customizer",  canCancel: true});
    with(dlg){
        with(dialogColumns.add()){
            with(borderPanels.add()){
                myPH=textEditboxes.add({editContents: "write input here...", minWidth:50});
            }
        }
     }
    if(dlg.show()==true){
        var input=myPH.editContents;
        var positionS=-1;
        var positionE=-1;
        var fontData=new Array();
        
        while((positionS=input.indexOf("**<"))!=-1){
            input=input.substring(0,positionS)+input.substring(positionS+3);
            positionE=input.indexOf(">**");
            input=input.substring(0,positionE)+input.substring(positionE+3);
            var add=[positionS, positionE];
            fontData.push(add);
        }
        selected[0].contents=input;
        
        for(var i=0;i<fontData.length;i++){
            $.writeln(fontData[i][0]);
            $.writeln(fontData[i][1]);
            selected[0].characters[10];
            for(var i1=fontData[i][0]; i1<fontData[i][1];i1++){
                // STEP3 should be here...
            }
        }
    }  
}

 

 

About step3, I tried to code like this, but I failed.

Please help me solve coding step3.

 

selected[0].characters[i1].appliedFont=app.fonts.item("FONT NAME");

 

P.S.

Since I'm Japanese and not good at English, pls tell me if you have something that you can't comprehend.

TOPICS
Scripting , SDK

Views

1.0K

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 , Mar 13, 2020 Mar 13, 2020

Hi Shion,

 

Change your for loop to the following, the idea is to apply the font to the range of text within the indexes that you have stored in your array. Insertionpoint can be equated to the cursor position in the text, look at the insertiopoint api documentation at

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#InsertionPoints.html

for(var i=0;i<fontData.length;i++)
{
	var ins = selected[0].insertionPoints.itemByRange(fontData[i][0], fontData[i][1])
	ins.appliedFont = "Arial"
}

 

...

Votes

Translate

Translate
Community Expert ,
Mar 13, 2020 Mar 13, 2020

Copy link to clipboard

Copied

Hi Shion,

 

Change your for loop to the following, the idea is to apply the font to the range of text within the indexes that you have stored in your array. Insertionpoint can be equated to the cursor position in the text, look at the insertiopoint api documentation at

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#InsertionPoints.html

for(var i=0;i<fontData.length;i++)
{
	var ins = selected[0].insertionPoints.itemByRange(fontData[i][0], fontData[i][1])
	ins.appliedFont = "Arial"
}

 

-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
Community Beginner ,
Mar 13, 2020 Mar 13, 2020

Copy link to clipboard

Copied

Thank you for your reply.

 

I tried to use appliedFont property, but it occurs error that "Object is invalid".

I don't know why..

 

image.png

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 ,
Mar 13, 2020 Mar 13, 2020

Copy link to clipboard

Copied

Also look into find/change grep, if you use that then you could put the total content into InDesign and use Grep to find and change its format.

 

-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
Community Expert ,
Mar 13, 2020 Mar 13, 2020

Copy link to clipboard

Copied

Seems the ins object is invalid, that can happen if the indexes that we used to form this object are incorrect. Check what happens with ins.contents, does it give any output. If not then you have an issue with the calculated indexes. With your sample input the code i gave worked for me, so that could be a starting point for you to debug.

 

-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
Community Beginner ,
Mar 13, 2020 Mar 13, 2020

Copy link to clipboard

Copied

LATEST

Thank you for your reply!

 

From the conclusion, I managed to solve it!

The reason why it occurred error was that the way I selected the target object was wrong.

It should be definitely selected (not in editor mode) and that made it wrong.

 

The final code is here!

Thank you so much, Manan!

I really appreciate your support!

 

 

var textFrames_All  = app.activeDocument.textFrames;
var myPH=0;
main();
function main(){
    var selected=app.activeDocument.selection;
    alert (selected[0].contents);
    var dlg = app.dialogs.add({name: "Text Customizer",  canCancel: true});
    with(dlg){
        with(dialogColumns.add()){
            with(borderPanels.add()){
                myPH=textEditboxes.add({editContents: "write input here", minWidth:50});
            }
        }
     }
    if(dlg.show()==true){
        var input=myPH.editContents;
        var positionS=-1, positionE=-1;
        var fontData=new Array();        
        while((positionS=input.indexOf("**<"))!=-1){
            input=input.substring(0,positionS)+input.substring(positionS+3);
            positionE=input.indexOf(">**");
            input=input.substring(0,positionE)+input.substring(positionE+3);
            var add=[positionS, positionE];
            fontData.push(add);
        }
        selected[0].contents=input;
        
        for(var i=0;i<fontData.length;i++){
            var ins = selected[0].insertionPoints.itemByRange(fontData[i][0], fontData[i][1]);
            ins.appliedFont="FontName";
            ins.appliedCharacterStyle.name="Bold";
        }
    }  
}

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