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

Script for Retrieving Hidden Conditional Text

New Here ,
Jan 07, 2025 Jan 07, 2025

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

 

TOPICS
Scripting
708
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
Guide ,
Jan 07, 2025 Jan 07, 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

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
New Here ,
Jan 07, 2025 Jan 07, 2025

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

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
Guide ,
Jan 07, 2025 Jan 07, 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).

 

(^/)

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
Mentor ,
Jan 07, 2025 Jan 07, 2025

Untested idea:

Wrap the condition changes and search into a function.

Remember the findings somewhere for later.

Execute the function with an app.doScript and appropriate undo mode.

As last step within the function, trigger the undo to revert the condition changes and pagination.

Outside, work thru your findings.

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
LEGEND ,
Jan 07, 2025 Jan 07, 2025

@jrsupplee 

 

Get HIDDEN Texts first:

 

RobertatIDTasker_0-1736262924667.png

 

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:

 

RobertatIDTasker_1-1736263369270.png

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.

 

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
LEGEND ,
Jan 07, 2025 Jan 07, 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:

 

RobertatIDTasker_0-1736264113513.png

 

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
LEGEND ,
Jan 07, 2025 Jan 07, 2025

And Conditions only:

 

RobertatIDTasker_0-1736264475587.png

 

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
Guide ,
Jan 07, 2025 Jan 07, 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):

 

Capture d’écran 2025-01-07 à 18.40.09.png

 

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

 

Capture d’écran 2025-01-07 à 18.40.34.png

 

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

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
New Here ,
Jan 10, 2025 Jan 10, 2025
LATEST

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;
}
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 ,
Jan 07, 2025 Jan 07, 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

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
LEGEND ,
Jan 07, 2025 Jan 07, 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. 

 

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 ,
Jan 07, 2025 Jan 07, 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.

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
LEGEND ,
Jan 07, 2025 Jan 07, 2025

@m1b

 

I think title is pretty clear? 

 

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
LEGEND ,
Jan 07, 2025 Jan 07, 2025

@m1b 

 

It might not be clear from my earlier screenshots - so here is a more clear example.

 

The whole text has exactly the same formatting - Minion Pro Regular, 12pt:

 

RobertatIDTasker_3-1736293648404.png

 

I've downloaded contents of the last Paragraph as separate characters - then loaded info about Hidden texts - for the whole Paragraph.

 

From the CharIndex column - you can see where each condition is anchored - marked with a green frame in the text. I should've rather selected "Character" with the index of 974 - 3 rows earlier.

 

And here is the "complete" text - with all hidden conditions visible:

 

RobertatIDTasker_4-1736293772007.png

 

So, for each piece of text, you can retrieve HiddenTexts collection - and from there, you can get its anchoring point - StoryOffset of the Hidden text - and then get a reference of the previous or next character to get ParentTextFrame.

 

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