Skip to main content
Known Participant
June 21, 2024
Answered

Outlining master page special characters s

  • June 21, 2024
  • 3 replies
  • 546 views

Below is a script which can outline text however is there a way to target master page text boxes on regular pages and even better - just the special characters (this is just a bonus). 

 

The reason being that when using another script for exporting indesign pages from a large multi page the page numbers all get exported wrong as the new file does not have the same pages etc 

Thanks 

 

Smyth 

app.doScript(function() {
    // Function to outline text in all text frames on a given page
    function outlineTextFrames(page) {
        var textFrames = page.textFrames;

        for (var i = 0; i < textFrames.length; i++) {
            var textFrame = textFrames[i];
            var text = textFrame.texts[0];

            // Convert text to outlines
            try {
                text.createOutlines();
            } catch (e) {
                // Handle error (e.g., empty text frame)
                $.writeln("Error outlining text: " + e);
            }
        }
    }

    // Get the active document
    var doc = app.activeDocument;

    // Loop through all pages in the document (excluding master spreads)
    for (var i = 0; i < doc.pages.length; i++) {
        var page = doc.pages[i];

        // Override all master page items on this page
        var masterItems = page.appliedMaster.pageItems.everyItem().getElements();
        for (var j = 0; j < masterItems.length; j++) {
            try {
                masterItems[j].override(page);
            } catch (e) {
                $.writeln("Error overriding master item: " + e);
            }
        }

        // Outline text frames
        outlineTextFrames(page);
    }

    // Save the document
    doc.save();
}, ScriptLanguage.JAVASCRIPT, null, UndoModes.ENTIRE_SCRIPT, "Outline Master Page Text");
This topic has been closed for replies.
Correct answer rob day

If it’s just the page numbers you want outlined then maybe this:

 

