Skip to main content
K.Daube
Community Expert
Community Expert
Jul 27, 2022 2:29 PM UTC
Answered

Replace condition format

Hi experts,

I want to replace a condition format A by format B.
The current TR contains format A (but of course may also contain other condition formats, such as C).
The following code works only partially:

 

ApplyCondFmt = function (oDoc, oTR, sFormat) { /* =========================
Arguments oDoc    Document to be handled
          oTR     Text range to receive the new format
          sFormat Name of Conditon format (was selected from catalogue)
Reference http://frameautomation.com/category/extendscript/page/3/
*/
var oCondFmt, oProps, iPropIndex;

oCondFmt = oDoc.GetNamedCondFmt(sFormat);
if(!oCondFmt.ObjectValid()) return; // emergency exit

oProps = oDoc.GetTextPropVal (oTR.beg, Constants.FP_InCond);
oProps.propVal.osval[0] = oCondFmt; // new format
oDoc.SetTextPropVal (oTR, oProps);  // seems to do nothing
} // --- end ApplyCondFmt ------------------------------------------------

 

And of course I have no idea how to replace the format if there is more than one entry in osval ...

This topic is closed to new replies. Start a new post to keep the conversation going.
K.Daube
K.DaubeCorrect answer
Community Expert
Aug 1, 2022 10:39 AM UTC

Comparing RemoveCondFmtFromText with Rick's removeCondFmtFromPgf I found that also oProps.propVal.isval must be handled:

 

KLD_Z.RemoveConditionsFromText = function (oDoc, oTR, sCondFmt) { // ============
/*            Remove selected condition format
Arguments     oDoc            Current document
              oTR             Text range with the condition
              sCondFmt        Condition format (name) to be removed
Returns       Modified Text range
Reference     https://community.adobe.com/t5/framemaker-discussions/replace-condition-format/td-p/13096681
History       2022-08-01
*/
var j, n, oProps;
  oProps = oDoc.GetTextPropVal (oTR.beg, Constants.FP_InCond); // conditions
  n = oProps.propVal.osval.length;
  if (n == 0) {
      return;   // No conditions are applied to the text range
  }
  for (j = 0; j < n; j += 1) { // Loop through the applied condition names
    if (oProps.propVal.osval[j].Name == sCondFmt) { // To be removed?
      Array.prototype.splice.call (oProps.propVal.osval, j, 1);
      Array.prototype.splice.call (oProps.propVal.isval, j, 1);
    }
  }
  if (oProps.propVal.osval.length != n) { // oProps have been changed
    oDoc.SetTextPropVal (oTR, oProps);
  }
} //--- end RemoveConditionsFromText ----------------------------

 

Hooray!

5 replies

frameexpert
Community Expert
Community Expert
Jul 27, 2022 2:58 PM UTC

I don't have time to test exactly what you are doing, but some of this code may help.

function removeCondFmtFromRow (row, condFmt) {
    
    var condFmts, i;
    
    // Get a list of the conditions applied to the row.
    condFmts = row.InCond;

    // Loop through the condition format names that are applied to the row.
    for (i = 0; i < condFmts.length; i += 1) {
        // See if the format should be removed.
        if (condFmts[i].Name === condFmt.Name) {
            // Remove it from the list.
            Array.prototype.splice.call (condFmts, i, 1);
        }
    }

    if (condFmts.length !== row.InCond.length) {
        // Conditions were removed; apply the updated list back to the row.
        row.InCond = condFmts;
    }
}

