11 Replies Latest reply on Nov 17, 2018 3:29 PM by Colin Flashman

    Auto Link in InDesign

    gupta.amit1981 Level 1

      Hi Experts,

      I need a script which needs automatically place as inline image wherever specific text as XX.eps, XX.ai, come in the document. I have exactly environment as below link. Although, this is a paid script but didn't afford at my end.


      I have 250+ images to link in the docs.

      Thanks very much in advance.


        • 1. Re: Auto Link in InDesign
          S Hopkins Adobe Community Professional

          Here is a simple AppleScript that can get you started. Note that the regular expression (grep) only allows regular characters and an underscore. Spaces or special characters will require a more robust expression.


          --simple regEx expression will not work if spaces, hyphens or special characters allowed in filenames

          set findStr to "\\b([A-Za-z0-9_]+\\.ai|[0-9A-Za-z_]+\\.eps)\\b"

          --define path to folder of graphics

          set fileDest to path to desktop from user domain as string

          set folderDest to fileDest & "graphics"

          tell application "Adobe InDesign CC 2018"

            --simple fine grep

            set find grep preferences to nothing

            set find what of find grep preferences to findStr

            --define find change grep options here

            tell find change grep options

            set include hidden layers to true

            set include locked layers for find to true

            end tell

            set docRef to document 1

            set theStory to story 1 of docRef

            tell theStory

            set foundList to find grep

            end tell

            set testList to {}

            if length of foundList > 0 then

            set find grep preferences to nothing

            set testList to {}

            with timeout of 300 seconds

            repeat with i from length of foundList to 1 by -1

            set thisItem to item i of foundList


            set filePath to (folderDest & ":" & contents of thisItem)

            set fileRef to filePath as alias

            tell insertion point 1 of thisItem

            place fileRef

            end tell

            on error

            set end of testList to filePath

            end try

            end repeat

            end timeout

            --return to default

            set find grep preferences to nothing

            end if

          end tell

          {foundList, testList}

          • 2. Re: Auto Link in InDesign
            gupta.amit1981 Level 1

            Hi Hopkins,


            Thanks for your reply and sorry to late reply from my side due to on vacation.


            This script is not working, I tell you every process which I followed:

            1. Copy the script and paste in to script editor and place on script panel in InDesign. As well as change the file extension in the script.
            2. Place reference text in InDesign document as screenshot attached.
            3. All related images keep on Desktop.
            4. Then run the script and didn't happen anything.


            Please suggest what need to be done or missing anything.


            Thanks in advance.

            Screen Shot 2018-10-31 at 5.22.38 PM.pngScreen Shot 2018-10-31 at 5.22.26 PM.png

            • 3. Re: Auto Link in InDesign
              Manan Joshi Level 4

              Hi Amit,


              I have not tried the AppleScript given by Hopkins, but mentioned below is my take on the problem using Javascript.

              function findAndReplace(foldername)
                   app.findGrepPreferences.findWhat = "\\b([A-Za-z0-9_]+\\.ai|[0-9A-Za-z_]+\\.eps)\\b"
                   var nameList = app.activeDocument.findGrep(true)
                   for(var i = nameList.length - 1; i >= 0; i--)
                        nameList[i].insertionPoints.itemByRange(0, nameList[i].insertionPoints.length - 1).place(foldername + "/" + nameList[i].contents)
                   app.findGrepPreferences = NothingEnum.nothing; 


              This method wants an argument that is the path of the folder from where it needs to pick up the image files to place. Give it a try and let me know how it goes.



              • 4. Re: Auto Link in InDesign
                S Hopkins Adobe Community Professional

                First, don't change the filename extension (leave it as .scpt). [Note: The format you choose when you save the script determines the file extension--you should not have to "change" the extension. Saving it as "text" gives it the .applescript extension.] Second, the script is expecting a folder named graphics on the Desktop that contains the files. Third, try running the script from the Script Editor. That way you can test values for variables at the bottom of the script to see what is happening. For instance put the variable foundList at the bottom of the script. If foundList is an empty list, then the regular expression needs to be fixed (the expression I gave you is looking for .png and .jpg files). Try the one that Manan gave you to get .ai and .eps files.


                • 5. Re: Auto Link in InDesign
                  gupta.amit1981 Level 1

                  Hi Manan,


                  Sorry for late reply and thanks to your response.


                  Please suggest the path (as well as provide the syntax) because nothing will happen.




                  • 6. Re: Auto Link in InDesign
                    gupta.amit1981 Level 1

                    Hi Shirley,


                    Thanks for your reply. I have done as per your instruction that run from the script editor, nothing will happen.


                    And having some result at the bottom as shown below.


                    Screen Shot 2018-11-06 at 5.14.42 PM.png

                    • 7. Re: Auto Link in InDesign
                      Manan Joshi Level 4

                      Hi Amit,


                      Could you please be specific on what you did and what results did you see. I am hoping that you know how to execute Javascript in InDesign. You could use ExtendScript Toolkit to run the code. The prerequisites for the code to run successfully would be an open document with some story containing the name of the images with extension .ai and .eps. Also you need to add the code to call the method i gave, by specifying the argument of the path of the folder where the images to be placed are present. Something like below

                      findAndReplace("Path to your folder containing the images to place")

                      Also make sure that the folder does contain the image with the name that is the same as the text in the document.



                      • 8. Re: Auto Link in InDesign
                        gupta.amit1981 Level 1

                        Hi Manan,


                        Thank you for your reply.


                        Yes, I know how to execute the JS file because I have many script to short my work.


                        The Process which I follow as under:

                        1. copy all text from your script and paste into the extendScript and save the file on respective script folder,

                        2. put the folder location on line 1 and replace .ai and .eps to jpg and png.



                        3. run the script and found the error as attached screenshot.


                        Screen Shot 2018-11-12 at 3.54.05 PM.png


                        4. file location as attached screenshot.




                        Please help and let me know if I missed anything.




                        • 9. Re: Auto Link in InDesign
                          Manan Joshi Level 4

                          The code is wrong typed, you are never calling the method and also in place of an argument you are sending in a string constant. In order to fix this copy the code i wrote as it is and then at the last add the following line,


                          So the code i gave before is the function definition which describes what the function does, and this line i mentioned above calls this function with an argument. Try this and let me know what you get.



                          • 10. Re: Auto Link in InDesign
                            S Hopkins Adobe Community Professional

                            Hi again,


                            In response to your query that shows a screen shot of the Result in the AppleScript editor window.

                            This shows us two things:

                            1. The GREP is working, it found two text references:

                            characters 1 to character 5 of the story, and text from character 7 to character 11.

                            2. Placing the file is failing, it failed with two files:

                            files A.png and A.jpg in the graphics folder on the user's desktop.

                            If the files are not in a folder named "graphics" on your desktop, then you have a problem.


                            Here is a little test you can do to determine the problem. Put the following in a new script for the AppleScript Editor. Paste in the following:


                            set fileName to "A.jpg"

                            set userDesktop to path to desktop from user domain as string

                            set userFolder to "graphics"

                            set folderPath to userDesktop & userFolder

                            set filePath to folderPath & ":" & fileName

                            tell application "Adobe InDesign CC 2018"

                              tell page 1 of document 1

                              place file filePath

                              end tell

                            end tell


                            Run it and check the result window. If it says the file can't be found, make sure the folder is named with a lower-case g and the file is in the folder--and the folder is on your desktop. If your files are elsewhere, you need to change the location in the script. Now you know how the Result window can help you to debug a script.

                            • 11. Re: Auto Link in InDesign
                              Colin Flashman Adobe Community Professional

                              Have you considered using the Place Inline Images script by Kasyan Servetsky? I believe that will do what you are after. A link to a site describing the script is here: http://www.kasyan.ho.com.ua/indesign/image/place_inline_images.html