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

.search keeps failing in this script

Enthusiast ,
Mar 25, 2023 Mar 25, 2023

Copy link to clipboard

Copied

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;
    }

 

 

TOPICS
Scripting

Views

305

Translate

Translate

Report

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 , Mar 25, 2023 Mar 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.

Votes

Translate

Translate
Community Expert ,
Mar 25, 2023 Mar 25, 2023

Copy link to clipboard

Copied

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()
}

 

 

 

 

 

Votes

Translate

Translate

Report

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 ,
Mar 25, 2023 Mar 25, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Mar 25, 2023 Mar 25, 2023

Copy link to clipboard

Copied

LATEST

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()
}

 

 

Votes

Translate

Translate

Report

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 ,
Mar 25, 2023 Mar 25, 2023

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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 ,
Mar 25, 2023 Mar 25, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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