JS to delete old style and replacing it wih new one.

Home Forums InDesign Add-ons (Scripts, Scripting, and Plug-ins) JS to delete old style and replacing it wih new one.

This topic contains 12 replies, has 6 voices, and was last updated by  Loic Aigon 6 months ago.

  • Author
  • #102318

    Masood Ahmad


    I have some files where I copy the text from one source InDesign file to the template file. The source file contains styles which needs to be replace with the one in the Template. I was performing this task by deleting the old style and replacing with the new one.

    I was looking for a script to automate this and somehow I found this script on the net:


    I tried to manipulate it as given below, but didn’t got any success. Can someone help me to fix this script or process.

    Thanks a lot in advance.

    var old1 = app.activeDocument.paragraphStyles.itemByName("body text");
    var new1 = app.activeDocument.paragraphStyles.itemByName("Text - nVent Data Sheet");

    var old2 = app.activeDocument.paragraphStyles.itemByName("body text with no After Space");
    var new2 = app.activeDocument.paragraphStyles.itemByName("Text - nVent Data Sheet");

    var old3 = app.activeDocument.paragraphStyles.itemByName("body text (span)");
    var new3 = app.activeDocument.paragraphStyles.itemByName("Text - nVent Data Sheet");

    var old4 = app.activeDocument.paragraphStyles.itemByName("body text (right)");
    var new4 = app.activeDocument.paragraphStyles.itemByName("Text - nVent Data Sheet");

    var old5 = app.activeDocument.paragraphStyles.itemByName("body text (centre)");
    var new5 = app.activeDocument.paragraphStyles.itemByName("Text - nVent Data Sheet");

    var old6 = app.activeDocument.paragraphStyles.itemByName("reference text");
    var new6 = app.activeDocument.paragraphStyles.itemByName("Text - nVent Data Sheet");

    var old7 = app.activeDocument.paragraphStyles.itemByName("reference text (span)");
    var new7 = app.activeDocument.paragraphStyles.itemByName("Text - nVent Data Sheet");

    var old8 = app.activeDocument.paragraphStyles.itemByName("bullet");
    var new8 = app.activeDocument.paragraphStyles.itemByName("Bullet - nVent Data Sheet");

    var old9 = app.activeDocument.paragraphStyles.itemByName("bullet (span)");
    var new9 = app.activeDocument.paragraphStyles.itemByName("Bullet - nVent Data Sheet");

    var old10 = app.activeDocument.paragraphStyles.itemByName("sub bullet");
    var new10 = app.activeDocument.paragraphStyles.itemByName("Sub bullet");

    var old11 = app.activeDocument.paragraphStyles.itemByName("last bullet");
    var new11 = app.activeDocument.paragraphStyles.itemByName("Bullet - nVent Data Sheet");

    var old12 = app.activeDocument.paragraphStyles.itemByName("last bullet (span)");
    var new12 = app.activeDocument.paragraphStyles.itemByName("Bullet - nVent Data Sheet");

    var old13 = app.activeDocument.paragraphStyles.itemByName("first bullet");
    var new13 = app.activeDocument.paragraphStyles.itemByName("First Bullet - nVent Data Sheet");

    var old14 = app.activeDocument.paragraphStyles.itemByName("first bullet (span)");
    var new14 = app.activeDocument.paragraphStyles.itemByName("First Bullet - nVent Data Sheet");

    var old15 = app.activeDocument.paragraphStyles.itemByName("table text");
    var new15 = app.activeDocument.paragraphStyles.itemByName("Table Text - nVent Data Sheet");

    var old16 = app.activeDocument.paragraphStyles.itemByName("table text (header)");
    var new16 = app.activeDocument.paragraphStyles.itemByName("Table Subhead - nVent Data Sheet");

    var old17 = app.activeDocument.paragraphStyles.itemByName("main Caddy header");
    var new17 = app.activeDocument.paragraphStyles.itemByName("Page 1 Subhead White - nVent Data Sheet");

    var old18 = app.activeDocument.paragraphStyles.itemByName("sub heading (level 1) (span)");
    var new18 = app.activeDocument.paragraphStyles.itemByName("Subhead - nVent Data Sheet");

    var old19 = app.activeDocument.paragraphStyles.itemByName("sub header (level 2)");
    var new19 = app.activeDocument.paragraphStyles.itemByName("Subhead 2 - nVent Data Sheet");

    var old20 = app.activeDocument.paragraphStyles.itemByName("sub header (level 2) (span)");
    var new20 = app.activeDocument.paragraphStyles.itemByName("Subhead 2 - nVent Data Sheet");

  • #102329

    Are you able to be a little more specific (i.e. when you say “but didn’t got any success” do you mean that nothing happened, or an error was produced?)

    It’s working for me, but both styles have to be present in the current document. That is, if the desired style isn’t in the document, it won’t work.

    The script is rather verbose, I’d probably use a for-loop rather than copy the code out twenty times, but either way it should work.

  • #102331

    Masood Ahmad

    …nothing happened. As you said, some of the old styles are not available. It depends, how my source is formatted therefore, when I copy text from source to Template file, some unused styles don’t get copied to the Template. I think that’s the problem.

    Any suggestions would be much appreciated.

  • #102334

    OK, that’s not what’s happening here when I test on my Mac, the script works as intended.

    My suggestion would be to bring in the desired styles first from a document that contains the styles you need, and then run the code you have. There is a script that will import all styles, master pages etc that might help – go to http://lf.corullon.com.br/scripts_id_lfc/index_enus.html and look for “byLFC_import what you want”

    While my earlier post does comment about the length of the code you have, it will still work and shouldn’t present any issues.

  • #102387

    Mary Posner

    I second Colin’s suggestion to do this with a for-loop, although I did also test a sample of what you have (also on a Mac) and it worked for me as well.

    I’d set up two arrays, one with the names of the old styles you’re looking to replace, one with the new ones you want to replace them with, and then loop through the items in those arrays. Wrap the “old.remove(new);” line in a try/catch in case one of the styles isn’t present in the document. However, keep in mind that if an old style is present in the document, but the corresponding new one isn’t, it will of course fail to change it.

  • #102498

    Masood Ahmad

    To all the Geniuses rather Money-making guys,

    If I would have known JavaScript, then I wouldn’t have posted this request here. There’s no point suggesting this or that. Simply give the solution, or leave it as-is. No one is forcing you to respond.

    I remember, the times when people share their knowledge on the Forums including InDesignSecrets.com. But God knows from these money making guys have come together here. It seems that they are out of their job and trying to earn their livelihood in some or the other way :)

    @InDesignSecrets team, as I said earlier, please create a separate page for these money loving guys and let them earn bread & butter for the day.

    I’m desperate to learn JavaScript but not getting the starting point. I’m trying to catch someone who can teach me JS for InDesign. And I’m sure, one day I’ll learn JavaScript.

    Thanks a ton for all your responses!

    • #102512

      David Blatner

      Hi Masood… I’m sorry, but I do not understand what you mean. I do not see any reference to making money in this thread.

      I agree that scripters looking for help with a script should not be offered paid consulting help. The scripter community must help each other. :-)

    • #102513

      As I understand, Masood doesn’t seem to appreciate an help given for free by so cool people as Mary and Colin [and me now because I agree they are right! =D ] because, according to him, their “rough” answers [and of course mine now] prevent him from making money himself with a workable personalized script written by Mary and Colin and this for free!

    • #102532

      Masood Ahmad

      Hi David, You need not to say sorry. I know you’re are doing a great job by supporting such a beautiful platform where people seek and get help. We all learn from here. But from the last couple of months, I am noticing that some people are not of helping nature. They need money for their efforts. And it is not bad, one should get the fee for his time. But forums like this is not the right place. This is a community where people come in a hope that they’ll get their issue resolved. But some people are violating its soul.

      It is also true that there’s no discussion of money making here on this thread. But it is there in another thread posted by me.

      Alternating Fills

      Reply #102308

      …where Colin, tries to give his humble support in breakups, supported by Kai.

      Colin, here follows the same pattern of his generous support.

      What I request here is for myself only. I don’t get a penny out of it. I don’t sell these things. My people come to me with varied queries and I try to resolve them as best I can. Sometimes, I too get stuck with some repeated task, so I think of a script and look for it. That’s it.

      My request to all the members is that if you’re willing to support this forum with your valuable experiences and knowledge, then please come forward, else nobody is forcing you to respond.

  • #102508


  • #102538

    David Blatner

    Hi Masood, I think perhaps what you are asking for is bigger than you expect. I’m sure you do not expect anyone to write the script for you for free, unless it would take only maybe 10 minutes. Yes, this forum is for people helping each other, but everyone is busy and their time is valuable. For a larger project that takes someone a significant amount of time, I think it is very reasonable to expect people to ask for a consulting fee.

    You mentioned in your post that you would like to learn how to script. That is awesome! I would suggest Keith Gilbert’s video title: InDesign Scripting Made Easy

  • #102539

    I’m sorry if my answers weren’t what you were expecting or wanted, but I believed that this forum operated in a similar function to the InDesign Scripting forum on the Adobe forums – that is, scripters posting to other scripters wanting help with specifics in scripts or bug detection. If I’ve misunderstood the purpose of this forum then that’s on me.

    I also feel that there is a misunderstanding about me trying to sell my services – I’m not doing that. Again, if that’s how it came across in my post then I apologise, but I actually feel identically to you in that using forums to advertise one’s services isn’t how one should conduct business and should NOT be done… whether this forum or any social media. My comment was simply meant to highlight that writing a complete script is usually a commercial service, and not something I’d expect to find in the public domain unless the author felt like doing so out of complete benevolence.

    It’s worth stating that I regularly contribute free scripts in the public domain via my own website. In fact, my website has no scripts available for purchase (at the time of writing this post) and the only money that changes hands via my site is if people choose to donate or not, and that option is entirely up to the user. Any post from me (or article, script, video etc) whether on this site or another – is completely done in my own spare time and not during my working hours as a Prepress Operator. For me, this is my form of serving my community – that is, other InDesign users.

  • #102587

    Loic Aigon

    While I certainly agree to the concept of being generous (as I try to always be), I am not disturbed by the idea of offering paid-for services to certain conditions. The reason is that sometimes people, naively or worse willingly will ask for huge piece of scripts that will ease their work. I do a difference between one asking for advice in his own development effort (this one should be encouraged and helped by community) and the one just saying, I need this please (and now).
    At this stage, it should be everyone conscience to either deliver for free (I do it more than often) or say you know what, the script i could write and that you are not trying to develop on your own could save you a lot of efforts and money, so that would be fair to get some money out of it (I help you, it helps me).
    The only thing that annoy me is when those offers of services are strongly and publicly repeated all over the place. When I do on a few occasions, I do send private message and I don’t judge the emitter. I just offer, you take it or leave it, no problem.
    And eventually, we, developers, have regular clients who can pay a lot for scripts and I don’t think they would enjoy seeing the same code delivered for free. So clearly when I provide some code, it’s either because emitter tried something first, or the expectation is low, or that I am in a really good mood (i.e. all stars aligned).

You must be logged in to reply to this topic.