Skip to main content
Inspiring
May 31, 2022
Answered

Script to check, if primary text frame is used on specific page

  • May 31, 2022
  • 2 replies
  • 391 views

I don’t have an idea, on how to start a script, which checks every page of a document, if the primary text frame of the master page is used or if it is still »unlocked«. Can someone please give me a hint, where to start this?

 

Thanks,

Tobias

This topic has been closed for replies.
Correct answer rob day

Hi Tobias , Not sure if someone can come up with something simpler, but try this:

 

 


var pgs = app.activeDocument.pages;
var tf, pfs;

for (var i = 0; i < pgs.length; i++){
    tf = pgs[i].textFrames;
    for (var j = 0; j < tf.length; j++){
        if (tf[j].overridden) {
            //get an array of primary frame ids from the frame’s master spread
            pfs = getPrimaryFrames(tf[j].overriddenMasterPageItem.parent);
            //checks if the overridden text frame id is in the primary frames array
            if (checkItem(pfs, tf[j].overriddenMasterPageItem.id)) {
                $.writeln("this text frame is primary")
            } 
        } 
    };   
};   



/**
* Get a list of a master spread’s primary text frame’s ids 
* @ param the master spread to check 
* @ return an array of ids 
* 
*/
function getPrimaryFrames(ms){
    var pf = ms.primaryTextFrame;
    var a = [pf.id]
    while (pf !=null) {
       pf = pf.nextTextFrame;
       try {a.push(pf.id)}catch(e) {}  
    }
    return a
}


/**
* Checks if an item is in an array
* @ param the array to check 
* @ param the item to look for 
* @ return true if the item is in the array 
* 
*/
function checkItem(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

 

 

2 replies

rob day
rob dayCorrect answer
Community Expert
May 31, 2022

Hi Tobias , Not sure if someone can come up with something simpler, but try this:

 

 


var pgs = app.activeDocument.pages;
var tf, pfs;

for (var i = 0; i < pgs.length; i++){
    tf = pgs[i].textFrames;
    for (var j = 0; j < tf.length; j++){
        if (tf[j].overridden) {
            //get an array of primary frame ids from the frame’s master spread
            pfs = getPrimaryFrames(tf[j].overriddenMasterPageItem.parent);
            //checks if the overridden text frame id is in the primary frames array
            if (checkItem(pfs, tf[j].overriddenMasterPageItem.id)) {
                $.writeln("this text frame is primary")
            } 
        } 
    };   
};   



/**
* Get a list of a master spread’s primary text frame’s ids 
* @ param the master spread to check 
* @ return an array of ids 
* 
*/
function getPrimaryFrames(ms){
    var pf = ms.primaryTextFrame;
    var a = [pf.id]
    while (pf !=null) {
       pf = pf.nextTextFrame;
       try {a.push(pf.id)}catch(e) {}  
    }
    return a
}


/**
* Checks if an item is in an array
* @ param the array to check 
* @ param the item to look for 
* @ return true if the item is in the array 
* 
*/
function checkItem(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

 

 

Inspiring
June 1, 2022

Thanks @rob day, this is an excellent starting point for my needs!

Inspiring
May 31, 2022

First findings in the API:

 

var theDoc = app.activeDocument,
    thePage = theDoc.pages[7];

var theFrames = thePage.textFrames,
    hasOverrides = false;

for ( var i = 0; i < theFrames.length; i++) {
    if ( theFrames[i].overridden ) {
        hasOverrides = true;
    }
}

if ( ! hasOverrides ) {
    var thePrimaryTextFrame = thePage.appliedMaster.primaryTextFrame;
    if ( thePrimaryTextFrame ) {
        thePrimaryTextFrame.override(thePage);
    }
}