Skip to main content
wckdtall
Inspiring
March 25, 2023
Answered

.search keeps failing in this script

  • March 25, 2023
  • 2 replies
  • 1244 views

I seem to be having a problem with variable scope, I wrote the below script that works fine by itself, but when I add it my main document .search always results in -1, even when it's searching a string that should result in 0+. I found that moving it to the very top of my document it works fine, but moving it below even the first function with everything commented out it's wrong. I even tried moving that initial function to another document and it works fine. 

I'm guessing there's some sort of circular reference that's setting the search incorrectly, any pointers on helping find?

 

 

function idLegalLineCount() {
        var newAD = app.activeDocument;
        var lgCnt = [];
//Loop through all the pages of the document
        for (var y = 0; y < newAD.pages.length; y++) {
            var aPg = newAD.pages[y];
            var aFr = aPg.textFrames;
            var aMF = aPg.masterPageItems;
            var foundYou = false;
            //Look through Page Text Frames
            for (var x = 0; x < aFr.length; x++) {
                var aPs = aFr[x].parentStory.contents;
                if (aPs.search(/©/gi) > -1) var foundYou = true; //alert((y + 1) + " - " + foundYou) }
            }
            //Look through Master Page Text Frames
            for (var z = 0; z < aMF.length; z++) {
                if (aMF[z].constructor.name != "TextFrame") continue;
                var mPs = aMF[z].parentStory.contents;
                if (mPs.search(/©/gi) > -1) var foundYou = true; //alert((y + 1) + "- M - " + foundYou) }
            }
            if (foundYou == false) lgCnt.push(y + 1);
        }
        return lgCnt;
    }

 

 

Correct answer wckdtall

This search came back, and I did extensive testing on scope as the functions I had trouble with aren't a problem on their own.
Ultimately, what I discovered, was that searching for a "©" was incorrectly returning a -1, when .search was nested, with regex and as a string. InDesign character ^2 didn't work, but String.fromCharCode(169) did!

//Revised Code below

function idLegalLineCount() {
    var newAD = app.activeDocument;
    //Set copyright symbol Character to a string. © Character itself will cause errors in certain situations.
    var cRSym = String.fromCharCode(169);
    var lgCnt = [];
    //Loop through all the pages of the document
    for (var y = 0; y < newAD.pages.length; y++) {
        var aPg = newAD.pages[y];
        var aFr = aPg.textFrames;
        var aMF = aPg.masterPageItems;
        var foundYou = false;
        //Look through Page Text Frames
        for (var x = 0; x < aFr.length; x++) {
            var aPs = aFr[x].parentStory.contents;
            // OLD METHOD Doesn't always work
            //if (aPs.search(/©/gi) > -1) var foundYou = true; //alert((y + 1) + " - " + foundYou)
            //search(cRSym) returns correct results
            if (aPs.search(cRSym) > -1) var foundYou = true; //alert((y + 1) + " - " + foundYou)
        }
    }
    //Look through Master Page Text Frames
    for (var z = 0; z < aMF.length; z++) {
        if (aMF[z].constructor.name != "TextFrame") continue;
        var mPs = aMF[z].parentStory.contents;
        // OLD METHOD Doesn't always work        
        //if (mPs.search(/©/gi) > -1) var foundYou = true; //alert((y + 1) + "- M - " + foundYou);
        //search(cRSym) returns correct results
        if (mPs.search(cRSym) > -1) var foundYou = true; //alert((y + 1) + "- M - " + foundYou) 
    }
}
if (foundYou == false) lgCnt.push(y + 1);
    }
return lgCnt;
}



2 replies

wckdtall
wckdtallAuthor
Inspiring
March 25, 2023

Further... I copied the contents of the file to another that's stored right next to it and it works fine there. I tried changing the name of the problem file and it doesn't work. Could I have something cached somewhere?

wckdtall
wckdtallAuthor
Inspiring
March 25, 2023

Ultimately, I think something funky happened to headers or the server that my file is on.
To fix:
1. Renamed fileA.jsx to fileA_bkup.jsx
2. Copied contents from fileA_bkup.jsx ->fileB.jsx
3. Renamed fileB.jsx -> fileA.jsx
4. File works completely fine again.

wckdtall
wckdtallAuthorCorrect answer
Inspiring
February 11, 2025

This search came back, and I did extensive testing on scope as the functions I had trouble with aren't a problem on their own.
Ultimately, what I discovered, was that searching for a "©" was incorrectly returning a -1, when .search was nested, with regex and as a string. InDesign character ^2 didn't work, but String.fromCharCode(169) did!

