3 Replies Latest reply on Feb 8, 2017 3:34 AM by davidg52583218

    Scripting images into frames

    davidg52583218

      Hi,

       

      I'm beginning with this so I apologise in advance if this seems trivial. I'm struggling to script the placement of an image into a frame using JavaScript for InDesign CC 2017 (ExtendScript Toolkit). On my last line, I get "object is invalid" for the frame - which I've labelled with a script lael "Image1" in the InDesign file.

       

      Any help would be really appreciated where it seems in terms of syntax I am doing the same as a number of tutorials out there.

       

      var folder = "/c/Users/d_a_g/OneDrive/Work/InDesign/ContentTest/"
      var myFolder = Folder(folder),
      myTemplate = File(myFolder + "/ContentTemplate.indd"),
      //myPics = myFolder.getFiles("*.jpg"),
      newDoc = app.open(myTemplate),
      myDoc = app.activeDocument,
      myPages = myDoc.pages,
      i = 0,
      lastPage = 0;

      firstPage = myDoc.pages.item(0)
      firstFrame = firstPage.rectangles.item("Image1");

      $.writeln(firstPage);
      $.writeln(firstFrame);

      var myGraphicFile = File.openDialog ("Choose a file");
      $.writeln(myGraphicFile);
      firstFrame.place(myGraphicFile);

        • 1. Re: Scripting images into frames
          vinny38 Level 4

          Hi

          I think you should ask your question in the INDD scripting forum:

          InDesign Scripting

          Vinny

          • 2. Re: Scripting images into frames
            Loic.Aigon Adobe Community Professional

            Try this:

             

            var main = function() {
              var folder = "/c/Users/d_a_g/OneDrive/Work/InDesign/ContentTest/",
              myTemplate, newDoc, firstPage, myGraphicFile;
            
              //Checking folder existence
              if ( !folder.exists ) {
              alert(folder.fsName+" doesn't exist" );
              return;
              }
            
            
              myTemplate = File(myFolder + "/ContentTemplate.indd"),
              //Checking myTemplate existence
              if ( !myTemplate.exists ) {
              alert(myTemplate.fsName+" doesn't exist" );
              return;
              }
            
              //Referencing newDoc as the document being opened i.e. the activeDocument
              newDoc = app.open(myTemplate),
            
              firstPage = newDoc.pages[0];
            
              //Referencing the first frame
              firstFrame = firstPage.rectangles[0];
            
              //Checking firstFrame validity
              if ( !firstFrame.isValid ) {
              alert(firstFrame+" doesn't exist" );
              doc.close(SaveOptions.NO);
              return;
              }
            
              myGraphicFile = File.openDialog ("Choose a file");
            
              //Exit if no file opened
              if ( !myGraphicFile ) return;
            
              //Placing the image
              firstFrame.place(myGraphicFile);
            }
            
            
            main();
            

             

            HTH

             

            Loic

            Ozalto | Productivity Oriented - Loïc Aigon

            1 person found this helpful
            • 3. Re: Scripting images into frames
              davidg52583218 Level 1

              Apologies - when I searched to find this - I couldn't initially find it. Thanks for the link. Will put any future posts there.