Skip navigation
Currently Being Moderated

Rename particular XML Tag

Jun 26, 2012 3:16 AM

Tags: #indesign_cs5 #rename #xmlelement

Hi Indesign Experts,

          I'm confused a lot in the below issue. I'm have a indesign document. In the xml structure, If i select a element, and trying to rename the selected item's markupTag.name, it's changing all the elements with the selected elements name....

input.GIF

I use the below code to run after selecting an xml Element.

 

app.selection[0].markupTag.name="test";

But the output look like the below.

output.GIF

But I want onlu to rename the selected xmlelement.

Any idea to perform this?

Thanks and Regards,

Vel.


 
Replies
  • Currently Being Moderated
    Jun 26, 2012 7:18 AM   in reply to Velprakash

    You're changing the name of the applied tag rather than assigning a different tag. Try this:

     

    var testTag = app.activeDocument.xmlTags.item("test").isValid ? app.activeDocument.xmlTags.item("test") : app.activeDocument.xmlTags.add({name: "test"});
    app.selection[0].markupTag = testTag;
    

     

    Jeff

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 26, 2012 2:36 PM   in reply to Velprakash

    Jeff is right.

     

    There is a quick and dirty way to create the tag on the fly: the "markupTag" property also accepts strings.

     

    app.selection[0].markupTag = "test";

     

    Dirk

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 27, 2012 11:06 AM   in reply to Dirk Becker

    Thanks Dirk. That's better. For some reason I always use the more explicit object assignment in these kinds of situations. Do you know of other instances where a string assignment like that will result in a non-existant object being created on the fly? Are there any rules governing this that you can make out?

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 27, 2012 11:43 AM   in reply to absqua

    The internal description of properties allows for alternative types. Most of the time the primary type would be the result for the getter access, while the alternative types are the accepted values for setters. I haven't searched for them in detail, but styles come to mind that are adressable by name, without creating them.

     

    In the ObjectModel Viewer xml file these types are automatically lumped together in the comment.

     

    Dirk

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 27, 2012 11:59 AM   in reply to Dirk Becker

    Dirk Becker wrote:

     

    ...but styles come to mind that are adressable by name, without creating them.

     

    I understand that you can use strings for assignments to a lot of different properties, but, e.g. in the case of styles, if the style itself—not the JavaScript object—doesn't exist, you'll get an error; it won't create the style for you. Your example with the xmlTags is the first time I think I've seen such creation "on the fly."

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 28, 2012 6:58 AM   in reply to Velprakash

    I'm glad it helped. But the try/catch seems unnecessary here; the isValid property was provided for just this kind of check. I used the ternary operator construct, but in a normal if/then it would look like:

     

    var testTag;
    if (app.activeDocument.xmlTags.item(renamewith).isValid) {
        testTag = app.activeDocument.xmlTags.item(renamewith);
    }
    else {
        testTag = app.activeDocument.xmlTags.add({name: renamewith});
    }
    

     

    But maybe this is only part of what you have in your try/catch in your complete script...

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points