//Revised Code below

function idLegalLineCount() {
    var newAD = app.activeDocument;
    //Set copyright symbol Character to a string. © Character itself will cause errors in certain situations.
    var cRSym = String.fromCharCode(169);
    var lgCnt = [];
    //Loop through all the pages of the document
    for (var y = 0; y < newAD.pages.length; y++) {
        var aPg = newAD.pages[y];
        var aFr = aPg.textFrames;
        var aMF = aPg.masterPageItems;
        var foundYou = false;
        //Look through Page Text Frames
        for (var x = 0; x < aFr.length; x++) {
            var aPs = aFr[x].parentStory.contents;
            // OLD METHOD Doesn't always work
            //if (aPs.search(/©/gi) > -1) var foundYou = true; //alert((y + 1) + " - " + foundYou)
            //search(cRSym) returns correct results
            if (aPs.search(cRSym) > -1) var foundYou = true; //alert((y + 1) + " - " + foundYou)
        }
    }
    //Look through Master Page Text Frames
    for (var z = 0; z < aMF.length; z++) {
        if (aMF[z].constructor.name != "TextFrame") continue;
        var mPs = aMF[z].parentStory.contents;
        // OLD METHOD Doesn't always work        
        //if (mPs.search(/©/gi) > -1) var foundYou = true; //alert((y + 1) + "- M - " + foundYou);
        //search(cRSym) returns correct results
        if (mPs.search(cRSym) > -1) var foundYou = true; //alert((y + 1) + "- M - " + foundYou) 
    }
}
if (foundYou == false) lgCnt.push(y + 1);
    }
return lgCnt;
}



rob day
Community Expert
March 25, 2023

Hi @wckdtall , Rather than trying to get all of the document’s story contents, and using the string search method, it’s easier to use the InDesign text or grep search. This would get all of the instances of copyright characters in the document as an array:

 

 

 

 

 

//get an array of copyright characters
var crArray = getTextSearch("^2")
alert("There are " + crArray.length + " copyright symbols in this document")

/**
* Gets results of a text search as an array of text objects
* @ param text to search for 
* @ return result array 
*/
function getTextSearch(fp){
    app.findTextPreferences = app.changeTextPreferences = app.findChangeTextOptions = null;
    app.findChangeTextOptions.properties = { 
        includeHiddenLayers:true, 
        includeLockedLayersForFind:true, 
        includeLockedStoriesForFind:true, 
        includeMasterPages:true} 
    app.findTextPreferences.findWhat = fp;
    return app.activeDocument.findText()
}

 

 

 

 

 

wckdtall
wckdtallAuthor
Inspiring
March 25, 2023

Thanks! @rob day The script I wrote is actually checking to see what pages the copyright symbol is missing from, so I have to cycle through all the pages I believe, because I'm also looking at the Master Page instances on each page. Alternatively I could see what pages a master is applied to, but that wouldn't account for overrides.

I do appreciate this script though, gives me a bit more insight on how to loop through some search and replace.

rob day
Community Expert
March 25, 2023

In case your problem is with the string search, you could still use the built in text search. Does this work?

 

 

var pgs = app.documents[0].pages.everyItem().getElements();
var c, api, mpi;
for (var i = 0; i < pgs.length; i++){
    c = 0;
    mpi = pgs[i].masterPageItems;
    for (var j = 0; j < mpi.length; j++){
        if (mpi[j].constructor.name == "TextFrame") {
            if (getTextSearch("^2", mpi[j].parentStory).length > 0) {
                c++
            } 
        }
    }; 
    api = pgs[i].allPageItems;
    for (var k = 0; k < api.length; k++){
        if (api[k].constructor.name == "TextFrame") {
            if (getTextSearch("^2", api[k].parentStory).length > 0) {
                c++
            } 
        }
    };   
    if (c == 0) {
        alert("Page " + pgs[i].name + " has "+ c + " copyright symbols")  
    }   
};   


/**
* Gets results of a text search as an array of text objects
* @ param text to search for 
* @ param story to search 
* @ return search resultsarray 
*/
function getTextSearch(fp, s){
    app.findTextPreferences = app.changeTextPreferences = app.findChangeTextOptions = null;
    app.findChangeTextOptions.properties = { 
        includeHiddenLayers:true, 
        includeLockedLayersForFind:true, 
        includeLockedStoriesForFind:true, 
        includeMasterPages:true} 
    app.findTextPreferences.findWhat = fp;
    return s.findText()
}