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

.search keeps failing in this script

Enthusiast ,
Mar 25, 2023 Mar 25, 2023

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
621
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 2 Correct answers

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.

Translate
Enthusiast , Feb 11, 2025 Feb 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
...
Translate
Community Expert ,
Mar 25, 2023 Mar 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()
}

 

 

 

 

 

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

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

 

 

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

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
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.

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
Enthusiast ,
Feb 11, 2025 Feb 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;
}



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 ,
Feb 11, 2025 Feb 11, 2025

Works fine for me:

 

RobertatIDTasker_0-1739319762115.pngexpand image

 

    app.findTextPreferences = app.changeTextPreferences = app.findChangeTextOptions = null;
    app.findChangeTextOptions.properties = { 
        includeHiddenLayers:true, 
        includeLockedLayersForFind:true, 
        includeLockedStoriesForFind:true, 
        includeMasterPages:true} 
    app.findTextPreferences.findWhat = "^2";
    app.select(app.findText()[0]);
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
Enthusiast ,
Feb 11, 2025 Feb 11, 2025
^2 is what InDesign’s find/change subs in as a “symbol” for copyright.
Didn’t think it would work, but I worth a shot!
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
Enthusiast ,
Feb 11, 2025 Feb 11, 2025

Yep, there it works. Ultimately not what my script is doing. I needed it to check to make sure there's a copyright symbol on every page of the document, and return a count. I had already gone down the road of looping through the pages, the fromCharCode, was the piece I needed to resolve a bug.

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 ,
Feb 11, 2025 Feb 11, 2025

@wckdtall

 

Just in case you're not aware - you should never work on the contents of the Story as a string - or a whole Paragraph, etc. - if there are TextStyleRanges.

 

I can explain it in detail - unless you know what I'm talking about. 

 

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
Enthusiast ,
Feb 11, 2025 Feb 11, 2025
Thanks!

I think I follow, so that the styles aren’t accidentally nuked? And
potentially scope changes? I’m not usually manipulating text or styles with
code.
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 ,
Feb 12, 2025 Feb 12, 2025
LATEST
quote
Thanks!I think I follow, so that the styles aren’t accidentally nuked? Andpotentially scope changes? I’m not usually manipulating text or styles withcode.

By wckdtall

 

Yes. Exactly.

 

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