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;
}
2 Correct answers
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.
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
...
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()
}
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.
Copy link to clipboard
Copied
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()
}
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?
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.
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
Works fine for me:
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]);
Copy link to clipboard
Copied
Didn’t think it would work, but I worth a shot!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.

