Skip to main content
Inspiring
August 29, 2024
Answered

Extend Script - How can I access the prefix of a prefix rule for a certain element

  • August 29, 2024
  • 1 reply
  • 528 views

I need to publish structured documents to xml. Some of the Headings are created by prefix rules in the EDD. When I publish the document to xml, the headings are missing in the xml.

I am a scripting novice to be honest, I have no clue how to get the text out of the prefix rule, which depends on the context. In the FrameMaker scripting guide I found  the following objects which may or may not handle the prefixes
- Element.MatchingPrefixClause; of data type Objects (ID of the prefix clause that apply to the element)

- ElementDef.PrefixRules; of data type objects (IDs of the prefix format rules)

- FmtRuleClause.ElemPrefixSuffix: of data type string (The text of the string)
I am able to select the heading element, but how the hell do I get the content of the prefix from there?
Any hints appreciated!

This topic has been closed for replies.
Correct answer frameexpert

This is kind of a brute force method, but it looks like a prefix shows up in the list of the element's strings. If you know for sure that the element has a prefix, you can probably use something like this. Note that you may have to drop the delimiter between the prefix and the main element text from the end of the string.

 

#target framemaker

main ();

function main () {

    var doc, element, stringArray, prefix;
    
    doc = app.ActiveDoc;
    if (doc.ObjectValid () === 1) {
        element = doc.ElementSelection.beg.child;
        stringArray = (getAnArrayOfStrings (element, doc));
        prefix = stringArray[0];
        alert (prefix);
    }
}

function getAnArrayOfStrings (textObj, doc) {
    
    // Gets an array of strings from the text object or text range.

    var text, textItems, count, i;
    
    text = [];
    
    // Get a list of the strings in the text object or text range.
    if (textObj.constructor.name !== "TextRange") {
        textItems = textObj.GetText(Constants.FTI_String);
    } else {
         textItems = doc.GetTextForRange(textObj, Constants.FTI_String);
    }
    // Concatenate the strings.
    count = textItems.len;
    for (i = 0; i < count; i += 1) {
        text.push (textItems[i].sdata);
    }
    
    return text; // Return the array
}

1 reply

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
August 29, 2024

This is kind of a brute force method, but it looks like a prefix shows up in the list of the element's strings. If you know for sure that the element has a prefix, you can probably use something like this. Note that you may have to drop the delimiter between the prefix and the main element text from the end of the string.

 

#target framemaker

main ();

function main () {

    var doc, element, stringArray, prefix;
    
    doc = app.ActiveDoc;
    if (doc.ObjectValid () === 1) {
        element = doc.ElementSelection.beg.child;
        stringArray = (getAnArrayOfStrings (element, doc));
        prefix = stringArray[0];
        alert (prefix);
    }
}

function getAnArrayOfStrings (textObj, doc) {
    
    // Gets an array of strings from the text object or text range.

    var text, textItems, count, i;
    
    text = [];
    
    // Get a list of the strings in the text object or text range.
    if (textObj.constructor.name !== "TextRange") {
        textItems = textObj.GetText(Constants.FTI_String);
    } else {
         textItems = doc.GetTextForRange(textObj, Constants.FTI_String);
    }
    // Concatenate the strings.
    count = textItems.len;
    for (i = 0; i < count; i += 1) {
        text.push (textItems[i].sdata);
    }
    
    return text; // Return the array
}
Inspiring
August 30, 2024

Hi Rick, it works like a charme - thank you very much. I am now able to traverse through the elements of my documents, grab the prefixes of certain Heading elements and write the prefix content into an attribute of the Heading element.