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
Inspiring
May 12, 2024

Thanks I will chk that.

 

By the way what is the major difference betwen app.selection and document.seleciton.

What are the things which we can get from app.selection and document.selection

 

Thanks

Robert at ID-Tasker
Legend
May 12, 2024
quote

By the way what is the major difference betwen app.selection and document.seleciton.

What are the things which we can get from app.selection and document.selection


By @Bedazzled532

 

app.selection - will return selection of the currently active document.

 

document.selection - will return selection of the specified document - it doesn't have to be the active one.