Skip to main content
Bedazzled532
Inspiring
May 12, 2024
Answered

How to override multiple named textframes only through script

  • May 12, 2024
  • 1 reply
  • 676 views

I have a script handy which overrides a textframe in document pages.

Process is, I go the the master page and select the text frame I want to be overriden from the document pages. Apply "bluepara" parastyle for blue and "yellowpara" parastyle yellow boxes.

 

Here is the overriding script I am using:

 

function main() {
    var doc = app.activeDocument;
    var sel = doc.selection[0];
    if (
        sel == undefined
        || !sel.isValid
        || !sel.hasOwnProperty('parentPage')
        || sel.parentPage.parent.constructor.name !== "MasterSpread"
    ) {
        alert("Error\rSelect exactly one item on a master page and try again.");
        return;
    }
    var masterSpread = sel.parentPage.parent,
        counter = 0;
    for (var i = 0; i < doc.pages.length; i++) {
        var page = doc.pages[i];
        if (page.appliedMaster == masterSpread) {
            try {
                sel.override(page);
                counter++;
            } catch (error) {
                // item doesn't exist on that page
            }
        }
    }
    alert('The selected item was overridden on ' + counter + ' pages.')
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Override Selected Master Page Item');

 

 

The script works fine if I have selected just 1 text frame. It does not work for multiple textframe. Ideally it should.

I am doing it individually currently. After that I run another script to put a particular special charater in that text frame.

 

Here is the another script:

 

myDoc = app.documents[0];
var txtFrame = myDoc.textFrames.everyItem().getElements();
for(var i = 0 ; i < txtFrame.length; i++){
    if(txtFrame[i].name == "blue"){ 
      txtFrame[i].insertionPoints[-1].contents = SpecialCharacters.ZERO_WIDTH_NONJOINER; 
    }
}

for(var i = 0 ; i < txtFrame.length; i++){
    if(txtFrame[i].name == "yellow"){        
      txtFrame[i].insertionPoints[-1].contents = SpecialCharacters.ZERO_WIDTH_NONJOINER; 
    }
}

 

I have named the textframes as "blue" and "yellow". There are other textframes also. I just want blue and yellow to be overridden and populated with the special character.

How can I combine both in a single script and it should work for named textframes "blue" and "yellow" only and ignore other textframes.

 

I have attached the idml file.

This topic has been closed for replies.
Correct answer Bedazzled532

@Robert at ID-Tasker Thanks Robert. I have modified the script and it worked. Here is the updated script. 

function main() {

    var doc = app.activeDocument;
    var sel = app.selection[0];

    var masterSpread = sel.parentPage.parent;

for(s=0;s<doc.pages.length;s++){
	var page = doc.pages[s];
		if(page.appliedMaster == masterSpread){
			for (var i = 0; i < app.selection.length; i++) {
				var selectedItem = app.selection[i];
				
					try {
						selectedItem.override(page);
						
					} catch (error) {
						// item doesn't exist on that page
					}
				
			}
		}
}
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Override Selected Master Page Item');

 

1 reply

Robert at ID-Tasker
Legend
May 12, 2024

The same way as you are looping through Pages, TextFrames, etc. collections - you can loop through app.selection

 

for (var s = 0; s < app.selection.length; s++) {
        var selectedItem = app.selection[s];
... 

 

But you have to be aware, that there is app.selection and document.selection - two different things.

 

Bedazzled532
Bedazzled532AuthorCorrect answer
Inspiring
May 14, 2024

@Robert at ID-Tasker Thanks Robert. I have modified the script and it worked. Here is the updated script. 

function main() {

    var doc = app.activeDocument;
    var sel = app.selection[0];

    var masterSpread = sel.parentPage.parent;

for(s=0;s<doc.pages.length;s++){
	var page = doc.pages[s];
		if(page.appliedMaster == masterSpread){
			for (var i = 0; i < app.selection.length; i++) {
				var selectedItem = app.selection[i];
				
					try {
						selectedItem.override(page);
						
					} catch (error) {
						// item doesn't exist on that page
					}
				
			}
		}
}
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Override Selected Master Page Item');

 

Robert at ID-Tasker
Legend
May 14, 2024

@Bedazzled532

 

You are welcome. 

 

There is one more thing - you should create array or collection of selected objects and then iterate through this array / collection - otherwise, your code won't work when selection changes - you are working on a live selection.