Skip to main content
Trevor:
Braniac
August 2, 2011
Answered

Returning a conditional text value.

  • August 2, 2011
  • 1 reply
  • 9766 views

Hi,

Can somebody tell me how to get the conditional text name (if there is) of the insertion point.

Something like.

CondintionalTextName=insertionpoint.conditional.name

but written in Java Script instead of Gibberish!

This topic has been closed for replies.
Correct answer Marc Autret

Hi Trevor,

Probably sth like:

ConditionalTextName=insertionpoint.appliedConditions[0].name;

Note that appliedConditions is an array, because multiple conditions can be applied to the same text.

EDIT—To avoid errors with non-conditional text:

var conds = myText.appliedConditions;

var condName = conds.length ? conds[0].name : '[No Condition]';

@+

Marc

1 reply

Marc Autret
Marc AutretCorrect answer
Braniac
August 3, 2011

Hi Trevor,

Probably sth like:

ConditionalTextName=insertionpoint.appliedConditions[0].name;

Note that appliedConditions is an array, because multiple conditions can be applied to the same text.

EDIT—To avoid errors with non-conditional text:

var conds = myText.appliedConditions;

var condName = conds.length ? conds[0].name : '[No Condition]';

@+

Marc

Trevor:
Trevor:Author
Braniac
August 22, 2011

Hi Marc,

Thanks for your answer, sorry for the long delay in my response .  I had a couple of things to iron out (like how to return all the conditions for all the selected texts and other simple issues that I'm too embarrassed to write) and I have been exceptinaly busy with other things.

This is the code that I came up with based on your answer.

try {
var cl=app.selection[0];
var w = new Window ("dialog","");
array = [];
for (var j = 0; cl.length>j; j++){
    var conds=app.selection[0].characters.appliedConditions;
    var curletter=app.selection[0].characters.contents;
   
for (var i = 0; conds.length > (i); i++) {

array.push ("Letter "+(j+1)+" \""+curletter+"\""+"\tCondition "+i+" name is \t"+conds.name+" ");}
if (conds.length==0) {
var condName = conds.length ? conds[0].name : '[No Condition]';
array.push ("Letter "+(j+1)+" \""+curletter+"\""+"\tCondition "+i+" name is \t"+condName)
}
}
if (j==0){var i=0;}
array.push ("\r\tSelected text \""+app.selection[0].characters.itemByRange(0,-1).contents+"\"");
array.push ("\tIn total "+j+ " characters");
alert_scroll ("Conditional Texts Names of Selectect Characters", array,);
function alert_scroll (title, input) // string, string/array
{
// if input is an array, convert it to a string
if (input instanceof Array)
input = input.join ("\r");
var w = new Window ("dialog", title);
var list = w.add ("edittext", undefined, input, {multiline: true, scrolling: true});
// the list should not be bigger than the maximum possible height of the window
list.maximumSize.height = w.maximumSize.height - 300;
list.minimumSize.width = 450;
w.add ("button", undefined, "Close", {name: "ok"});
w.show ();   
}}
catch (cl)
{
    alert("First Select Text!");
}

It produces the following result:

I would like to know two more things about conditionals texts.


1) I would like to add a line at the bottom stating how many DIFFERENT conditions are used throughout all the selected texts. In the above example including [no condition] a total of 8 conditions are used 1) AppleScript 2) blue 3) Green 4) Nikud 5) Red 6) Violet 7) Yellow 8) No condition


2) To apply the "Red" condition to the selected text I would use

app.selection[0].applyConditions ( app.activeDocument.conditions.item("Red", true), true);

I can't figure out how to make the selection have no conditions

app.selection[0].applyConditions ( app.activeDocument.conditions.item("[Unconditional]", true), true); Does not work.


Any Ideas?

Inspiring
June 13, 2012

Very good Tobias

One a page document with a lot of conditions applied your optimized code took about 5.5 seconds as apposed to the about 17.5 that my one took!!

Note

Still not bulletproof

The line in the optimized code

    for ( var m = 0; m < allRanges.length; m++ ) {

need to be changed to

          for ( var m =  allRanges.length-1 ; m >=0; m-- ) {

without the change does not work properly

Please edit your code post above and I will delete this note on changing the code.


tobias.wantzen wrote:

  • You can extend the script's functionality by adding a drop down dialog window so the user can choose the condition by name.

The purpose ot the script is to be able to delete a condition as a function within another script without a user interface.

For a user to perform the task he just goes to the conditional text panel and unticks the Red condition Easy and Instant!

That's why I thought to use menu action, but it doesn't seem possible I would like to be proven wrong on this point !!

My Idea to use a search was silly as the condition can be taken off the whole selection.

Your right about the feature request it is really dumb that one can't search for a particlar condition that is part of an array of conditions.

Trevor


5.5 to 17.5 sec.? Nice improvement.

Corrected Script:

#target InDesign

app.scriptPreferences.enableRedraw = 0;

app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Remove Red Condition");

function main() {

    var theCondition = 'Red';

    var theSelection = app.selection[0];

 

    var allRanges = [];

    var theRange = '';

    var allRanges = theSelection.textStyleRanges;

    for ( var m = allRanges.length-1; m >= 0; m-- ) {

        theRange = allRanges;

        if ( theRange.appliedConditions.length > 0 ) {

            for ( var n = theRange.length-1; n >= 0; n-- ) { 

                removeCondition(theRange, theCondition);  // remove condition

            }

        }

    }

}

  

function removeCondition ( selectedObject, condName ) {

    var hasCondition = false;

    var condObject = selectedObject.appliedConditions;

    var newCondObject = [];

    for ( var i = 0; i < condObject.length; i++ ) {     

        (condObject.name != condName) ? newCondObject.push(condObject) : hasCondition = true;

    }

    if ( hasCondition ) selectedObject.applyConditions( newCondObject, true );  // condtions are only changed if there is a "Red" condtion

    return null;

}

(I can only edit my post so long as no one has answered to it ...)

I didn't get the point of your menu action suggestion: What do you want to achieve with it? Can you verbose your idea?

Tobias