18 Replies Latest reply on Sep 13, 2010 4:56 AM by pasithee

    change text

    pasithee

      Hi all

       

      I try to change a text in a textFrame. As long as I use a Dialog it works. Switching to a

      Window('dialog', 'a')
      

       

      popup it does not work anymore. Why that?

       

      Here my source to change a specifiic text in a paragraph:

      for(var i = 0; i < app.selection[0].parentStory.paragraphs.length; i++) {
          var tf = app.selection[0].parentStory.paragraphs[i].parent.texts[0];
          var start = tf.parentStory.paragraphs.item(i).characters.item(0);
          var end = tf.parentStory.paragraphs.item(i).characters.item(-2);
          var line = doLine(tf.texts.itemByRange(start, end).contents.toString(), delimiter);
              
          tf.texts.itemByRange(start, end).contents = line;
      }
      

       

      thanks

      pasithee

        • 1. Re: change text
          pasithee Level 1

          does someone has an idea? any hint?

          • 2. Re: change text
            Marijan Tompa [tomaxxi] Level 4

            Can you maybe post bigger part of your script? I wanna see how you use Window().

             

            --

            tomaxxi

            http://indisnip.wordpress.com/

            • 3. Re: change text
              pasithee Level 1

              here the most important part of my script:

              var win = main();
              
              function main() {
                  var win = new Window('dialog','Reformat');
                  
                  var spaces = Array("-- add space --", "hair", "thin", "punctuation", "figure", "sixth", "quarter", "third", "em", "en", "flush", "nonbreaking (fixed width)", "nonbreaking");
                  var codes = Array("", "^|", "^<", "^.", "^/", "^%", "^4", "^3", "^m", "^>", "^f", "^s", "^S");
                  
                  win.orientation = "row";
                  win.spacing = 0;
                  
                  with(win) {
                      var delimiterField = add('edittext');
                      delimiterField.preferredSize = [100,25];
                      
                      var spacesDrop = add('dropdownlist', undefined, spaces);
                      spacesDrop.selection = 0;
                      spacesDrop.preferredSize = [20,25];
                  };
                  
                  spacesDrop.onChange = function() { delimiterField.text += codes[spacesDrop.selection.index]; spacesDrop.selection = 0; };
                  
                  
                  var group = win.add("group");
                  win.okButton = group.add("button", undefined, "reformat");
                  win.okButton.onClick = function() { reFormat(delimiterField.text) };
                  
                  win.canButton = group.add("button", undefined, "Cancel");
                  win.canButton.onClick = function() { this.window.close(2) }; 
                  
                  win.center();
                  win.show();
                  
                  return win;
              }
              
              function reFormat(delimiter) {
                      var del = delimiter;
                  
                      del = del.replace("^|", "\u200A") //hair space
                      del = del.replace("^<", "\u2009") //thin space
                      del = del.replace("^.", "\u2008") //punctuation space
                      del = del.replace("^/", "\u2007") //figure space
                      del = del.replace("^%", "\u2006") //sixth space
                      del = del.replace("^4", "\u2005") //quarter space
                      del = del.replace("^3", "\u2004") //third space
                      del = del.replace("^m", "\u2003") //em space
                      del = del.replace("^>", "\u2002") //en space
                      del = del.replace("^f", "\u2001") //flush space
                      del = del.replace("^s", "\u202F") //nonbreaking space (fixed width)
                      del = del.replace("^S", "\u00A0") //nonbreaking space
                      //" ", "\u0020" //space
                      doStory(del);
              }
              
              
              function doStory(delimiter) {
                  for(var i = 0; i < app.selection[0].parentStory.paragraphs.length; i++) {
                      var tf = app.selection[0].parentStory.paragraphs[i].parent.texts[0];
                      var start = tf.parentStory.paragraphs.item(i).characters.item(0);
                      var end = tf.parentStory.paragraphs.item(i).characters.item(-2);
                      var line = doLine(tf.texts.itemByRange(start, end).contents.toString(), delimiter); //do line returns a string
                      
                      tf.texts.itemByRange(start, end).contents = line;
                  }
              }
              

               

              thanks

              pasithee

              • 4. Re: change text
                [Jongware] Most Valuable Participant

                It appears to work on CS4, Mac -- unless this isn't what you meant?

                 

                Screen shot 2010-08-31 at 11.26.29 PM.png

                • 5. Re: change text
                  Marijan Tompa [tomaxxi] Level 4

                  Cool, nice menu

                  Yeah, working here also!

                   

                  --

                  tomaxxi

                  http://indisnip.wordpress.com/

                  • 6. Re: change text
                    pasithee Level 1


                    Hi all

                     

                    it is not exactly what I meant. So ... the replacment should take place in a text frame. So that mean if you select a text frame in your document and then run this script you see the text field with the dropdown menu (picture). There you can enter a text and select spaces. Then the script calculates some thing (not posted) and returns a string. With this string I would like to replace special text section in each paragraph in the text frame.


                    So at the end I need to replace text in a paragraph. I try to do it this way:

                        for(var i = 0; i < app.selection[0].parentStory.paragraphs.length; i++) {
                            var tf = app.selection[0].parentStory.paragraphs[i].parent.texts[0];
                            var start = tf.parentStory.paragraphs.item(i).characters.item(0);
                            var end = tf.parentStory.paragraphs.item(i).characters.item(-2);
                            var line = doLine(tf.texts.itemByRange(start, end).contents.toString(), delimiter); //do line returns a string
                            //replace a specific range in paragraph with the value of the variable line
                            tf.texts.itemByRange(start, end).contents = line; //line is a variable of type string
                        }
                    
                    

                    In this script I loop over all paragraphs of the selected text frame and replace a specific range in this paragraph with a string.

                     

                    does it help

                     

                    thanks

                    pasithee

                    • 7. Re: change text
                      [Jongware] Most Valuable Participant

                      You said the behavior changed when you changed from a regular dialog to the ScriptUI one.

                       

                      Could the problem be you don't get the correct text string returned from the ScriptUI dialog? Perhaps the string you were expecting disappears from memory if the dialog is closed -- remember, it only exists in the variable inside your dialog, so I think you have to save it into another variable before closing (= destroying) the window.

                      • 8. Re: change text
                        pasithee Level 1

                        Hi all

                         

                        it does not work. What I want is to replace a existing text in a indesign document and not the text in the input box.

                         

                        As you can see in the image I select a text frame and open the script. In the script I enter for example a comma space an click on reformat. Then the loops over each paragraph of this text frame and replaces the the text which include the value given by the script. e.g. 'Ode 6, 7, 9, 10, 11, 14' comes 'Ode 6, 9a, 14'.

                        Bild 3.png

                         

                        Thanks

                        Pasithee

                        • 9. Re: change text
                          [Jongware] Most Valuable Participant

                          Changing text with a script is tricky, because some collections get cached -- in your case, possibly the start of paragraphs inside the app.selection object.

                           

                          It appears to me you are changing your paragraphs from the first to the last one. Can you rewrite the main loop, so it changes paragraphs from end to start?

                           

                          (It would not explain why it suddenly stopped working when you changed the dialog to a SciptUi window.)

                          • 10. Re: change text
                            [Jongware] Most Valuable Participant

                            Ah -- while you are editing, you might want to clarify this:

                             

                            var tf = app.selection[0].parentStory.paragraphs[i].parent.texts[0];

                             

                            It seems to do this:

                             

                            app.selection[0] -> any selected text

                              parentStory -> the entire story

                                paragraphs[i] -> any paragraph within that story

                                  parent -> its parent ... which is parentStory

                                    texts[0] -> ... the text itself ...

                             

                            and you use the variable as

                             

                            var start = tf.parentStory.paragraphs.item(i).characters.item(0);
                            var end = tf.parentStory.paragraphs.item(i).characters.item(-2);

                             

                            which translates back into

                             

                            tf -> an entire story's worth of text

                              parentStory -> the story with that text

                                paragraphs.item(i) -> a paragraph in there

                             

                            So -- unless there is lots more code which you are not showing -- it could be compacted into

                             

                            var start = app.selection[0].parentStory.paragraphs[i].characters[0];
                            var end = app.selection[0].parentStory.paragraphs[i].characters[-2];

                             

                            without the need to walk up, then down, then up, then down the text hierarchy again (if I'm counting right).

                             

                            In addition, since you only need start and end as input for ItemByRange, you could replace the entire thing, including

                             

                            var line = doLine(tf.texts.itemByRange(start, end).contents.toString(), delimiter);

                             

                            with

                            var line = doLine(app.selection[0].parentStory.paragraphs[i].characters.itemByRange(0, -2).contents.toString(), delimiter);

                             

                            (It seemed to me "toString" on the contents is unnecessary, but -- it might be, if the paragraph consists only of one SpecialCharacter. [...] Some real intensive checking later: I bet what you get in that case is worthless anyway...)

                            • 11. Re: change text
                              pasithee Level 1

                              Hi Jongware

                               

                              thanks a lot for your answer but getting the paragraph into a variable and changing this variable works. the only thing that not work is writing the value back to the InDesign document/story.

                               

                              so lets have a simpler example that try to write the string 'ABC' back the first paragraph:

                              app.selection[0].parentStory.paragraphs[0].contents = "ABC";
                              

                               

                               

                              thanks

                              pasithee

                              • 12. Re: change text
                                pasithee Level 1

                                Hi all

                                 

                                Ok I tried to setup an example. Here to scripts.

                                 

                                This one works

                                test();
                                
                                function test() {
                                   for(var i = 0; i < app.selection[0].parentStory.paragraphs.length; i++) {
                                       
                                        var start = app.selection[0].parentStory.paragraphs.item(i).characters.item(0);
                                        var end = app.selection[0].parentStory.paragraphs.item(i).characters.item(-2);
                                        
                                        app.selection[0].texts.itemByRange(start, end).contents = "A";
                                        
                                   }
                                }
                                

                                 


                                and this one doesn't work

                                var win = main();
                                
                                function main() {
                                    
                                    var win = new Window('dialog','Reformat Index');
                                
                                    win.orientation = "row";
                                    win.spacing = 0;
                                        
                                    var group = win.add("group");
                                    win.okButton = group.add("button", undefined, "start");
                                    win.okButton.onClick = function() { test() };
                                    
                                    win.canButton = group.add("button", undefined, "Cancel");
                                    win.canButton.onClick = function() { this.window.close(2) }; 
                                    
                                    win.center();
                                    win.show();
                                    
                                    return win;
                                }
                                
                                function test() {
                                    
                                   for(var i = 0; i < app.selection[0].parentStory.paragraphs.length; i++) {
                                       
                                        var start = app.selection[0].parentStory.paragraphs.item(i).characters.item(0);
                                        var end = app.selection[0].parentStory.paragraphs.item(i).characters.item(-2);
                                        
                                        app.selection[0].texts.itemByRange(start, end).contents = "A";
                                        
                                   }
                                }
                                

                                 

                                 

                                In both scripts I replace each paragraph of a selected text frame with 'A'. When using scriptUI it doesn't work anymore. Any idea?

                                 

                                Thanks

                                pasithee

                                • 13. Re: change text
                                  [Jongware] Most Valuable Participant

                                  I can tell you right away why that doesn't work ...

                                   

                                  Look: top is before, bottom is after running your one-line script.

                                   

                                  Screen shot 2010-09-09 at 9.51.09 PM.png

                                   

                                  You replace the contents of paragraph #1 (well, #0), and that contents include the hard return at the end that tells InDesign where that paragraph #1 ends.

                                  If you use this instead

                                   

                                   

                                  app.selection[0].parentStory.paragraphs[0].contents = "ABC\r";
                                   
                                  
                                  it'll not be messed up.
                                  • 14. Re: change text
                                    pasithee Level 1

                                    Hi Jongware

                                     

                                    thanks for your help. I changed what you wrote but still doesn't work.


                                    here my script

                                        var win = new Window('dialog','Reformat Index');
                                    
                                        win.orientation = "row";
                                        win.spacing = 0;
                                            
                                        var group = win.add("group");
                                        win.okButton = group.add("button", undefined, "start");
                                        win.okButton.onClick = function() { test() };
                                        
                                        win.canButton = group.add("button", undefined, "Cancel");
                                        win.canButton.onClick = function() { this.window.close(2) }; 
                                        
                                        win.center();
                                        win.show();
                                      
                                    
                                    function test() {
                                        
                                       for(var i = 0; i < app.selection[0].parentStory.paragraphs.length; i++) {
                                            app.selection[0].parentStory.paragraphs[0].contents = "ABC\r";
                                       }
                                    }
                                    

                                     

                                    Does this script works in you InDesign? Could it be a issue in CS5?

                                     

                                    Thanks

                                    pasithee

                                    • 15. Re: change text
                                      [Jongware] Most Valuable Participant

                                      I'm afraid this has gone beyond me ... Anyone else? Please?

                                       

                                      This function 'test' does pop up an alert containing the text of the first paragraph, so that part works just fine. As you have shown, it also works when called directly -- but as soon as you put it into a dialog it shows the alert but stubbornly refused to change the text!

                                       

                                      ... It must have something to do with ScriptUI, but I'm flummoxed, stymied, and generally shut up.

                                       

                                       

                                      function test() {
                                         for(var i = 0; i < app.selection[0].parentStory.paragraphs.length; i++) {
                                      
                                              var start = app.selection[0].parentStory.paragraphs.item(i).characters.item(0);
                                              var end = app.selection[0].parentStory.paragraphs.item(i).characters.item(-2);
                                      
                                              sel = app.selection[0].parentStory.paragraphs[i].characters.itemByRange(start, end);
                                              if (!i) alert (sel.contents);
                                              sel.contents = "A";
                                         }
                                      }
                                       
                                      

                                      • 16. Re: change text
                                        Marijan Tompa [tomaxxi] Level 4

                                        Well, in my case (CS5) it's not working also... But why?! It gives me error when script wants to change contents of paragraph. Error: "Cannot handle the request because a modal dialog or alert is active." So, you can't do changes while your dialog is active. So, you have two solutions. First is to close dialog, make changes and then reopen dialog, or to use 'palette' instead of 'dialog'. To use 'palette' add 'targetengine' on beginning of your script and change 'dialog' to 'palette' in window creation row.

                                         

                                        #targetengine textChange
                                        var win = new Window('palette','Reformat Index');
                                        

                                         

                                        Hope it helps.

                                         

                                        --

                                        tomaxxi

                                        http://indisnip.wordpress.com/

                                        • 17. Re: change text
                                          pasithee Level 1

                                          once again. thanks a lot tomaxxi that was the right answer/hint.

                                           

                                          cheers

                                          pasithee

                                          • 18. Re: change text
                                            Marijan Tompa [tomaxxi] Level 4

                                            You are welcome!

                                             

                                            Glad it worked!

                                             

                                            --

                                            tomaxxi

                                            http://indisnip.wordpress.com/