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

How to override multiple named textframes only through script

Enthusiast ,
May 12, 2024 May 12, 2024

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.

TOPICS
How to , Scripting
644
Translate
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

Enthusiast , May 14, 2024 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);
						
					} ca
...
Translate
LEGEND ,
May 12, 2024 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.

 

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

Translate
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
LEGEND ,
May 12, 2024 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.

 

Translate
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
Enthusiast ,
May 14, 2024 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');

 

Translate
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
LEGEND ,
May 14, 2024 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.

 

Translate
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
Enthusiast ,
May 14, 2024 May 14, 2024

@Robert at ID-Tasker Thanks Robert but an example would be helpful, preferably from the code which I have written. Actually, I am not very good at programming. Trying to learn from masters like you and others.

Thanks 

Translate
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
LEGEND ,
May 14, 2024 May 14, 2024

@Bedazzled532

 

I'm not JS guy and I'm on my phone - but I'll try:

 

If you change this:

    var sel = app.selection[0];

to this:

    var sel = app.selection;

 

Then this:

    var masterSpread = sel.parentPage.parent;

to this:

    var masterSpread = sel[0].parentPage.parent;

 

Then this:

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

to this:

			for (var i = 0; i < sel.length; i++) {
				var selectedItem = sel[i];

 

It should work.

 

Translate
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
Enthusiast ,
May 14, 2024 May 14, 2024
LATEST

@Robert at ID-Tasker Thank you so much. I will try this and update.

Translate
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