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 4 replies, has 3 voices, and was last updated by  Mary Posner 4 days, 1 hour 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.

You must be logged in to reply to this topic.