Script to change the language of all text on document

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Script to change the language of all text on document

Viewing 21 reply threads
  • Author
    Posts
    • #60811
      Cooch
      Participant

      Hi all,

      I was wondering if anyone has or knows of a script that will do the same thing as…

      Find/Change

      Find Format – Language (USA)

      Change Format – Language (UK)

      I've built a custom dictionary for our specific type of work, but to use it on existing documents I need to do this find/change first.

      To run a script over a document when it's first opening would be a huge saving in time considering I get between 20 and 30 jobs through here per day.

      Any help would be greatly appreciated

      Thanks in advance

      Graeme

    • #60817

      Main();

      function Main() {
      if (app.documents.length == 0) {
      alert(“Please open a document and try again.”);
      exit();
      }
      var doc = app.activeDocument;
      app.findTextPreferences = app.changeTextPreferences = null;
      app.findTextPreferences.appliedLanguage = “English: USA”;
      app.changeTextPreferences.appliedLanguage = “English: UK”;
      doc.changeText();
      app.findTextPreferences = app.changeTextPreferences = null;
      }

    • #60818
      Cooch
      Participant

      That's awesome Kasyan, thank you so much.

      I've just saved and installed it and it works a treat, really wish I knew how to write a script.

      Thanks again.

      Graeme.

    • #60834

      Here I posted some links for first-time scripters. From my own experience (I'm not a programmer by background), I can tell you that EVERYBODY can write scripts — the devil is not so terrible as he is painted!

      The most difficult is to make the first step. But when you make progress in it, another problem appears: you can't stop — scripting is very absorbing.

      On this site, I find a lot of information on how to use InDesign — both for beginners and advanced users. There are many “ready to use” scripts here as well. But I've never seen a scripting tutorial here and remember reading only one scripting trick lately.

      I think this is a big omission for indesignsecrets and suggest that a series of scripting lessons, tutorials, articles, tips & tricks, etc. should be started here on regular basis.

    • #60857
      David Blatner
      Keymaster

      Kasyan, I think you have a great idea. We will certainly think about how we can include some beginner scripting articles here, trying not to scare away designers and other users. :)

    • #61197

      would be even better if I had a simple dialog window where I could choose source and target language. anyone?

    • #61198

      Sure, no problem. May Be Handy: if you have some text selected, it will be the initial language in the Find box.

      Main();
      function Main() {
      if (app.documents.length == 0) {
      alert(“Please open a document and try again.”);
      exit();
      }
      langList = app.languagesWithVendors.everyItem().name.sort();
      langList.splice (0,0, langList[langList.length-1]);
      langList.pop();

      preselected = 0;
      if (app.selection.length == 1 && app.selection[0].hasOwnProperty(“appliedLanguage”))
      {
      for (i=0; i<langList.length; i++)
      {
      if (langList[i] == app.selection[0].appliedLanguage.name)
      {
      preselected = i;
      break;
      }
      }
      }

      var langDialog = app.dialogs.add({name:”Find/Replace Languages”, canCancel:true});
      with (langDialog)
      {
      with(dialogColumns.add())
      {
      with(dialogRows.add())
      {
      staticTexts.add({staticLabel:”&Find”, minWidth:40});
      findDropDown = dropdowns.add ({stringList:langList, selectedIndex:preselected});
      }
      with(dialogRows.add())
      {
      staticTexts.add({staticLabel:”&Change”, minWidth:40});
      chngDropDown = dropdowns.add ({stringList:langList, selectedIndex:0});
      }
      }
      }

      if (langDialog.show() == true)
      {
      var doc = app.activeDocument;
      app.findTextPreferences = app.changeTextPreferences = null;
      app.findTextPreferences.appliedLanguage = langList[findDropDown.selectedIndex];
      app.changeTextPreferences.appliedLanguage = langList[chngDropDown.selectedIndex];
      doc.changeText();
      app.findTextPreferences = app.changeTextPreferences = null;
      }
      }

      The convulated stuff right after retrieving the language list is because the languages don't come in their usual sorted order. It's easy to apply a sort, but then [No Language] ends up at the bottom. Rather than writing a custom sort function, I merely move the last item up to the top. I'm only assuming that'll actually always be [No Language].

    • #61203

      amazing, works like a charm. exactly what I was looking for. tnx

    • #61210
      David Goodrich
      Participant

      I don't mean to look a gift horse in the mouth, but this very handy script seems not to work for languages that don't come with ID, such as Chinese and Japanese (it took me a while to realize my ID grabbed those languages from imported files). Poking around, I found an older script Jongware made available over on Adobe's ID scripting forum that does find all the languages in a document, though so thorough a check takes time. I often get files with all kinds of odd languages, and I can't search for them unless I know they're there. So I really want to thank Jongware for both scripts.

      David

    • #61212

      Well, David, you are correct in your observation of the behavior of these two scripts. The one above inquires InDesign about the list of supported languages, and the one you mention reads them from a document — which, annoyingly, may include text in a language that's actually not supported! So you can't use this script to remove the non-supported languages.

      Oh, the conundrum.

      I'll take a peek to see if it's possible to combine the two (and make the list of not-supported languages appear in a section of their own, in full ID Font List style. Also, if possible.)

    • #61214

      Okay so this is a tricky one. It appears you cannot easily use “find” with a language that is not in InDesign's internal list.

      I've put up a query on Adobe's scripting forum for this, as I got everything working just fine — it's just this one little thing that stumps me!

    • #61356
      Eric Chapman
      Member

      How exactly do I install this script in ID (I have CS4 and CS5.5 (Mac OS 10.6.8)? And is it Java or AppleScript? I found this explanation of how to install a script (https://creativepro.com/how&#8230;..design.php), but since what Jongware provided is the text of the script and not a script file, I don't know what to save it as (.js, .jsx, .as, .scpt).

      I went to Apple Script Editor and tried to save it as .scpt, since that's what came up. I got a syntax error: “Unknown token can't go after this “)”. It was referring to the ); at the end of Main();. So I'm assuming this was a Java script. Next I went to TextEdit and pasted in Jongware's script saving it as Language_Change_Throughout.jsx. I put it in the Scripts Panel folder, and it now shows up in ID CS5.5. And it works beautifully! Thank you, Jongware!

      I am posting this simply for other newbies who might not know how to install the script text above.

    • #61357
      Colin Flashman
      Participant

      @Eric:

      Jongware's script is javascript, not java, nor applescript.

      go back to Jongware's post and hit the “select code” button. copy this text to your clipboard.

      next, open textedit on your mac (or textwrangler if you have it) and paste the text into a new text file.

      from here, save the file with whatever name you like, SO LONG AS the suffix is “jsx”.

      then, put this into your scripts panel folder of indesign like the indesignsecrets.com page suggests.

    • #61359

      Eric, sorry — you are correct, I re-read my post and indeed forgot to mention it's Javascript.

      InDesign supports three different scripting languages out-of-the-box, and so it would be wise to always state what language is used :-) There are a few small clues, though, hidden inthe “syntax”:

      AppleScript uses a syntax based on English:

      tell application “InDesign” to copy item 3 of page 1 … — this is a comment

      etc. (I'm making this one up as I don't speak AppleScript).

      Visual Basic leans heavily on ye olde Basic and has Initial Caps for Everything:

      Sub CopyItem (item) ' a comment after a ' sign

      x = Rectangles.Add()

      Javascript looks like … the above :D Lots of curly braces, a semicolon to end each command.

    • #78354

      But if I want to change the language of all my paragraph styles instead document text?

      • #82986

        I have actually just written this script to accomplish what you asked for:

        Application.prototype.main = function() {

        if (!app.documents.length < 1 ){
        var theDoc = app.documents[0];
        var theParagraphStyles = theDoc.allParagraphStyles;
        }
        else{
        alert(“You need an open document to run this.”);
        exit();
        }

        var myDialog = app.dialogs.add({name:”Change global language in all paragraph styles”, canCancel:true});
        var myLanguages = app.languagesWithVendors.everyItem().name.sort();
        myLanguages.splice (0,0, myLanguages[myLanguages.length-1]);
        myLanguages.pop();

        try{
        with(myDialog)
        {
        with(dialogColumns.add())
        {

        with(borderPanels.add())
        {
        staticTexts.add({staticLabel:”Change language to “});
        var myLanguagesDropDown = dropdowns.add({stringList:myLanguages, selectedIndex:0});
        staticTexts.add({minWidth:25});
        }

        }
        }// End of the dialog box

        }//End of try
        catch(e){alert(e + ” Just another error”);}

        var myResult = myDialog.show();

        if(myResult == true){
        for (var i=1;i<theParagraphStyles.length;i++){
        theParagraphStyles[i].appliedLanguage = myLanguages[myLanguagesDropDown.selectedIndex];
        }
        }
        else{
        exit();
        }
        }
        app.doScript(‘app.main();’,ScriptLanguage.JAVASCRIPT,undefined,UndoModes.ENTIRE_SCRIPT, “Change language in all paragraphstyles”);

    • #83075
      Gert Verrept
      Member

      I found something similar, long time ago, don’t know who made it, but it still works, so I’ll post it here too

      var myDoc = app.activeDocument;

      var myLanguages = new Array();

      for (j=0;j<app.languagesWithVendors.length;j++){
      myLanguages.push(app.languagesWithVendors[j].name);
      }

      myDlg = new Window(‘dialog’, ‘Select language’);
      myDlg.orientation = ‘column’;
      myDlg.alignment = ‘right’;
      //add drop-down
      myDlg.DDgroup = myDlg.add(‘group’);
      myDlg.DDgroup.orientation = ‘row’;
      myDlg.DDgroup.add(‘statictext’, undefined, “Languages”);
      myDlg.DDgroup.DD = myDlg.DDgroup.add(‘dropdownlist’, undefined, undefined, {items:myLanguages})
      myDlg.DDgroup.DD.selection = 7;
      myDlg.closeBtn = myDlg.add(‘button’, undefined, ‘OK’);

      // add button functions
      myDlg.closeBtn.onClick = function()
      { this.parent.close();
      }
      result = myDlg.show();
      alert(myDlg.DDgroup.DD.selection);

      for (i=0;i<myDoc.paragraphStyles.length;i++){
      try{
      myDoc.paragraphStyles[i].appliedLanguage = app.languagesWithVendors.item(myDlg.DDgroup.DD.selection.index);
      }catch(e){}
      }

      for (k=0;k<myDoc.characterStyles.length;k++){
      try{
      myDoc.characterStyles[k].appliedLanguage = app.languagesWithVendors.item(myDlg.DDgroup.DD.selection.index);
      }catch(e){}
      }

    • #14323116

      I would be grateful if the offered scripts, quite practical for users handling versions of documents in several languages, were updated/retouched for working in current InDesign 2020, as they don’t, for some syntax changes that make them incompatible. Thanks a lot.

    • #14323115

      I just tested the ‘Change the language in all paragraph and/or character styles’ script in InDesign 2020: it works as expected.
      https://bit.ly/2ScnR1U

    • #14323114
      Gert Verrept
      Member

      The one I posted is still working fine in INDD 2020

    • #14333368
      Ryan Dumas
      Member

      I’m trying to install and run the script but running into issues. Should I save the script as an rtf from my Text Editor and then change the file extension to “.js”?

    • #14333369
      Ryan Dumas
      Member

      Please disregard my previous comment. My error was using .rtf initially so the quote marks were creating errors. I changes to plain text and the script works great – thank you!

    • #14347379
      Tommaso Galvani
      Participant

      Hi there,
      I thank you all for the wonderful help you’re giving.
      I’m quite a newbie scripter, but I’m working so hard to learn the “how-to”.

      I think this is the right post for my issue: I need to implement an existing script, adding this simple command: “change selected text language in “English: UK””.

      I’m using the “Smart Title Case Script” (which I’m trying to improve in other ways by the way, if anyone’s interested!), which works on any selected text. I would appreciate any help, tip or suggestion.

      Thank you so much!

Viewing 21 reply threads
  • You must be logged in to reply to this topic.
>