function removeCondFmtFromPgf (textList, pgf, doc, condFmt) {  
  
    var end = Constants.FV_OBJ_END_OFFSET, begin = 0, textRange, prop, i;
    
    // Loop through the text property changes in the paragraph.
    for (i = textList.length - 1; i >= 0; i -= 1) {  
       // Loop through them and see if a Condition format has been changed.
       if (Constants.FTF_CONDITIONTAG & textList[i].idata) {  
           begin = textList[i].offset;  
            if (begin !== end) {  
                // Make a text range for the unique condition format combination.
                textRange = new TextRange (new TextLoc (pgf, begin), new TextLoc (pgf, end));  
                prop = doc.GetTextPropVal (textRange.beg, Constants.FP_InCond);
                // Make sure one or more conditions formats are applied.
                if (prop.propVal.osval.length > 0) {
                    prop = removeCondFmtFromList (prop, condFmt);
                    // Apply the updated list back to the text range.
                    doc.SetTextPropVal (textRange, prop);
                }
            }  
            end = begin;  
        }  
    } 
    if (end !== begin) {
        // A condition may be applied to the entire paragraph.
        textRange = new TextRange (new TextLoc (pgf, begin), new TextLoc (pgf, end));  
        prop = doc.GetTextPropVal (textRange.beg, Constants.FP_InCond);  
        if (prop.propVal.osval.length > 0) {
            prop = removeCondFmtFromList (prop, condFmt);
            // Apply the updated list back to the text range.
            doc.SetTextPropVal (textRange, prop);
        }
    }
}

function removeCondFmtFromList (prop, condFmt) {
    
    var count, i;
    
    count = prop.propVal.osval.length;
    for (i = 0; i < count; i += 1) {
        if (prop.propVal.osval[i].Name === condFmt.Name) {
            Array.prototype.splice.call (prop.propVal.osval, i, 1);
            Array.prototype.splice.call (prop.propVal.isval, i, 1);
            break;
        }
    }

    return prop;
}
Translate
www.frameexpert.com
K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
Jul 28, 2022 3:22 PM UTC

Rick, thank You very much for your immediate response.

I have however a problem with the following script:

 

KLD_F.RemoveConditionsFromText = function (oDoc, oTR, sCondFmt) { // ======
var j, aoCondFmts, oCondFmt, oProps;

  oProps = oDoc.GetTextPropVal (oTR.beg, Constants.FP_InCond); 
	aoCondFmts = oProps.propVal.osval;
  if (aoCondFmts.length === 0) {
      return; 
  }
  for (j = 0; j < aoCondFmts.length; j += 1) { 
    if (aoCondFmts[j].Name == sCondFmt) { 
      Array.prototype.splice.call (aoCondFmts, j, 1); // <<<<<<<
    }
  }
  if (aoCondFmts.length !== oProps.propVal.osval.length) {
    oProps.propVal.osval = aoCondFmts; 
    oDoc.SetTextPropVal (oTR, oProps);
  }
} //--- end RemoveConditionsFromText ----------------------------------

 

As soon as the line indicated (<<<<) is executed, also  oProps.propVal.osval is updated. Hence the final clause is always false and hence oDoc.SetTextPropVal (oTR, oProps); is not executed. Thus the condition is not removed.

The rason might be, that assignent of an object does not create a new object, but only new pointer. But how to cope with this?

Translate
K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
Jul 29, 2022 10:52 AM UTC

Strange for me is that the counterpart for deleting conditions in Row works as intended:

KLD_F.RemoveCondFmtFromRow = function (oDoc, oRow, sCondFmt) { // ===============
var j, aoCondFmts;
  aoCondFmts = oRow.InCond;       // Get list of the conditions applied to the row
  if (aoCondFmts.length === 0) {
    return;                       // No conditions are applied to the row
  }
  // Loop through the format names in the row.
  for (j = 0; j < aoCondFmts.length; j += 1) {    
    if (aoCondFmts[j].Name == sCondFmt) { // Should format  be removed?
      Array.prototype.splice.call (aoCondFmts, j, 1); // Remove it from the list
    }
  }
  if (aoCondFmts.length !== oRow.InCond.length) { 
    oRow.InCond = aoCondFmts; // Conditions were removed; apply the updated list
  }
} //--- end RemoveCondFmtFromRow ------------------------------------

So I definitely need a detective with the magnifying glass...

Translate