4 Replies Latest reply on Feb 8, 2014 11:35 AM by Melchiar

    Save Indd Filename as Filename of placed PDF


      Hi all, I'm not much of a programmer so this may seem like an incredibly easy problem for most of you, but it's baffling me. I'm running Indesign 5.5, and I have a javascript I've hacked together that will step a PDF times onto a page at specific spacing. What I'd like the script to do next is to insert the filename of that placed PDF into a textbox somewhere on the page, and then prompt you to save the indesign file, with the filename defaulting to the name of the placed pdf.


      Ie. If the filename of the PDF placed on that page is "cct-smith-1.pdf", the text "cct-smith-1" would appear in a textbox on that page, and then the indesign file would prompt you to save with a default filename of "cct-smith-1.indd". Does that make sense?



        • 1. Re: Save Indd Filename as Filename of placed PDF
          Loic.Aigon Adobe Community Professional


          I think you have two separate concerns here. For the labeling, you can simply use the caption tool that will make most of the job for you.


          The second topic (save as) may require that you actually selected a "caption" frame but I don't know if you can get to display teh save as dialog while pre typing the file name. What youc an get for sure is save the file with the caption as a filename straight away. The script may require a folder selection though.



          function saveAsCaption()
               var tf, fileName, fo, saveFile;
               if ( app.documents.length || app.selection.length != 1 || !(app.selection[0] instanceof TextFrame) ) {
                    alert("You need to select a caption");
               tf = app.selection[0];
               fileName = tf.contents.replace ( ".pdf", ".indd");
                 fo = Folder.selectDialog("Please select a saveTo Folder");
               if ( !fo ) return;
               saveFile = File ( fo+"/"+fileName );
               app.activeDocument.saveACopy ( saveFile );





          1 person found this helpful
          • 2. Re: Save Indd Filename as Filename of placed PDF
            Melchiar Level 1

            Thanks for the quick response Loic, the caption tool would make sense if I were needing to display the file name right next to each PDF, however I actually need to display the filename at the end of the page.


            The reason for this is that we use the PDF file name as an identifier for each print job before we put it through a digital die-cutter, so currently we always manually write the name at the end of the page. However, is there any way to have the script pull the file name from the currently selected object and assign a variable to it?


            Thanks, your help is greatly appreciated!

            • 3. Re: Save Indd Filename as Filename of placed PDF
              Loic.Aigon Adobe Community Professional



              Ok, you may want to use this startup script that will add a "First PDF file name" text variable to your document :




              It will just add a blank "First PDF fil name" txt variable every new open document to this document. Then you can update the variable with this snippet or manually :



              //will edit "First PDF file name" text variabl within the document if any
              function editFirstPDFCaption() {
                        var doc, tv, caption = "";
                        if ( !app.documents.length ) return;
                        doc =app.activeDocument;
                        tv = doc.textVariables.itemByName ( "First PDF file name" );
                        //if the first graphic of the page is a PDF then set the text variable to contain link name
                        if ( doc.pages[0].rectangles.length &&
                        doc.pages[0].allGraphics.length &&
                        (doc.pages[0].allGraphics[0] instanceof PDF ) ){
                                  caption = doc.pages[0].rectangles[0].graphics[0].itemLink.name;
                                  tv.isValid && tv.variableOptions.contents = caption;


              Hope that helps,



              1 person found this helpful
              • 4. Re: Save Indd Filename as Filename of placed PDF
                Melchiar Level 1

                Thanks Loic for your help. You helped me get to part of the solution. Ultimately though I was able to find a simpler solution based on a couple other scripts I dug up, which does exactly what I needed. It's not quite as elegant as it should be (there are no checks or alerts if the conditions of the script aren't met) but it does the job well enough.


                Basically, it just creates a variable for both the filename and filepath of the first graphic it finds in the document. It then uses a replace text script to remove the PDF extension from the name and swap the text into a text box containing "Default Text", also adding a variable I use earlier in the script to indicate how many PDFs were stepped. Finally, it saves the document to the same folder as the PDF using the MyImagePath variable, also including the number of PDFs variable in the filename.


                Hope someone finds it useful!


                var myDoc = app.activeDocument;

                var myImage = myDoc.allGraphics;


                //find file name and path of PDF in document

                for (var i=0; myImage.length>i; i++){



                    var MyImageName  = myImage[i].itemLink.name;

                    var MyImagePath  = myImage[i].itemLink.filePath;



                //replace text box containing "Default Text" with "filename-# of labels variable-up"

                replaceText ("Default Text", MyImageName + "-" + myNumberOfLabels + "up");

                replaceText (".pdf", "");


                function replaceText (input, output)


                     app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;

                     app.findTextPreferences.findWhat = input;

                     app.changeTextPreferences.changeTo = output;


                     app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;


                //save indd file into same directory as PDF, with same filename as PDF + variable indicating the number of labels being stepped


                saveFile = File(MyImagePath.replace(/.pdf$/,'')+"-"+myNumberOfLabels+"up"+'.indd');

                app.activeDocument.save ( saveFile );