8 Replies Latest reply on Oct 8, 2015 2:14 AM by K159

    Change the word.

    K159 Level 1

      Hi,

       

      I want to get the last word (cursor left word) in the text frame would like to change it to another word.

       

       

         < - textFrame

          STEP 1 : Run the script.

       

       

       

       

       

         STEP 2 : It selects the last word by the cursor.

       

       

       

       

       

       

         STEP 3 : Get selected word.

       

       

       

       

       

       

         STEP 4 : Type the word I want to change.

       

       

       

       

       

         STEP 5 : Change the word.

       

       

       

       

       

      ======================================================

      #targetengine session;
      main();
      function main()
      {
          var curDoc = app.activeDocument;
          var mySel = app.selection[0];   
         
           if(!app.selection[0])
              alert("did not select the object.");
             
          else if (app.selection[0].textFrames)
          {
              if(app.selection[0].contents)   // Word blocks
                  var mySelWord = mySel.words.lastItem().contents;
              
              else
                  var mySelWord = "did not select the word.";    
          }

         
          var w = new Window ("palette", "Form");
          w.add ("statictext", undefined, "Get selected word:");
          var getText = w.add ("edittext", undefined, "");
          getText.text = mySelWord;
         
          w.add ("statictext", undefined, "Change the word:");
          var changeText = w.add ("edittext", undefined, "");
         
          getText.characters = 20;
          changeText.characters = 20;
          changeText.active = true;
          var myButtonGroup = w.add ("group");
          myButtonGroup.alignment = "right";
          var bt1 = myButtonGroup.add ("button", undefined, "Change word");
          var bt2 = myButtonGroup.add ("button", undefined, "Cancel");
         
         
         
          bt1.onClick = function()
          {
              var myDocument = app.activeDocument;   
       
             app.findTextPreferences = NothingEnum.nothing;
             app.changeTextPreferences = NothingEnum.nothing;
              //Set the find options.
              app.findChangeTextOptions.caseSensitive = false;
              app.findChangeTextOptions.includeFootnotes = false;
              app.findChangeTextOptions.includeHiddenLayers = false;
              app.findChangeTextOptions.includeLockedLayersForFind = false;
              app.findChangeTextOptions.includeLockedStoriesForFind = false;
              app.findChangeTextOptions.includeMasterPages = false;
              app.findChangeTextOptions.wholeWord = false;
              //Search the document for the string "copy" and change it to "text".
              app.findTextPreferences.findWhat = getText.text;
              app.changeTextPreferences.changeTo = changeText.text;
              myDocument.changeText();
              //Clear the find/change text preferences after the search.
              app.findTextPreferences = NothingEnum.nothing;
              app.changeTextPreferences = NothingEnum.nothing;   
             
              w.close();
          }   
                 
          w.show ();

      }

      =============================================

       

       

      I can get the word, if run the script after text selection(block).

       

      But I don't know how to get the last word without selection(block).

       

      Please help me.

       

      Even a little helps a lot.

       

      Thanks.

        • 1. Re: Change the word.
          Jump_Over Level 5

          Hi,

           

          Your code has trouble while veryfying selection.

          I suggest to create a new function to do this job and return a word or false.

          This function should check if:

          •     selection is Valid
          •     selection is "text-based"
          •     detect if selection is "block" or "cursor"
          •     find last word and return OR return false

           

          I.e. like this:

          var mySelWord = lastWord() ? lastWord().contents : "did not select the word.";
          function lastWord()
          {
            if (!app.documents.length || !app.selection.length)
            return false;
            var
                 resWord = false,
                 mSel = app.selection[0],
                 mCursor, cIdx, mStory;
          
            if (mSel.hasOwnProperty ("baseline") )
                 mCursor = mSel.insertionPoints[-1];
            else return resWord;
          
            cIdx = mCursor.index;
            mStory = mCursor.parent;
          
            do {
                 if (!cIdx) break;
                 if (mCursor.words.length) {
                      resWord = mCursor.words[0];
                      break;
                 }
                 mCursor = mStory.insertionPoints[cIdx--];
                 } while (true)
          
            return resWord;
            }
          

           

          use var mySelWord to further job of script

           

          Jarek

          • 2. Re: Change the word.
            K159 Level 1

            Your answer is very detailed.

            It really was helpful.

            Thanks Jarek !

             

             

            + ask one more.

            How to set text block like "Find"?

            • 3. Re: Change the word.
              Jump_Over Level 5

              Hi,

               

              This line in your code makes it already:


              app.findTextPreferences.findWhat = getText.text;


              Jarek

              • 4. Re: Change the word.
                K159 Level 1

                Hi, Jarek

                 

                That's not what I want.

                 

                I would like to just set the text block.

                 

                Does that make a block(like the picture) in that code?

                • 5. Re: Change the word.
                  Jump_Over Level 5

                  Hi,

                   

                  So explain me what do you mean by "set the text block"

                   

                  Jarek

                  • 6. Re: Change the word.
                    K159 Level 1

                    Sorry, I'm  English is unskilled.

                     

                    "set the text block" means selection. Black box. Like (how) in the picture.

                    • 7. Re: Change the word.
                      Jump_Over Level 5

                      Hi,

                       

                      Basically, you cant write, modify, or even read javascript code for InDesign without this help site (neverending thanks for jongware):

                      Indesign JavaScript Help

                       

                      to select text use text.select() method

                       

                      in your specific case:

                      lastWord().select();


                      Jarek

                      • 8. Re: Change the word.
                        K159 Level 1

                        Thanks for answer and advice to the end.