• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

wrap text with element

Explorer ,
Oct 14, 2016 Oct 14, 2016

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

811

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 17, 2016 Oct 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

Votes

Translate

Translate
Mentor ,
Oct 17, 2016 Oct 17, 2016

Copy link to clipboard

Copied

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;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 17, 2016 Oct 17, 2016

Copy link to clipboard

Copied

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;

        }

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 17, 2016 Oct 17, 2016

Copy link to clipboard

Copied

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 17, 2016 Oct 17, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 17, 2016 Oct 17, 2016

Copy link to clipboard

Copied

LATEST

re-Hello,

lol Rick ... i was trying the same when i look your last code (your code look prettiest)

Thanks very much both of you

It work just fine.

PS: Juste made a mistake trying to set the attribute before creating the tag, but needed to set after

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines