36 Replies Latest reply on Nov 17, 2016 10:51 PM by Jump_Over

    Positioning Inline images? [CS3 JS WIN]

      I have an inline graphic in a line of text. I want to be able to automatically position it in the vertical center of the line, but i can't find anyway to do this in javascript. Ive tried geometric and visible bounds to no avail. Ive also tried looking for a way to change it in indesign, but it only seems to be possible by dragging it with the mouse. Any ideas?
        • 1. Re: Positioning Inline images? [CS3 JS WIN]
          Level 1
          Use a move.

          Something like this:

          myGBs = myInline.geometricBounds;
          myCenterY = (myGBs[2] + myGBs[0])/2;
          myBaseline = myInline.parent.baseline;
          myInline.move(undefined, [0, myBaseline - myCenterY);

          Dave
          • 2. Re: Positioning Inline images? [CS3 JS WIN]
            Bullitt667 Level 1
            Dave

            I have a related problem. I'm not familiar with scripts but need one that would search a story and, e.g., replace the text "Pencil.tif" with a graphic file named Pencil.tif from a given folder.

            Can you help with this?

            Thanks,

            Tom
            • 3. Re: Positioning Inline images? [CS3 JS WIN]
              Kasyan Servetsky Level 5
              >need one that would search a story and, e.g., replace the text "Pencil.tif" with a graphic file named Pencil.tif from a given folder.

              I've found in my archive a script written by Peter Kahrel (I modified it a little). It finds text between two @ characters – e.g. @Pencil.tif@ – and replaces it with image that has the same name and is in the root folder of C drive.
              // reset the Find/Change dialog
              
              app.findGrepPreferences = app.changeGrepPreferences = null;
              // formulate a grep search string
              app.findGrepPreferences.findWhat = '@.+?@';
              // find all occurrence, last one first
              f = app.activeDocument.findGrep (true);
              for (i = 0; i < f.length; i++)
                 {
                 // construct file name
                 name = f[i].contents.replace (/@/g, '');
                 // place the pdf
                 f[i].insertionPoints[0].place (File ('/c/' + name));
                 }
              // delete all @??@ codes
              app.activeDocument.changeGrep()
              • 4. Re: Positioning Inline images? [CS3 JS WIN]
                Bullitt667 Level 1
                Kasyan

                Forgot to mention I'm working in original CS version. Grep probably won't work?

                Tom
                • 5. Re: Positioning Inline images? [CS3 JS WIN]
                  Kasyan Servetsky Level 5
                  >Grep probably won't work?

                  Certainly it won't – Grep is supported since CS3.
                  • 7. Re: Positioning Inline images? [CS3 JS WIN]
                    Harbs. Level 6
                    You can get the same result in CS2, but it's a lot more convoluted.

                    The basic idea is to build an array of the exact file name strings. To
                    do this you use js RegEx to search the contents of every story. The
                    results of this match() will give you the strings you need. You then
                    iterate through your array and do a search() on your doc to do the
                    replacements.

                    I think Peter's book on scripting InDesign has examples of this method.

                    --
                    Harbs
                    http://www.in-tools.com
                    • 8. Re: Positioning Inline images? [CS3 JS WIN]
                      Peter Kahrel Adobe Community Professional & MVP
                      The good old days, eh, Harbs? Here's a CS2 version. It skips footnotes (because you can't do this kind of thing in footnotes in CS2) and tables.

                      Peter

                      #target indesign
                      

                      picture_dir = '/d/'
                      app.findPreferences = app.changePreferences = null;
                      myStories = app.activeDocument.stories;
                      for (i = 0; i < myStories.length; i++)
                         {
                         picnames = myStories[i].contents.match (/@.+?@/g);
                         if (picnames != null)
                            {
                            for (j = 0; j < picnames.length; j++)
                               {
                               found = app.activeDocument.search (picnames[j],false,false);
                               for (k = found.length-1; k > -1; k--)
                                  {
                                  try
                                     {
                                     filename = found[k].contents.replace (/@/g, "");
                                     found[k].insertionPoints[-1].place (File (picture_dir + filename));
                                     found[k].remove();
                                     }
                                  catch(_){}
                                  }
                               }
                            }
                         }
                      • 9. Re: Positioning Inline images? [CS3 JS WIN]
                        Harbs. Level 6
                        Peter Kahrel wrote:
                        > The good old days, eh, Harbs?

                        Yeah. ;) My problem is, that I still need to support CS2, so I'm still
                        stuck using these backwards methods on occasion. :(

                        --
                        Harbs
                        http://www.in-tools.com
                        • 10. Re: Positioning Inline images? [CS3 JS WIN]
                          Level 1
                          Took a little messing around with my current situation (its not always centered - sometimes a little off) but the move worked perfectly!

                          Thank you Dave
                          • 11. Re: Positioning Inline images? [CS3 JS WIN]
                            Bullitt667 Level 1
                            Kasyan Servetsky

                            I've upgraded to CS4 and need a script to replace "filename.tif" with a photo of the same name inline.

                            Do you have a script that will work in InDesign CS4?

                            Thanks,
                            • 12. Re: Positioning Inline images? [CS3 JS WIN]
                              Kasyan Servetsky Level 5
                              I don’t have CS4 version at my disposal right now, so I can’t check if this script works with it. But even if it doesn’t, placing the script into ‘Version 5.0 Scripts’ folder should make it compatible with CS4.

                              Kasyan
                              • 13. Re: Positioning Inline images? [CS3 JS WIN]
                                Bullitt667 Level 1
                                Kasyan

                                Where would I find this "version 5.0 scripts" folder? It doesn't appear in the InDesign/Scripts folder.
                                • 14. Re: Positioning Inline images? [CS3 JS WIN]
                                  Level 1
                                  You make it. And the capitalization of the name is vital. It must be named:

                                  Version 5.0 Scripts

                                  not "version 5.0 scripts" as you wrote.

                                  Dave
                                  • 15. Re: Positioning Inline images? [CS3 JS WIN]
                                    Kasyan Servetsky Level 5
                                    Create it yourself – it isn't installed by default.
                                    The exact path should be:
                                    Adobe InDesign CS4\Scripts\Scripts Panel\Version 5.0 Scripts
                                    • 16. Re: Positioning Inline images? [CS3 JS WIN]
                                      Bullitt667 Level 1
                                      Okay I did as you suggested, but get this error message:
                                      Windows Scripting Error!
                                      Error Number: 1024
                                      Error String: Expected Statement

                                      File: C\Program Files\Adobe\Adobe InDesign CS4\Scripts\Scripts Panel\Version 5.0 Scripts\InlinePix.vbs
                                      Line: 1
                                      Source://reset the Find/Change dialog
                                      Interpreter: CS3 (5.0)

                                      Any help?
                                      • 17. Re: Positioning Inline images? [CS3 JS WIN]
                                        Kasyan Servetsky Level 5
                                        This script is written in JavaScript(jsx extension), but you saved it as VBscript (vbs extension). So no wonder that it doesn't work.
                                        Copy code, start ExtendScript Toolkit, create a new file, paste the code and save the file.
                                        • 18. Re: Positioning Inline images? [CS3 JS WIN]
                                          Bullitt667 Level 1
                                          I had saved it as a jsx after going thru the ExtendScript. It just sits there and does nothing apparent.

                                          Sorry, but I'm a dummy with scripts.
                                          • 19. Re: Positioning Inline images? [CS3 JS WIN]
                                            Kasyan Servetsky Level 5
                                            Aha, I see! The script (post 3) expects that the files to be placed are stored in the root folder of C drive. So you should change the line 12 to where you store your files e.g. suppose folder called ‘Images’ on your Desktop:
                                            f[i].insertionPoints[0].place (File ('~/Desktop/Images/' + name));
                                            
                                            • 20. Re: Positioning Inline images? [CS3 JS WIN]
                                              Kasyan Servetsky Level 5
                                              Or, as an option, the script can ask you to choose the folder:
                                              if(app.documents.length != 0){
                                              
                                                var myFolder = Folder.selectDialog ('Choose a folder with images');
                                                if(myFolder != null){
                                                    // reset the Find/Change dialog
                                                    app.findGrepPreferences = app.changeGrepPreferences = null;
                                                    // formulate a grep search string
                                                    app.findGrepPreferences.findWhat = '@.+?@';
                                                    // find all occurrence, last one first
                                                    f = app.activeDocument.findGrep (true);
                                                    for (i = 0; i < f.length; i++)
                                                       {
                                                       // construct file name
                                                       name = f[i].contents.replace (/@/g, '');
                                                       // place the pdf
                                                       f[i].insertionPoints[0].place (myFolder.fsName + '/' + name);
                                                       }
                                                    // delete all @??@ codes
                                                    app.activeDocument.changeGrep()
                                                }
                                              }
                                              else{
                                                 alert('Please open a document and try again.');
                                              }
                                              1 person found this helpful
                                              • 21. Re: Positioning Inline images? [CS3 JS WIN]
                                                Bullitt667 Level 1
                                                Kasyan

                                                I used your suggestion in post 20 and got:
                                                JavaScript Error
                                                Error #23
                                                Error String: does not have a value
                                                File: C:\Program Files\Adobe\Adobe InDesign CS4\Scripts\Scripts Panel\Version 5.0 Scripts\Source1.jsx
                                                Source: //reset the Find/Change dialog app.findGrepPreferences = app.changeGrepPreferences = null /formulate a grep search string app.find GrepPreferences , findWhat = '@,+@'; . . .

                                                etcera(Tom)
                                                • 22. Re: Positioning Inline images? [CS3 JS WIN]
                                                  Kasyan Servetsky Level 5
                                                  May be something went wrong while coping/pasting – I sent the script to your e-mail in attachment.
                                                  • 23. Re: Positioning Inline images? [CS3 JS WIN]
                                                    Bullitt667 Level 1
                                                    Kasyan

                                                    Thanks for your help. I sent you an email back with the latest error message.

                                                    Tom
                                                    • 24. Re: Positioning Inline images? [CS3 JS WIN]
                                                      Oriup Level 1

                                                      Dear Kasyan,

                                                       

                                                      I have problem,

                                                       

                                                      In my document there is more than 100 images name, when I run your script I got an error because in my directory some images is different location/path.

                                                       

                                                      Regards,

                                                      Sumit

                                                      • 25. Re: Positioning Inline images? [CS3 JS WIN]
                                                        Kasyan Servetsky Level 5

                                                        I just tested the script (I posted in post 20) in InDesign 2015.4 and it works for me in the same way as it worked in CS3.

                                                        Before

                                                        11-7-2016 6-59-33 PM.png

                                                        After

                                                        11-7-2016 7-00-04 PM.png

                                                        This script doesn't care about the existing images and their locations. It only places new images using the names of files found in between a pair of '@' characters and the selected folder for images.

                                                        — Kas

                                                        • 26. Re: Positioning Inline images? [CS3 JS WIN]
                                                          Oriup Level 1

                                                          Dear Kasyan,

                                                           

                                                          Thank you for quick response.

                                                           

                                                          Your script is great!

                                                           

                                                          My problem is, if some images are not found in selected directory then pop up error massage and stop executing next.

                                                           

                                                          Below is my scenario,  but my document contains more than 100 image names and in my directory there is less than 100 images found then stop executing script.

                                                           

                                                          I hope you understand my problem, my english is not good.

                                                          Screen Shot 2016-11-08 at 7.17.25 am.pngScreen Shot 2016-11-08 at 7.17.41 am.pngScreen Shot 2016-11-08 at 7.20.02 am.png

                                                           

                                                          Thanks,

                                                          Sumit

                                                          • 27. Re: Positioning Inline images? [CS3 JS WIN]
                                                            Harbs. Level 6

                                                            This should do it:

                                                             

                                                            if(app.documents.length != 0){
                                                              var myFolder = Folder.selectDialog ('Choose a folder with images');
                                                              if(myFolder != null){
                                                                  // reset the Find/Change dialog
                                                                  app.findGrepPreferences = app.changeGrepPreferences = null;
                                                                  // formulate a grep search string
                                                                  app.findGrepPreferences.findWhat = '@.+?@';
                                                                  // find all occurrence, last one first
                                                                  var f = app.activeDocument.findGrep (true);
                                                                  var missedFiles = [];
                                                                  for (var i = 0; i < f.length; i++){
                                                                     // construct file name
                                                                     name = f[i].contents.replace (/@/g, '');
                                                                     // place the pdf
                                                                     try{
                                                                       f[i].insertionPoints[0].place (myFolder.fsName + '/' + name);
                                                                     }catch(err){
                                                                      missedFiles.push(name);
                                                                     }
                                                                    }
                                                                  // delete all @??@ codes
                                                                  app.activeDocument.changeGrep();
                                                                  if(missedFiles.length){
                                                                    alert("The following files could not be found: " + missedFiles.join(", "));
                                                                  }
                                                              }
                                                            }
                                                            else{
                                                               alert('Please open a document and try again.');
                                                            }
                                                            
                                                            1 person found this helpful
                                                            • 28. Re: Positioning Inline images? [CS3 JS WIN]
                                                              Kasyan Servetsky Level 5

                                                              Harbs outstripped me! Here's my version. It writes the list of missing images to a txt-file on the desktop.

                                                              main();
                                                              
                                                              function main() {
                                                                  var name, f, file, text,
                                                                  arr = [];
                                                                  
                                                                  if(app.documents.length != 0) {
                                                                      var doc = app.activeDocument;    
                                                                      var folder = Folder.selectDialog("Choose a folder with images");
                                                              
                                                                      if (folder != null) {
                                                                          app.findObjectPreferences = app.changeObjectPreferences = NothingEnum.NOTHING;
                                                                          app.findGrepPreferences.findWhat = "@.+?@";
                                                                          f = doc.findGrep(true);
                                                                          
                                                                          for (i = 0; i < f.length; i++) {
                                                                              name = f[i].contents.replace(/@/g, "");
                                                                              file = new File(folder.fsName + "/" + name);
                                                                              
                                                                              if (file.exists) {
                                                                                  f[i].insertionPoints[0].place(file);
                                                                              }
                                                                              else {
                                                                                  arr.push("File doesn't exist '" + name + "'");
                                                                              }
                                                                          }
                                                                      
                                                                          doc.changeGrep();
                                                                          arr.push("------------------------------------------");
                                                                          text = arr.join("\r");
                                                                          writeToFile(text);
                                                                      }
                                                                  }
                                                                  else{
                                                                      alert("Please open a document and try again.");
                                                                  }    
                                                              }
                                                              
                                                              function writeToFile(text) {
                                                                  var file = new File("~/Desktop/Place inline images.txt");
                                                                  if (file.exists) {
                                                                      file.open("e");
                                                                      file.seek(0, 2);
                                                                  }
                                                                  else {
                                                                      file.open("w");
                                                                  }
                                                                  file.write(text + "\r"); 
                                                                  file.close();
                                                              }
                                                              
                                                              1 person found this helpful
                                                              • 29. Re: Positioning Inline images? [CS3 JS WIN]
                                                                Oriup Level 1

                                                                Dear Kasyan and Harbs,

                                                                 

                                                                Thank you so much both of you.

                                                                 

                                                                You guys understood my problem.

                                                                 

                                                                Now, problem is why deleted those names that was not found.

                                                                 

                                                                If deleted those text from indesign file, it will be big problem for me, how to search where to insert images.

                                                                 

                                                                Thanks,

                                                                Sumit

                                                                • 30. Re: Positioning Inline images? [CS3 JS WIN]
                                                                  Kasyan Servetsky Level 5

                                                                  Now, problem is why deleted those names that was not found.

                                                                  If deleted those text from indesign file, it will be big problem for me, how to search where to insert images.

                                                                  Let's try this version now:

                                                                  main();
                                                                  
                                                                  function main() {
                                                                      var name, f, file, text,
                                                                      arr = [];
                                                                      
                                                                      if(app.documents.length != 0) {
                                                                          var doc = app.activeDocument;    
                                                                          var folder = Folder.selectDialog("Choose a folder with images");
                                                                  
                                                                          if (folder != null) {
                                                                              app.findObjectPreferences = app.changeObjectPreferences = NothingEnum.NOTHING;
                                                                              app.findGrepPreferences.findWhat = "@.+?@";
                                                                              f = doc.findGrep(true);
                                                                              
                                                                              for (i = 0; i < f.length; i++) {
                                                                                  name = f[i].contents.replace(/@/g, "");
                                                                                  file = new File(folder.fsName + "/" + name);
                                                                                  
                                                                                  if (file.exists) {
                                                                                      f[i].remove();
                                                                                      f[i].insertionPoints[0].place(file);
                                                                                  }
                                                                                  else {
                                                                                      arr.push("File doesn't exist '" + name + "'");
                                                                                  }
                                                                              }
                                                                          
                                                                              arr.push("------------------------------------------");
                                                                              text = arr.join("\r");
                                                                              writeToFile(text);
                                                                          }
                                                                      }
                                                                      else{
                                                                          alert("Please open a document and try again.");
                                                                      }    
                                                                  }
                                                                  
                                                                  function writeToFile(text) {
                                                                      var file = new File("~/Desktop/Place inline images.txt");
                                                                      if (file.exists) {
                                                                          file.open("e");
                                                                          file.seek(0, 2);
                                                                      }
                                                                      else {
                                                                          file.open("w");
                                                                      }
                                                                      file.write(text + "\r"); 
                                                                      file.close();
                                                                  }
                                                                  

                                                                   

                                                                  Before

                                                                  11-8-2016 12-35-29 PM.png

                                                                  After

                                                                  11-8-2016 12-35-17 PM.png

                                                                  11-8-2016 12-36-08 PM.png

                                                                  — Kas

                                                                  1 person found this helpful
                                                                  • 31. Re: Positioning Inline images? [CS3 JS WIN]
                                                                    Oriup Level 1

                                                                    Dear Kasyan,

                                                                     

                                                                    You are awesome.

                                                                     

                                                                    Just asking one thing to you, why do you clear objectPreferences?

                                                                     

                                                                    I think, GrepPreferences should be clear.

                                                                     

                                                                    And everything is good.

                                                                     

                                                                    Nice working.

                                                                     

                                                                    Thank you so much.

                                                                     

                                                                    Sumit

                                                                    • 32. Re: Positioning Inline images? [CS3 JS WIN]
                                                                      Kasyan Servetsky Level 5

                                                                      Just asking one thing to you, why do you clear objectPreferences?

                                                                      It's necessary to clean up the prefs because InDesign may 'remember' settings from the previous search-replace operation and you would get unexpected results. It's also a good idea to do this after the search is complete to keep things tidy: so that the user wouldn't see the last used settings in the find-change dialog box, but that's optional.

                                                                       

                                                                      — Kas

                                                                      • 33. Re: Positioning Inline images? [CS3 JS WIN]
                                                                        Laubender Adobe Community Professional & MVP

                                                                        Hi Kasyan,

                                                                        Sumit is wondering why line 12 of your script is reading:

                                                                         

                                                                        app.findObjectPreferences = app.changeObjectPreferences = NothingEnum.NOTHING;

                                                                         

                                                                        and not:

                                                                         

                                                                        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;

                                                                         

                                                                        Just a typo, I think…

                                                                         

                                                                        Regards,
                                                                        Uwe

                                                                        • 34. Re: Positioning Inline images? [CS3 JS WIN]
                                                                          Kasyan Servetsky Level 5

                                                                          Hi Uwe,

                                                                           

                                                                          Oops! Of course, this is a typo. Thanks for pointing this out. Please don't judge me harshly: I became so absentminded for last year.

                                                                          Here's a fixed version:

                                                                          main();  
                                                                            
                                                                          function main() {  
                                                                              var name, f, file, text,  
                                                                              arr = [];  
                                                                                
                                                                              if(app.documents.length != 0) {  
                                                                                  var doc = app.activeDocument;      
                                                                                  var folder = Folder.selectDialog("Choose a folder with images");  
                                                                            
                                                                                  if (folder != null) {  
                                                                                      app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;  
                                                                                      app.findGrepPreferences.findWhat = "@.+?@";  
                                                                                      f = doc.findGrep(true);  
                                                                                        
                                                                                      for (i = 0; i < f.length; i++) {  
                                                                                          name = f[i].contents.replace(/@/g, "");  
                                                                                          file = new File(folder.fsName + "/" + name);  
                                                                                            
                                                                                          if (file.exists) {  
                                                                                              f[i].remove();  
                                                                                              f[i].insertionPoints[0].place(file);  
                                                                                          }  
                                                                                          else {  
                                                                                              arr.push("File doesn't exist '" + name + "'");  
                                                                                          }  
                                                                                      } 
                                                                                  
                                                                                      app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;  
                                                                          
                                                                                      arr.push("------------------------------------------");  
                                                                                      text = arr.join("\r");  
                                                                                      writeToFile(text);  
                                                                                  }  
                                                                              }  
                                                                              else{  
                                                                                  alert("Please open a document and try again.");  
                                                                              }      
                                                                          }  
                                                                            
                                                                          function writeToFile(text) {  
                                                                              var file = new File("~/Desktop/Place inline images.txt");  
                                                                              if (file.exists) {  
                                                                                  file.open("e");  
                                                                                  file.seek(0, 2);  
                                                                              }  
                                                                              else {  
                                                                                  file.open("w");  
                                                                              }  
                                                                              file.write(text + "\r");   
                                                                              file.close();  
                                                                          } 
                                                                          

                                                                           

                                                                          — Kas

                                                                          1 person found this helpful
                                                                          • 35. Re: Positioning Inline images? [CS3 JS WIN]
                                                                            Kasyan Servetsky Level 5

                                                                            I also posted the script here on my site.

                                                                            • 36. Re: Positioning Inline images? [CS3 JS WIN]
                                                                              Jump_Over Level 5

                                                                              Absentminded but still effective.

                                                                              Dont judge yourself harshly, Kasyan:)

                                                                               

                                                                              rgds

                                                                              Jarek