Skip to main content
Inspiring
October 14, 2016
Answered

wrap text with element

  • October 14, 2016
  • 2 replies
  • 1093 views

Hello,

I'm using FM12 with structured docs. I'm looking to convert some Framescript into Extendscript language.

I finish my first Extendscript this morning (too me 3 days) : searching Element and saving the attributes value into a text file.

But I'm stuck converting my second one.

In a structured doc, i want to wrap a selected text with a element "PRESS" into a element "PARA" (the element PRESS is allowed in the element PARA by the DTD/EDD).

My Framescript (FS 6.0)

Set vCurrentDoc = ActiveDoc;

Set vSelect = vCurrentDoc.TextSelection;

// Get the Element Def

Get Object NewVar(vEltDef) Type(ElementDef) Name('PRESS');

// Wrap the selected text

Wrap ElementDef(vEltDef);

// Select the new PRESS element

Set vPRESS = ElementSelection.begin.child;

// Create the attribs

New AttributeList NewVar(vPRESSatt) AttrName('SMALLCAPS') Value('1');

// Assign the Attributes to the tag

Set vPRESS.Attributes = vPRESSatt;

In Extendscript, how do i write it?

var vCurrentDoc = app.ActiveDoc;

var vSelect = vCurrentDoc.TextSelection;

var vElem = vCurrentDoc.GetNamedElementDef("PRESS");

var attrs = vElem.AttributeDefs;

attrs[1].values = "ITAL";

vElem.Attributes = attrs;

vElem.WrapElement();

The script add the PRESS element but the attribute is not fill. Why? and how to correct it?

Many thanks for considering my request.

Ce message a été modifié par : Philippe Pinpin Add info

This topic has been closed for replies.
Correct answer frameexpert

For my function, you need an element object, the attribute name, and the value you want to set for the attribute. You can test it with the selected element by doing this:

var doc = app.ActiveDoc;

var element = doc.ElementSelection.beg.child;

Now call the function, using element and the attribute names and values.

-Rick

2 replies

frameexpert
Community Expert
Community Expert
October 17, 2016

Here is my version of a similar function.

function setAttributeValue (element, name, value) {

   

    // Sets the value of an attribute on the element.

   

    var attrList = element.Attributes, i = 0;

    for (i = 0; i < attrList.length; i += 1) {

        if (attrList.name === name) {

            attrList.values[0] = value;

            element.Attributes  = attrList;

            return;

        }

    }

}

www.frameexpert.com
Inspiring
October 17, 2016

Hello Russ and frameexpert,

I started my code with that function. But I had an "undefined is not an objet" error.

So i decide to make my code as simply as possible to find my error.

I think the problem come from my var declaration.

When I set vElem = vCurrentDoc.GetNamedElementDef("PRESS") he create a vElem [Object ElementDef] but I think i need an [Object Element].

So when I want to get the attribute i need to use the "AttributeDefs" instead of "Attributes". But i get some error with it.

So how i can get [Object Element] from an [Object ElementDef] ?

(Sorry if my question it's so obviously but i juste started javascript with Extendscript last week and my OOP knowledge is poor)

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
October 17, 2016

For my function, you need an element object, the attribute name, and the value you want to set for the attribute. You can test it with the selected element by doing this:

var doc = app.ActiveDoc;

var element = doc.ElementSelection.beg.child;

Now call the function, using element and the attribute names and values.

-Rick

www.frameexpert.com
Legend
October 17, 2016

Hi philippep2776167,

It is a bit tricky to set an attribute. Below is a function that demonstrates how to do it. You can send the function either the name of the attribute you want to set or the index. I think this example should give you the information that you need.

Russ

//Sets an attribute value by name or by index.

//Send an index or an attribute name, not both.

//The name is case sensitive.

//Returns true if successful, false otherwise

//It overwrites whatever is already there.

function SetAttribute(elem, attrName, attrIndex, newValue)

{

    if(elem == null || !elem.ObjectValid()) return false;

   

    var returnVal = false;

   

    var attrs = elem.Attributes;

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

    {

        if (attrs.name == attrName ||

            i == attrIndex)

        {

            attrs.values = new Strings();

            attrs.values.push(newValue);

           

            elem.Attributes = attrs;

            i = attrs.length;

            returnVal = true;

        }

    }

    return returnVal;

}