app.doScript(outlinePageNumbers, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Outline Page numbers');


function outlinePageNumbers(){
    var pg=app.activeDocument.pages.everyItem().getElements();
    for (var i = 0; i < pg.length; i++){
        overridePageNumbers(pg[i], pg[i].masterPageItems)
    }; 
}



/**
* Outline auto page numbers 
* @ param the page 
* @ param masterpage items array 
* @ return void 
*/
function overridePageNumbers(p, pa){
    var sc, pn
    for (var i = 0; i < pa.length; i++){
        if (pa[i].hasOwnProperty("texts")) {
            sc = pa[i].characters.everyItem().getElements()
            for (var j = 0; j < sc.length; j++){
                if (sc[j].contents == SpecialCharacters.AUTO_PAGE_NUMBER) {
                    pn = pa[i].override(p);
                    pn.createOutlines(true);
                } 
            };   
        } 
    }; 
}

3 replies

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
June 22, 2024

If it’s just the page numbers you want outlined then maybe this:

 

app.doScript(outlinePageNumbers, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Outline Page numbers');


function outlinePageNumbers(){
    var pg=app.activeDocument.pages.everyItem().getElements();
    for (var i = 0; i < pg.length; i++){
        overridePageNumbers(pg[i], pg[i].masterPageItems)
    }; 
}



/**
* Outline auto page numbers 
* @ param the page 
* @ param masterpage items array 
* @ return void 
*/
function overridePageNumbers(p, pa){
    var sc, pn
    for (var i = 0; i < pa.length; i++){
        if (pa[i].hasOwnProperty("texts")) {
            sc = pa[i].characters.everyItem().getElements()
            for (var j = 0; j < sc.length; j++){
                if (sc[j].contents == SpecialCharacters.AUTO_PAGE_NUMBER) {
                    pn = pa[i].override(p);
                    pn.createOutlines(true);
                } 
            };   
        } 
    }; 
}
Robert at ID-Tasker
Legend
June 22, 2024

@rob day

 

How about using Find and then only process found results? For bigger documents should be quicker? 

 

This will also find detached objects? 

 

And TFs within Groups or InLine / Anchored, etc.

 

But we can only guess why OP wants to outline texts... 

 

Known Participant
June 22, 2024

Hello, 

Thanks Rob will have a look 

RE: Robert

As meantioned there is a script (below) which enables indesign pages from a document to be saved out as separate indesign files  - however master text such as page numbers export poorly as of course they are relative to the document page count. So if exporting a single page of say 20 it will show as page 1 etc 

// 1. Control for documents open. If true, the script launches the dialog
if(app.documents.length > 0) {
    var doc = app.documents[0];
    if(doc.saved == true) {
        extractdlg();
    } else {
        alert("Please save your file first before processing the script");
    }
} else {
    alert("No documents open!");
}

// 2. Gathers many infos on the document.
function pageinfos() {
    var pg = doc.pages;
    var pglg  = pg.length;
    var pFirst = Number(pg[0].name);
    var pLast = Number(pg[pglg-1].name);
    var pgHeight = doc.documentPreferences.pageHeight;
    var pgWidth = doc.documentPreferences.pageWidth;
    var docname = String(doc.fullName).replace(/\.indd$/, "");
    var docpath = doc.filePath;
    var docfullname = doc.fullName;
    var infoarr = [pglg, pFirst, pLast, pgHeight, pgWidth, docname, docpath, docfullname];
    return infoarr;
}

// 3. Main function. First part is the dialog
function extractdlg() {
    var docfile = String(pageinfos()[7]);
    var dlg = app.dialogs.add({name : "Pages Extractor 1.1 - ©www.loicaigon.com"});
    with(dlg) {
        var firstclmn = dialogColumns.add();
        with(firstclmn) {
            var firstrow = dialogRows.add();
            with(firstrow) {
                var clmn1 = dialogColumns.add();
                with(clmn1) {
                    var row1 = dialogRows.add();
                    row1.staticTexts.add({staticLabel : "Extract pages..."});
                    var row2 = dialogRows.add();
                    with(row2) {
                        var r2c2 = dialogColumns.add();
                        with(r2c2) {
                            var r2c2r1 = dialogRows.add();
                            var pgStart = r2c2r1.realEditboxes.add({editValue:pageinfos()[1], minWidth: 30});
                        }
                        var r2c3 = dialogColumns.add();
                        with(r2c3) {
                            var r2c3r1 = dialogRows.add();
                            r2c3r1.staticTexts.add({staticLabel : "to"});
                        }
                        var r2c4 = dialogColumns.add();
                        with(r2c4) {
                            var r2c4r1 = r2c4.dialogRows.add();
                            var pgEnd = r2c4r1.realEditboxes.add({editValue:pageinfos()[2], minWidth: 30});
                        }
                    }
                }
            }
            var secondrow = dialogRows.add();
            with(secondrow) {
                var clmn2 = dialogColumns.add();
                with(clmn2) {
                    var row2 = dialogRows.add();
                    with(row2) {
                        var sepbox = checkboxControls.add({staticLabel: "Extract as separate pages", checkedState:false});
                    }
                }
            }
            var thirdrow = dialogRows.add();
            with(thirdrow) {
                var clmn3 = dialogColumns.add();
                with(clmn3) {
                    var row3 = dialogRows.add();
                    with(row3) {
                        var rembox = checkboxControls.add({staticLabel: "Remove pages after extraction", checkedState:false});
                    }
                }
            }
            var foutrhrow = dialogRows.add();
            with(foutrhrow) {
                var clmn4 = dialogColumns.add();
                with(clmn4) {
                    var row4 = dialogRows.add();
                    with(row4) {
                        var savebox = checkboxControls.add({staticLabel: "Choose other extraction folder", checkedState:false});
                    }
                }
            }
        }
    }

    // If the user made good choices, the script operates.
    if(dlg.show() == true) {
        if(pgStart.editValue >= pageinfos()[2] || pgEnd.editValue <= pageinfos()[1]) {
            alert("The pages numbers may be at least " + pageinfos()[1] + " for the first page of the range and " + pageinfos()[2] + " at maximum for the last page");
        } else {
            // If the user choose to pick a different folder, he will be asked for. Otherwise, the default folder is the one containing the file.
            if(savebox.checkedState == true) {
                var extractfolder = Folder.selectDialog ("Please choose a folder where to save extracted pages...");
                if(!extractfolder) {
                    exit();
                } else {
                    var saveextractfolder = String(extractfolder.fullName) + "/" + String(doc.name).replace(/\.indd/, "");
                }
            } else {
                var saveextractfolder = String(pageinfos()[5]);
            }

            var rem0 = pageinfos()[0] - 1;
            var rem2 = (pgStart.editValue - 2);

            // Variables definition regarding to the choice of the user to separate or not the extracted pages.
            if(sepbox.checkedState == true) {
                var W = pgEnd.editValue - pgStart.editValue + 1;
                var rem1 = pgStart.editValue;
            } else {
                var W = 1;
                var rem1 = pgEnd.editValue;
            }

            // Extraction loop
            for(w = 0; w < W; w++) {
                if(sepbox.checkedState == true) {
                    var exportdocname = "_Pg" + (pgStart.editValue + w) + ".indd";
                } else {
                    var exportdocname = "_Pg" + pgStart.editValue + "_to_Pg_" + pgEnd.editValue + ".indd";
                }

                // Convert special characters to static text before removing pages
                convertSpecialCharsToStaticText();

                for(var i = rem0; i >= rem1 + w; i--) {
                    doc.pages[i].remove();
                }

                for(var i = rem2 + w; i >= 0; i--) {
                    doc.pages[i].remove();
                }

                var exportdoc = doc.save(File(saveextractfolder + exportdocname));
                exportdoc.close(SaveOptions.no);

                if(sepbox.checkedState == true && w < (pgEnd.editValue - pgStart.editValue)) {
                    app.open(File(docfile));
                }
            }

            // If the user chose to remove the extracted pages from the original document, it will re-open the first document then remove the unuseful pages.
            if(rembox.checkedState == true) {
                app.open(File(docfile));
                for(var i = pgEnd.editValue - 1; i >= pgStart.editValue - 1; i--) {
                    doc.pages[i].remove();
                }
                app.activeDocument.close(SaveOptions.yes);
            }
        }
    }
}

// Function to convert special characters in page numbers to static text
function convertSpecialCharsToStaticText() {
    for (var i = 0; i < doc.pages.length; i++) {
        var page = doc.pages[i];
        for (var j = 0; j < page.textFrames.length; j++) {
            var textFrame = page.textFrames[j];
            for (var k = 0; k < textFrame.paragraphs.length; k++) {
                var paragraph = textFrame.paragraphs[k];
                paragraph.contents = paragraph.contents.replace(/[\u2018\u2019\u201C\u201D\u201E\u201F]/g, function (c) {
                    return c.charCodeAt(0);
                });
            }
        }
    }
}
rob day
Community Expert
Community Expert
June 22, 2024

Hi @SmythWharf , Are you trying to outline all of the document text or just the page numbers? If it’s all, I also wonder what the point of outlining is?

 

You could try this, which outlines everything:

app.doScript(outlineText, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Outline text');


function outlineText(){
    var pg=app.activeDocument.pages.everyItem().getElements();
    var ptf;
    for (var i = 0; i < pg.length; i++){
        overrideTextFrames(pg[i], pg[i].masterPageItems)
        ptf = pg[i].textFrames.everyItem().getElements();
        for (var j = 0; j < ptf.length; j++){
            ptf[j].createOutlines(true);
        };   
    }; 
}


/**
* Override a page’s masterpage text frames 
* @ param the page 
* @ param masterpage items array 
* @ return void 
*/
function overrideTextFrames(p, pa){
    for (var i = 0; i < pa.length; i++){
        if (pa[i].hasOwnProperty("texts")) {
           pa[i].override(p);
        } 
    }; 
}



 

Outlining destroys many text properties—bullets, underlines, frame strokes etc. so consider this example

 

Before:

 

 

After:

 

 

 

Robert at ID-Tasker
Legend
June 22, 2024

Why do you need to outline texts?