16 Replies Latest reply on Mar 12, 2014 3:26 AM by The Gothamite

    Move URLs in Text to End of Story Or Link Text. Also: Tables

    The Gothamite Level 1

      Hello everyone

       

      I'm currently working on a script that translates Textile Markup into formatted InDesign-Text. This worked fine for the most part, seeing as it's not really hard to do by FindChangeByList. Some things are a bit trickier than other things, though. Namely URLs in the text. In Textile, all URLs are like so. Let's say I'd like to link to Google. So I'd type this: "Google":http://www.google.com This would produce Google. I can isolate the word and delete the URL by [\\\"](.+?)[\\\"]:\\?\\S+ and then replacing by $1. So far so good.

       

      However!

       

      It would be awesome, if the URLs were still in the text. So I'd either like to have it like so: Google or that the word Google (or any $1, really) stands in the text and then the link is added to the end of the text as a kind of "Sources"-paragraph-thing. How can this be done? Can I do it by FindChangeByList? Because that would be cool, seeing as then I'd only need to run one script.

       

      And here's a general question: How can I translate Textile-Tables into Formatted InDesign-Tables?

       

      For reference, this would be a table with one header row.

       

      |_. name |_. age |_. sex |

      | joan | 24 | f |

      | archie | 29 | m |

      | bella | 45 | f |

       

      Can this be done?

       

      Thank you very much for your help!

        • 1. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
          Jump_Over Level 5

          Hi,

           

          In case of translating to table for mText (if it refers to your example) use this:

          mText.convertToTable("|","\r");
          

           

           

          In case of URLs in text you can not use find...replace method, I am afraid.

          With more info it could be done with some "move_function ()" or "hyperlink_function ()" which can be involved after doc.findGrep() prepares an array with parts of texts.

           

          Jarek

          • 2. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
            The Gothamite Level 1

            Thanks, Jarek

             

            I don't think I'll have the time to test this today, but I'll get around to it. I had one odd thought, though, thinking along the lines of Find/Replace again. What if I did this?

             

            Find: [\\\"](.+?)[\\\"]:(http\\S+)

            Replace: $1 .+ $2

             

            Or something along those lines in general? Would that work?

            • 3. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
              Jump_Over Level 5

              Hi,

               

              You cant use wildcards (".+") for Replace, at sure.

               

              I am still not sure about your goal but you could test something alike:

              find: (...)(...)(...)     word - url - rest_of_para

              replace: $1$3$2

               

              Jarek

              • 4. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
                The Gothamite Level 1

                Hey Jarek

                 

                Bascially what I want to do is move actual URLs around. Let's say I have a text, written in Textile Markup. Like so:

                 

                Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur "Google":http://google.com sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

                 

                See that URL to Google in there? I want to make the text look like one of the following.

                 

                Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur Google elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

                 

                Read also

                - www.google.com

                or

                 

                Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur Google sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

                • 5. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
                  Jump_Over Level 5

                  Hi,

                   

                  1st goal, using your way:

                  var mDoc = app.activeDocument;
                  app.findGrepPreferences = app.changeGrepPreferences = null;
                  app.findGrepPreferences.findWhat = "[\\\"](.+?)[\\\"]:(http\\\S+)(.+)$";
                  app.changeGrepPreferences.changeTo = "$1$3\r\rRead also:\r- $2";
                  mDoc.changeGrep();
                  

                   

                  2nd goal needs more steps, I guess.

                  This effect:

                  ...

                  consetetur Google sadipscing

                  ...

                  means to add hyperlink to your doc, but it can be shown if doc exported to, i.e., interactive PDF - is it your goal?

                   

                  Jarek

                  • 6. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
                    The Gothamite Level 1

                    That is indeed my goal. The final product is a PDF that I'm sending out via e-mail. It will never be printed. So both ways work. So, actually, the second solution would probably be the best.

                    • 7. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
                      Jump_Over Level 5

                      Hi,

                       

                      for 2nd goal try this:

                      app.findGrepPreferences = app.changeGrepPreferences = null;
                      app.findGrepPreferences.findWhat = "[\\\"](.+?)[\\\"]:(http\\\S+)";
                      app.changeGrepPreferences.changeTo = "$1\t$2";
                      var 
                           mDoc = app.activeDocument,
                           mRes = mDoc.findGrep(),
                           len = mRes.length,
                           mText, mSource, mDest, currHyp;
                      
                      while (len-->0) {
                           mText = mRes[len].changeGrep();
                           mSource = mDoc.hyperlinkTextSources.add(mText[0].words[0].texts[0]);
                           mDest = mDoc.hyperlinkURLDestinations.add(mText[0].words[1].contents);
                           currHyp = mDoc.hyperlinks.add(mSource, mDest);
                           mText[0].words[-1].remove();
                           mText[0].contents = mText[0].contents.replace("\t","");
                          } 
                      app.findGrepPreferences = app.changeGrepPreferences = null;
                      

                       

                      Jarek

                      • 8. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
                        The Gothamite Level 1

                        Wow, this works like a charm. You really are awesome at this. Also, sorry I was away, I suddenly found myself swamped in work and didn't have time to attend to this thing which isn't due until the 17th.

                         

                        There's one little flaw in this that I can see. Links in our writings can look something like this: "Try it out on Google":www.google.com and of course, if you're going "Link first word to second word" then that won't really work. Any ideas on how I can tackle that? Also, I'd like to apply a characterstyle to the finished hyperlink. Can this be done?

                         

                        EDIT: I just managed to apply a character style. I'm a twit. The script now begins with

                         

                        function textile_link(){

                        app.findGrepPreferences = app.changeGrepPreferences = null;

                        app.findGrepPreferences.findWhat = "[\\\"](.+?)[\\\"]:(http\\\S+)";

                        app.changeGrepPreferences.changeTo = "$1\t$2";

                        app.changeGrepPreferences.appliedCharacterStyle = "URL";

                         

                        and the rest is (thus far) identical to yours.

                         

                        EDIT II: I've looked at what I need from a sort of general standpoint: I need the script to make a hyperlink that displays as $1 and links to $2,

                        • 9. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
                          Jump_Over Level 5

                          Hi,

                           

                          OK. If you can assume that $2 is  exactly one word ==> we could modify above like this:

                           

                          //...
                          mSource = mDoc.hyperlinkTextSources.add(mText[0].words.itemByRange(0,-2).texts[0]);
                          mDest = mDoc.hyperlinkURLDestinations.add(mText[0].words[-1].contents);
                          //...
                          

                           

                           

                          Jarek

                          • 10. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
                            The Gothamite Level 1

                            That's it! You are amazing! And yes, our URLs are always the same and they always work the same. I will have to figure out a way to have the internal links be parsed into full URLs, though. I guess that would be a second function, though, that just prefixes a different find.

                             

                            Like:

                            function textile_link(){

                            app.findGrepPreferences = app.changeGrepPreferences = null;

                            app.findGrepPreferences.findWhat = "[\\\"](.+?)[\\\"]:(?\\\S+)";

                            app.changeGrepPreferences.changeTo = "$1\twww.oururl.com$2";

                            app.changeGrepPreferences.appliedCharacterStyle = "URL";

                             

                            That should work.

                             

                            Anyways, I'm now working on the table-maker. I have a few  questions for that one as well.

                             

                            Why does it convert every single thing into a table? That's not quite ideal. Can right now, I have this table that I want to convert

                             

                            |_. name |_. age |_. sex |

                            | joan | 24 | f |

                            | archie | 29 | m |

                            | bella | 45 | f |

                             

                            The final result should be a 3x3 table.

                             

                            So I first have to remove the | at the beginning and end of every line. Easy.

                             

                            app.findGrepPreferences.findWhat = "^\\|(.+)\\|$";

                            app.changeGrepPreferences.changeTo = "$1"

                             

                            Then convert to table.

                             

                            But the problem is this: My text looks like this

                             

                            Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.

                            |_. name |_. age |_. sex |

                            | joan | 24 | f |

                            | archie | 29 | m |

                            | bella | 45 | f |

                            Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.

                             

                            Needless to say, I want the Loremipsums to not be a table but regular text.

                             

                            And can I apply a tablestyle to the table it's creating? Because the top line is a header line, the rest is regular lines.

                             

                            EDIT: The second script with the internal links worked flawlessly.

                            • 11. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
                              Jump_Over Level 5

                              Hi,

                               

                              If your target text can be named mTarget (1 table inside):

                               

                              app.findTextPreferences = app.changeTextPreferences = null;
                              app.findTextPreferences.findWhat = "|";
                              mFound = mTarget.findText();
                              startPara = mFound[0].paragraphs[0];
                              endPara = mFound[mFound.length-1].paragraphs[0];
                              //...
                              //... here remove "|" from the begin and end of lines
                              //...
                              mTarget = mTarget.paragraphs.itemByRange(startPara,endPara).texts[0];
                              //... here mTarget is ready to convert to a table
                              

                               

                              Jarek

                               

                              PS. If some post were helpful (or possibly even correct?) ==> mark it for the rest of forum, pls

                              • 12. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
                                The Gothamite Level 1

                                Marked as correct: Done! Thanks for letting me know. Because I hope I'm not the only one looking for a solution to this.

                                 

                                Anyways, I get a mTarget is undefined using your script. And I'm really, really confused by this. I mean, I could follow the rest of your advice without a problem thus far, but this has me completely stumped. So I apologize if I come across as stupid.

                                 

                                Also, is there a way I can apply a paragraph-style to the table I'm creating?

                                 

                                A follow-up question that is coming up on the horizon will be whether or not I can add a column to a paragraphs with numbers from 01 to n, whereas n is a varying number of lines.

                                • 13. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
                                  Jump_Over Level 5

                                  Hi,

                                   

                                  Above code IS NOT a script but just a part of it to ilustrate a way.

                                  It assumes that var mTarget is declared earlier (as a story or selected text which covers a source of table).

                                   

                                  To format your table with chosen para style ==> apply it to mTarget before table convertion:

                                  mTarget.appliedParagraphStyle = mDoc.paragraphStyles.item("style_name");
                                  

                                   

                                  To add a column is easy (just dont remove "|" from the begin of lines).

                                   

                                  To fill it with numbers is another story ==> it depends on your exact goal and info is too poor for now.

                                   

                                  Jarek

                                  • 14. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
                                    The Gothamite Level 1

                                    I have one more question regarding the link-generating script. Is there a way to apply it to only the current selection as opposed to the entire document?

                                     

                                    Also: Why does InDesign lack any and all understanding for Breakpoints?

                                    • 15. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
                                      Jump_Over Level 5

                                      Hi,

                                       

                                      mRes = mDoc.findGrep(), ==> here is target defined.

                                      mRes = app.selection[0].findGrep(), ==> works inside selected text only (select text)

                                       

                                      The Gothamite wrote:

                                      ...

                                      Also: Why does InDesign lack any and all understanding for Breakpoints?

                                       

                                      Sorry, could you explain it deeply, pls.

                                       

                                      Jarek

                                      1 person found this helpful
                                      • 16. Re: Move URLs in Text to End of Story Or Link Text. Also: Tables
                                        The Gothamite Level 1

                                        This works like a charm. You're amazing. For all those interested, here's the entire code of the function:

                                         

                                        function textile_link(){

                                         

                                        app.findGrepPreferences = app.changeGrepPreferences = null; //Resets Find/Replace

                                         

                                        var myDocument = app.selection[0];

                                            ResetFind ()

                                            app.findChangeGrepOptions.includeFootnotes = false;

                                            app.findChangeGrepOptions.includeHiddenLayers = false;

                                            app.findChangeGrepOptions.includeLockedLayersForFind = false;

                                            app.findChangeGrepOptions.includeLockedStoriesForFind = false;

                                            app.findChangeGrepOptions.includeMasterPages = false;

                                           

                                         

                                        app.findGrepPreferences = app.changeGrepPreferences = null;

                                        app.findGrepPreferences.findWhat = "[\\\"](.+?)[\\\"]:(http\\\S+)";

                                        app.changeGrepPreferences.changeTo = "$1\t$2";

                                        app.changeGrepPreferences.appliedCharacterStyle = "Hyperlink";

                                        var

                                             mRes = app.selection[0].findGrep(),

                                             len = mRes.length,

                                             mDoc = app.activeDocument,

                                             mText, mSource, mDest, currHyp;

                                         

                                         

                                        while (len-->0) {

                                             mText = mRes[len].changeGrep();

                                             mSource = mDoc.hyperlinkTextSources.add(mText[0].words[0].texts[0]);

                                             mDest = mDoc.hyperlinkURLDestinations.add(mText[0].words[-1].contents);

                                             currHyp = mDoc.hyperlinks.add(mSource, mDest);

                                             mText[0].words[-1].remove();

                                             mText[0].contents = mText[0].contents.replace("\t","");

                                            }

                                        app.findGrepPreferences = app.changeGrepPreferences = null;

                                        }

                                         

                                        I don't claim credit for this code. Everything was done by the amazing Jump_Over. So all props to him.