Skip to main content
Participating Frequently
February 10, 2023
Answered

Script for show or hide conditional text in InDesign

  • February 10, 2023
  • 3 replies
  • 2357 views

Hello everyone

I have a document that use a conditional text, and i need check how the text stay with or without the conditional, in many pages, today i do this showing and hiding the eye in conditional text panel, so, i am trying write a code for atutomate this task.

 

I am yet in begin

var delay = 50;
var stopCheck = false;

while (stopCheck == false){
    
    if (app.activeDocument.conditions.item("condition_name").visible == true) {
        
        app.activeDocument.conditions.item("condition_name").visible = false;
         $.sleep(delay);
         
        } else {
            app.activeDocument.conditions.item("condition_name").visible = true;
             $.sleep(delay);
             
             }
        
    }

 

the code worked, I know this because i see the eye in the panel changing, but in the document the text do not hide or show, I understand that InDesign freeze the view, while the script runs.

 

I try too use the invoke of menuActions, but the result is same

 

app.menuActions.itemByID(133136).invoke() // Hide
app.menuActions.itemByID(133135).invoke() // Show

 

somebody knows how i can resolve, or the reason for this

grateful for help

This topic has been closed for replies.
Correct answer m1b

Hi @cleitongoncalves and @brian_p_dts I actually couldn't get anything to work. In Illustrator we can do "app.redraw()" but Indesign doesn't seem to have that.

 

The best I could do is to set up an IdleTask, but the problem with that is that is the timing is unpredictable and sometimes hangs for long periods when (presumably) another task takes precedence. I hope an expert can tell us how to make the event fire with predictable timing.

- Mark

 

 

 

 

/**
 * Toggle conditional text in idle task.
 * The aim is to get Indesign to redraw,
 * like in Illustrator you have "app.redraw()".
 * Note that due to this using idle tasks, the
 * timing is unpredictable, and often *terrible*.
 * Also none of the following worked for me:
 *   app.activeDocument.recompose();
 *   app.menuActions.itemByName("$ID/Recompose all stories").invoke();
 *   app.scriptMenuActions.itemByID(318).invoke(); // force redraw menu
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/script-for-show-or-hide-conditional-text-in-indesign/m-p/13570934
 */

//@targetengine 'toggler'

// settings
var conditionName = 'condition_name',
    maxCount = 10,
    delay = 500;

// internal vars
var myTaskName = 'myIdleTask';

if (app.idleTasks.itemByName(myTaskName).isValid)
    stopToggler();
else
    startToggler();


function startToggler() {

    alert('Starting ' + maxCount + ' toggles.');
    app.idleTasks.add({ name: myTaskName, sleep: delay })
        .addEventListener(IdleEvent.ON_IDLE, (function (counter) {
            return function () {
                if (--counter < 0)
                    stopToggler();
                else
                    app.activeDocument.conditions.item(conditionName).visible = !app.activeDocument.conditions.item(conditionName).visible;
            }
        })(maxCount));

};

function stopToggler() {
    app.idleTasks.itemByName("myIdleTask").remove();
    alert('Stopped');
};

 

 

 

 

3 replies

Participating Frequently
February 12, 2023

Hey folks

first I would like to thank you for your contribution, the tips opened up new possibilities here, I bring the results of the tests I did

Before explaining, better use of conditional text here:

I have been using it to export a pdf for students and teachers, the answers for the teacher are with the conditional text, and i export the pdf for the student with the conditional text hidden.

These responses are in their own text boxes and anchored to the main text.

If there is only text on the page, the .recompose() approach works perfectly, but in some cases the response is a vector, a circle to mark an alternative, which I anchor to the text, and apply the condition to just the anchor character, in these cases recompose() interferes with the visualization of the main text.

 

 

Here if I unanchor the circle, the script does not interfere with the visualization of the main text, but precisely because it is no longer anchored, I lose the function of the conditional.

 

I'm still doing tests here.

@m1b I was curious about the approach with idleTasks, but I can't get it to work here, the message "The requested action could not be completed because the object no longer exists". appears when run, I could not understand. I am use the ID 18.1

m1b
Community Expert
Community Expert
February 12, 2023

Thanks for the extra info. As for the idleTasks, can you share a small demo file that I can test with? Maybe it's not finding the condition or something. Very hard to know without reproducing the error. - Mark

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
February 10, 2023

Hi @cleitongoncalves and @brian_p_dts I actually couldn't get anything to work. In Illustrator we can do "app.redraw()" but Indesign doesn't seem to have that.

 

The best I could do is to set up an IdleTask, but the problem with that is that is the timing is unpredictable and sometimes hangs for long periods when (presumably) another task takes precedence. I hope an expert can tell us how to make the event fire with predictable timing.

- Mark

 

 

 

 

/**
 * Toggle conditional text in idle task.
 * The aim is to get Indesign to redraw,
 * like in Illustrator you have "app.redraw()".
 * Note that due to this using idle tasks, the
 * timing is unpredictable, and often *terrible*.
 * Also none of the following worked for me:
 *   app.activeDocument.recompose();
 *   app.menuActions.itemByName("$ID/Recompose all stories").invoke();
 *   app.scriptMenuActions.itemByID(318).invoke(); // force redraw menu
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/script-for-show-or-hide-conditional-text-in-indesign/m-p/13570934
 */

//@targetengine 'toggler'

// settings
var conditionName = 'condition_name',
    maxCount = 10,
    delay = 500;

// internal vars
var myTaskName = 'myIdleTask';

if (app.idleTasks.itemByName(myTaskName).isValid)
    stopToggler();
else
    startToggler();


function startToggler() {

    alert('Starting ' + maxCount + ' toggles.');
    app.idleTasks.add({ name: myTaskName, sleep: delay })
        .addEventListener(IdleEvent.ON_IDLE, (function (counter) {
            return function () {
                if (--counter < 0)
                    stopToggler();
                else
                    app.activeDocument.conditions.item(conditionName).visible = !app.activeDocument.conditions.item(conditionName).visible;
            }
        })(maxCount));

};

function stopToggler() {
    app.idleTasks.itemByName("myIdleTask").remove();
    alert('Stopped');
};

 

 

 

 

brian_p_dts
Community Expert
Community Expert
February 10, 2023

@m1b We do have recompose. I'm actually working on a doc/script for conditional text right now, and this works fine. I see the text disappearring and reappearing when running it. 

var main = function()  {
    var doc = app.activeDocument;
    var cons = doc.conditions;
    var conName = "RPS_Staff";
    var con = cons.itemByName(conName);
    if (con.visible) {
        con.visible = false;
        doc.recompose();
    } 
    else {
        con.visible = true;
        doc.recompose();
    }
}

main();

 

m1b
Community Expert
Community Expert
February 10, 2023

Thanks @brian_p_dts, but in my testing (ID 18.1 MacOS 13.2) recompose doesn't work. I believe this is because recompose has nothing to do with redrawing, but serves to re-apply the paragraph composer(s) to the text object(s), which I'd guess happens automatically when the app redraws.

 

Your script works perfectly because Indesign does redraw when the script ends. And when I remove the two recompose calls from your script, it still works fine, because they don't do anything there (but in a longer script you may still require them for other reasons, of course.)

 

The difference between @cleitongoncalves and your requirements is that @cleitongoncalves needs the redraw to happen while the script is still running.

 

(I might be wrong—I'm just learning this stuff!)

- Mark

brian_p_dts
Community Expert
Community Expert
February 10, 2023

Try adding app.activeDocument.recompose() at the end. Conditional text can be laggy/buggy