Skip to main content
Community Expert
January 27, 2025
Answered

ExtendScript: How to remove conditions and how to apply them

  • January 27, 2025
  • 1 reply
  • 324 views

Hi,

I do not have much time to rewrite my FrameScript scripts in ExtendScripts, but I have to. I see that FrameScript will not be supported forever.

In one of my scripts I have to remove conditions from a paragraph. It does not matter which conditions are there, and it does not matter, if not all of the paragraph has a condition.
In Rick's FrameAutomation blog he suggests this to remove all conditions from the text:

http://frameautomation.com/removing-conditions-from-text-and-table-rows-part-2/

#target framemaker

var doc = app.ActiveDoc;

var textRange = doc.TextSelection;
var prop = doc.GetTextPropVal (textRange.beg, Constants.FP_InCond);

// Remove all conditions from the text.
prop.propVal.osval.length = 0;

// Apply the updated list back to the text range.
doc.SetTextPropVal (textRange, prop);

 

When I create a short dummy file and apply a condition to the second to last paragraph, select this paragraph and copy this snippet into ESTK and run it, I get the following:
• The paragraph still has its condition.
• Now even the empty paragraph _after_ the selected paragraph has the condition!
What is correct? How can I remove all conditions from a paragraph?

Thank you very much for your help!

Best regards, Winfried

    Correct answer Winfried Reng

    @Klaus Göbel also helped me with my functions to apply a conditions. The core is from @Russ Ward in a post on this forum. Finally this works as well! Thank you very much!

    function ApplyConditionToTbl (foDoc, foTbl, fsCondition)
    {
        var loCondFmt, loRow, lProps, lPropIndex;
    
        // Get the condition format object.
        loCondFmt = foDoc.GetNamedCondFmt (fsCondition);
        if (!loCondFmt.ObjectValid()) return;
        
        loRow = foTbl.FirstRowInTbl;
        while (loRow.ObjectValid()) {
            lProps = loRow.GetProps ();
            lPropIndex = GetPropIndex (lProps, Constants.FP_InCond);
            lProps [lPropIndex].propVal.osval [0] = loCondFmt; 
            lProps [lPropIndex].propVal.isval [0] = loCondFmt.id;
            loRow.SetProps (lProps);
            loRow = loRow.NextRowInTbl;
        }
    } // --- end ApplyConditionToTbl
    
    
    function ApplyConditionToTblAnchorPgf (foDoc, foTbl, fsCondition)
    {
        var loCondFmt, loTLoc, loTRange, loPgf, lProps, lPropIndex;
        
        // Get the condition format object.
        loCondFmt = foDoc.GetNamedCondFmt (fsCondition);
        if (!loCondFmt.ObjectValid()) return;
        
        // Get the text location of the table anchor and then the text range of the anchor paragraph.
        loTLoc = foTbl.TextLoc;
        loPgf = loTLoc.obj;
        loTRange = new TextRange ();
        loTRange.beg.obj = loPgf;
        loTRange.beg.offset = 0;
        loTRange.end.obj = loPgf;
        loTRange.end.offset = Constants.FV_OBJ_END_OFFSET;
        // Populate a PropVal array for condition formats by asking for the current value at the beginning of the anchor.
        lProps = foDoc.GetTextPropVal (loTRange.beg, Constants.FP_InCond); 
        // Reset the PropVal to the condition we want.
        lProps.propVal.osval [0] = loCondFmt;  
        lProps.propVal.isval [0] = loCondFmt.id;  
        // Set the revised properties to the full text range.
        foDoc.SetTextPropVal (loTRange, lProps);  
    } // --- end ApplyConditionToTblAnchorPgf
    

     

    1 reply

    Community Expert
    January 27, 2025

    @Klaus Göbel , thank you very much for your help!

    Klaus told me that in the osval array there are the values of the conditions. However, there is also an isval array with the IDs of the conditions. This must be emptied as well.

    Therefore the script has to be changed to:

    #target framemaker

    var doc = app.ActiveDoc;

    var textRange = doc.TextSelection;
    var prop = doc.GetTextPropVal (textRange.beg, Constants.FP_InCond);

    // Remove all conditions from the text.

    prop.propVal.osval.length = 0;

    prop.propVal.isval.length = 0;

     

    // Apply the updated list back to the text range.
    doc.SetTextPropVal (textRange, prop);

     

    Best regards, Winfried

    Winfried RengCommunity ExpertAuthorCorrect answer
    Community Expert
    February 3, 2025

    @Klaus Göbel also helped me with my functions to apply a conditions. The core is from @Russ Ward in a post on this forum. Finally this works as well! Thank you very much!

    function ApplyConditionToTbl (foDoc, foTbl, fsCondition)
    {
        var loCondFmt, loRow, lProps, lPropIndex;
    
        // Get the condition format object.
        loCondFmt = foDoc.GetNamedCondFmt (fsCondition);
        if (!loCondFmt.ObjectValid()) return;
        
        loRow = foTbl.FirstRowInTbl;
        while (loRow.ObjectValid()) {
            lProps = loRow.GetProps ();
            lPropIndex = GetPropIndex (lProps, Constants.FP_InCond);
            lProps [lPropIndex].propVal.osval [0] = loCondFmt; 
            lProps [lPropIndex].propVal.isval [0] = loCondFmt.id;
            loRow.SetProps (lProps);
            loRow = loRow.NextRowInTbl;
        }
    } // --- end ApplyConditionToTbl
    
    
    function ApplyConditionToTblAnchorPgf (foDoc, foTbl, fsCondition)
    {
        var loCondFmt, loTLoc, loTRange, loPgf, lProps, lPropIndex;
        
        // Get the condition format object.
        loCondFmt = foDoc.GetNamedCondFmt (fsCondition);
        if (!loCondFmt.ObjectValid()) return;
        
        // Get the text location of the table anchor and then the text range of the anchor paragraph.
        loTLoc = foTbl.TextLoc;
        loPgf = loTLoc.obj;
        loTRange = new TextRange ();
        loTRange.beg.obj = loPgf;
        loTRange.beg.offset = 0;
        loTRange.end.obj = loPgf;
        loTRange.end.offset = Constants.FV_OBJ_END_OFFSET;
        // Populate a PropVal array for condition formats by asking for the current value at the beginning of the anchor.
        lProps = foDoc.GetTextPropVal (loTRange.beg, Constants.FP_InCond); 
        // Reset the PropVal to the condition we want.
        lProps.propVal.osval [0] = loCondFmt;  
        lProps.propVal.isval [0] = loCondFmt.id;  
        // Set the revised properties to the full text range.
        foDoc.SetTextPropVal (loTRange, lProps);  
    } // --- end ApplyConditionToTblAnchorPgf