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

InDesign Script error 21

Community Beginner ,
Jun 08, 2024 Jun 08, 2024

I had this working the other day before upgrading my version of InDesign. Now it doesnt work. Not sure if that's related. This little script throws a script error 21 on the line:

myParas[i].parentTextFrames[0].parentPage.appliedMaster = myDocument.masterSpreads.item("MC-Chapter");

I have double checked the correct spelling MC-Chapter. Any thoughts?

 

var myDocument = app.activeDocument;
var myParas = myDocument.stories.everyItem().paragraphs.everyItem().getElements();
var myPage = myDocument.pages;

for (i=0; i<myParas.length; i++)
{
    if(myParas[i].appliedParagraphStyle.name == "Chapter Title" || myParas[i].appliedParagraphStyle.name == "Chapter Number")
    {
    myParas[i].parentTextFrames[0].parentPage.appliedMaster = myDocument.masterSpreads.item("MC-Chapter");
    }
}
alert("Done");

TOPICS
Scripting
1.2K
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

Community Beginner , Jun 09, 2024 Jun 09, 2024

Thanks everyone! I resolved my issues with a different appraoch to the problem and have working code now that will apply odd and even masters to the document starting at a user defined page. For anyone looking to do the same, here's what works for me.

//applies left and right masters

var document = app.documents[0];
var masterSpreads = document.masterSpreads;
var pages = document.pages;
var master1Name = "MR-Right";
var master2Name = "ML-Left";


x = prompt ('What is the starting page of range?', '')
x =

...
Translate
Community Expert ,
Jun 08, 2024 Jun 08, 2024

Not sure what error 22 is, but if a text frame is on the pasteboard, you will get an error because that frame has a null parentPage.

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
Community Expert ,
Jun 08, 2024 Jun 08, 2024

If the suggestion from @brian_p_dts does not work for you then share the document and we shall be able to resolve the problem or let you know the reasons of the problem.

-Manan

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
Community Beginner ,
Jun 09, 2024 Jun 09, 2024

I've attached the file

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
Community Expert ,
Jun 09, 2024 Jun 09, 2024

Not sure if this works for you - but I have terrible trouble making scripts (I'm not that good) but I've started adding alerts to see where I go wrong
It might throw up where the issue is occurring. 

var myDocument = app.activeDocument;
var myParas = myDocument.stories.everyItem().paragraphs.everyItem().getElements();
var myPage = myDocument.pages;

for (var i = 0; i < myParas.length; i++) {
    try {
        var para = myParas[i];
        if (para.appliedParagraphStyle.name == "Chapter Title" || para.appliedParagraphStyle.name == "Chapter Number") {
            // Check if parentTextFrames and parentPage exist
            if (para.parentTextFrames.length > 0) {
                var parentPage = para.parentTextFrames[0].parentPage;
                // Check if parentPage is a valid page
                if (parentPage && parentPage.constructor.name === "Page") {
                    var masterSpread = myDocument.masterSpreads.item("MC-Chapter");
                    // Check if masterSpread is valid
                    if (masterSpread.isValid) {
                        parentPage.appliedMaster = masterSpread;
                    } else {
                        throw new Error("Master Spread 'MC-Chapter' is not valid.");
                    }
                } else {
                    throw new Error("Parent Page is not valid.");
                }
            } else {
                throw new Error("Paragraph is not within a text frame.");
            }
        }
    } catch (e) {
        alert("Error processing paragraph " + (i + 1) + ": " + e.message);
    }
}
alert("Done");

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
Community Beginner ,
Jun 09, 2024 Jun 09, 2024

Thanks everyone! I resolved my issues with a different appraoch to the problem and have working code now that will apply odd and even masters to the document starting at a user defined page. For anyone looking to do the same, here's what works for me.

//applies left and right masters

var document = app.documents[0];
var masterSpreads = document.masterSpreads;
var pages = document.pages;
var master1Name = "MR-Right";
var master2Name = "ML-Left";


x = prompt ('What is the starting page of range?', '')
x = x-2


//applies to all right pages
for (var n = x; n < pages.length; n++) {
if (n % 2 === 0) {
pages[n].appliedMaster = masterSpreads.itemByName(master1Name);
}
};

x = x+1
//applies to all left pages
for (var n = x; n < pages.length; n++) {
if (n % 2 != 0) {
pages[n].appliedMaster = masterSpreads.itemByName(master2Name)
}
};


alert("Done");

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
Community Expert ,
Jun 09, 2024 Jun 09, 2024

Just as an FYI, page has a .side property that returns an enum of a few different page positions including recto and verso

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
Community Beginner ,
Jun 09, 2024 Jun 09, 2024

Oh that would be great Brian, That would simplify things! what is the format of that?

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
Community Expert ,
Jun 09, 2024 Jun 09, 2024
LATEST

Hi @Serenity McLean , In case @brian_p_dts doesn’t reply right away here’s the page API:

 

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

 

so:

var pgs = document.pages.everyItem().getElements();

for (var i = 0; i < pgs.length; i++){
    $.writeln(pgs[i].side)
    //returns RIGHT_HAND or LEFT_HAND for a facing page doc
};   
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
Engaged ,
Jun 09, 2024 Jun 09, 2024

I have checked the above code. It's working fine. Could you please tell me which InDesign version you are using?

Indesign 2021
16.3.2

Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.
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
Community Beginner ,
Jun 09, 2024 Jun 09, 2024

InDesign 2023

18.5.2

 

I upgraded from 2021, 16.4.3

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