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

How to change Tag Element Name in xml structure?

Contributor ,
Jan 07, 2019 Jan 07, 2019

Hi Everyone,

               I want to change tag name after creating tag through scripting.

Untitled.png

Thank you.....

TOPICS
Scripting
987
Translate
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 , Jan 07, 2019 Jan 07, 2019

Ahh, my bad i did not look into it deeply. Well your case would fail if the selection is a textframe as mine would if the selection is a text. So a more appropriate code that handles both the cases would be

var newItem = app.activeDocument.xmlTags.itemByName("Name") 

if(newItem.isValid)

{

     var a;

     try{

          a = app.selection[0].associatedXMLElements[0]

     }catch(e){}

     try{

          if(!a)

               a = app.selection[0].associatedXMLElement

     }catch(e){}

     if(a)

          a.mar

...
Translate
Community Expert ,
Jan 07, 2019 Jan 07, 2019

You could use the following

var newItem = app.activeDocument.xmlTags.itemByName("boxed-text")

if(newItem.isValid)

    app.selection[0].associatedXMLElement.markupTag = newItem

Add in the required error checks like the selection is valid or not and so forth.

-Manan

Translate
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
Contributor ,
Jan 07, 2019 Jan 07, 2019

Thank you....

It works when app.selection[0].associatedXMLElement.markupTag = newItem change to

app.selection[0].associatedXMLElements[0].markupTag = newItem,

only difference is associatedXMLElements[0].

Translate
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 ,
Jan 07, 2019 Jan 07, 2019

Ahh, my bad i did not look into it deeply. Well your case would fail if the selection is a textframe as mine would if the selection is a text. So a more appropriate code that handles both the cases would be

var newItem = app.activeDocument.xmlTags.itemByName("Name") 

if(newItem.isValid)

{

     var a;

     try{

          a = app.selection[0].associatedXMLElements[0]

     }catch(e){}

     try{

          if(!a)

               a = app.selection[0].associatedXMLElement

     }catch(e){}

     if(a)

          a.markupTag = newItem

}

The idea is simply to use try catch block to see if the property exists or not, this should also cater to selection not being tagged as well.

-Manan

Translate
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 ,
Jan 07, 2019 Jan 07, 2019

After thought a more clean solution would be to use hasOwnProperty method

var newItem = app.activeDocument.xmlTags.itemByName("Name")

if(newItem.isValid)

{

     var a;

     if(app.selection[0].hasOwnProperty('associatedXMLElements'))

          a = app.selection[0].associatedXMLElements[0]

     else if(app.selection[0].hasOwnProperty('associatedXMLElement'))

          a = app.selection[0].associatedXMLElement

     if(a)

          a.markupTag = newItem

}

-Manan

Translate
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
Contributor ,
Jan 07, 2019 Jan 07, 2019
LATEST

my app.selection is text because i am selecting that text before replacing tag name, thats why app.selection[0].associatedXMLElements[0] it works.

Thank you for helping......

Translate
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