Skip to main content
Participant
January 7, 2025
Question

Script for Retrieving Hidden Conditional Text

  • January 7, 2025
  • 4 replies
  • 750 views

Is there a way to retrieve hidden conditional text in a script?

 

I have tried the following code which works if the text is not hidden.  It does not return any hits if the conditional text is hidden.

 

function getTextByCondition(doc, conditionName) {
    var results = [];
    var condition = doc.conditions.itemByName(conditionName);

    app.findChangeTextOptions.includeFootnotes = true;
    app.findChangeTextOptions.includeHiddenLayers = true;
    app.findChangeTextOptions.includeLockedLayersForFind = true;
    app.findChangeTextOptions.includeLockedStoriesForFind = true;
    app.findChangeTextOptions.includeMasterPages = true;

    app.findTextPreferences = app.changeTextPreferences = null;
    app.findTextPreferences.appliedConditions = [condition];
    // app.findTextPreferences.hiddenText = true;

    var foundItems = doc.findText();

    for (var i = 0; i < foundItems.length; i++) {
        var foundItem = foundItems[i];
        var text = foundItem.contents;
		var pageNumber = null
		try {
        	pageNumber = foundItem.parentTextFrames[0].parentPage.name;
		} catch (error) {
			// do nothing
		}
        results.push({text: trim(text), page: pageNumber});
    }

    app.findTextPreferences = app.changeTextPreferences = null;
    return results;
}

 

4 replies

m1b
Community Expert
Community Expert
January 7, 2025

Hi @jrsupplee, if you care about the pagination, then your script will need to show/hide conditions as necessary (I would suggest a good use of Condition Sets?). So your script will make a condition set active, perform the find, collect the contents and page numbers, then make another condition set active, perform the find, collect the contents and page numbers, etc. Finding the hiddenTexts won't give you correct pagination just as you point out that showing all conditions will show incorrect pagination.

- Mark

Robert at ID-Tasker
Legend
January 7, 2025

@m1b

 

Have you read previous replies?? 

 

Beside - showing each condition - even separately - WILL make a shift of the text so you won't be able to get correct page numbers. 

 

You can't show / hide each occurrence of the same condition individually. 

 

m1b
Community Expert
Community Expert
January 7, 2025

Yes I did, but maybe I misunderstood. Did OP want to show/hide each occurrence of the condition individually? I didn't understand that, sorry. I guess I don't understand why anyone would want the page number of hidden text, given that it won't be correct when its condition is made visible.

FRIdNGE
January 7, 2025

Based on "HiddenTexts"! …

 

The treatment is here on the masked condition called "_temp1".

 

The first screenshot shows the text with this condition applied (in orange):

 

 

The second screenshot shows how the Script works when this condition is masked:

 

 

Script Code:

 

/*
    _FRIdNGE-0777_ConditionalHiddenTextsProperties.jsx
    Script written by FRIdNGE, Michel Allio [07/01/25]
*/

var myDoc = app.activeDocument;
// Condition Name To Be Defined:
var myCondition = "_temp1";

var myHiddenTexts = myDoc.stories.everyItem().hiddenTexts.everyItem().getElements();
var H = myHiddenTexts.length,  h;

var myResults = [];

for ( h = 0; h < H; h++ ) {
    var myHiddenText = myHiddenTexts[h];
    if ( myHiddenText.texts[0].appliedConditions[0].name === myCondition ) myResults.push( myHiddenText.texts[0].contents + "-- page " + myHiddenText.storyOffset.parentTextFrames[0].parentPage.name + " --" );
}

alert( "\"" + myCondition + "\"" + " Hidden Texts Properties:\r" + myResults.join("\r") )

 

… To be adapted for your own use!  😉

 

(^/)  The Jedi

jrsuppleeAuthor
Participant
January 10, 2025

The following function is working to find hidden conditional text and return the page number.

 

Thanks for the help.

 

function getConditionalText(doc, conditionName) {
    var results = [];
    var condition = doc.conditions.itemByName(conditionName);
	var hiddenTexts = doc.stories.everyItem().hiddenTexts.everyItem().getElements();

	for (i = 0; i < hiddenTexts.length; i++) {
		var hiddenText = hiddenTexts[i];
		if (hiddenText.texts[0].appliedConditions[0].name === conditionName) {
		    results.push({
				text: hiddenText.texts[0].contents,
				page: hiddenText.storyOffset.parentTextFrames[0].parentPage.name,
				noteNumber: getFootnoteNumber(hiddenText.storyOffset)
			});
		}
	}
	return results;
}
Robert at ID-Tasker
Legend
January 7, 2025

@jrsupplee 

 

Get HIDDEN Texts first:

 

 

But then, you need to do a 2nd run on them - check which Condition(s) is(are) applied.

 

Screenshot shows raw text at the top with all conditional texts hidden.

 

Below you can see my ID-Tasker tool - this part is free - effect of first loading Hidden texts - then 2nd run that analyses those Hidden Texts and loads info about applied Conditions to them.

 

2nd, indented, copies of "HiddenText" should be renamed to "Conditional Text" - it's a quick test so I haven't done this yet.

 

CharIndex column is misleading - during 1st run, InDesign returns indexes of texts EXCLUDING hidden texts - so when I've done 2nd run - the 1st hidden text on the above list is in fact FIRST paragraph:

 

And there are no Page Names / Indexes - hidden textts don't have Parent TextFrames, Layers, etc.

 

There is one drawback - not sure if/how would you be able to store this information - but after showing Conditional texts in InDesign - when I try to access location of the Hidden Texts - loaded during 1st run, when conditions were hidden - InDesign return error that object no longer exists. Not a problem for my IDT - but it might be a problem for you.

 

Robert at ID-Tasker
Legend
January 7, 2025
quote

2nd, indented, copies of "HiddenText" should be renamed to "Conditional Text" - it's a quick test so I haven't done this yet.

 

And done:

 

 

Robert at ID-Tasker
Legend
January 7, 2025

And Conditions only:

 

 

FRIdNGE
January 7, 2025

In your function, just make the condition temporarily visible at the beginning and invisible again at the end:

 

if ( condition.visible === false ) {
condition.visible = true;
condition.label = "false";
}
……
if ( condition.label === "false" ) {
condition.visible = false;
condition.label = "";
}

 

(^/)  The Jedi

jrsuppleeAuthor
Participant
January 7, 2025

The problem with this solution is that making the conditional text visible could change the pagination 

FRIdNGE
January 7, 2025

When you want to get the "pageNumber", we can suppose it's the page of the first char found!

 

First, collect the "Text" (with the condition we call "X").

 

Create another simple function that adds a "non-joiner" (with no condition) at the beginning of the each text this the condition "X" and collect their page number..

 

Now, you have an array of the contents. Just include for each the page number of the non-joiner (new function).

 

After that, remove the non-joiner (simple regex).

 

(^/)