23 Replies Latest reply on Jul 30, 2009 3:14 PM by Thomas B. Nielsen

    Convert or repurpose Applescript to Javascript for InDesign?

    schnarkle Level 1

      A colleague developed this script to import comics into a template as seen below and I've been reading up on Javascript trying to figure out how to convert this for use in our windows environment:

       

      --This script will open an Adobe Indesign page for the Comics and flow all comics files for the selected date.
      set displayString to "Please Enter Year"
      set DYear to (year of (current date))
      set response to display dialog displayString default answer DYear
      set theNumber to (text returned of response) as number
      set DYear to text returned of response
      set displayString to "Please Enter Month"
      set Dmonth to do shell script "date '+%m'"
      set response to display dialog displayString default answer Dmonth
      set theNumber to (text returned of response) as number
      set Dmonth to text returned of response
      set displayString to "Please Enter Day"
      set Dday to (day of (current date)) + 1
      set response to display dialog displayString default answer Dday
      set theNumber to (text returned of response) as number
      set Dday to text returned of response
      tell application "Adobe InDesign CS4"
      set comic1 to "macintosh Hd:users:marshall:documents:comics:" & DYear & Dmonth & Dday & "pzjud-a.tif"
      set comic2 to "macintosh hd:users:marshall:documents:comics:fct" & DYear & Dmonth & Dday & ".tif"
      set comic3 to "macintosh hd:users:marshall:documents:comics:det" & DYear & Dmonth & Dday & ".tif"
      set comic4 to "Macintosh HD:users:marshall:documents:comics:bbt" & DYear & Dmonth & Dday & ".tif"
      set comic5 to "Macintosh HD:users:marshall:documents:comics:blt" & DYear & Dmonth & Dday & ".tif"
      set mydoc to open "Macintosh HD:users:marshall:documents:comix:GGDailyBlank.indd"
      tell mydoc
      tell page 1
      set myrect1 to page item named "image1"
      set myrect2 to page item named "image2"
      set myrect3 to page item named "image3"
      set myrect4 to page item named "image4"
      set myrect5 to page item named "image5"
      tell myrect1
      set myGraphic to place (comic1 as string)
      fit myrect1 given content to frame
      end tell
      tell myrect2
      set myGraphic to place (comic2 as string)
      fit myrect2 given content to frame
      end tell
      tell myrect3
      set myGraphic to place (comic3 as string)
      fit myrect3 given content to frame
      end tell
      tell myrect4
      set myGraphic to place (comic4 as string)
      fit myrect4 given content to frame
      end tell
      tell myrect5
      set myGraphic to place (comic5 as string)
      fit myrect5 given content to frame
      end tell
      end tell
      end tell

      -------------------------------------------------------

      Any tips or ideas to help get me started with this?  Most of the tutorials I'm reading deal with text and not images...

      Thanks for any help!

       

      Doug

        • 1. Re: Convert or repurpose Applescript to Javascript for InDesign?
          Thomas B. Nielsen Level 3

          Hi Doug,

           

          I have made a JavaScript version for you.

          Changing the pathnames is up to you, but you should be able to figure it out :-)

           

          #targetengine "session"
          var date, month, year, myDocument;
          var curDate = new Date();
          //"Macintosh HD:users:marshall:documents:comix:GGDailyBlank.indd"
          var myTemplatePath = "/c/Programmer/Adobe/Adobe%20InDesign%20CS3/Scripts/Scripts%20Panel/Nobrainer%20Scripting /Adobe%20Forums%20Help/comicImport.indd";
          //app.open(myTemplate);

           

          var win = showDialog();

           

          function showDialog() {
              var win = new Window('palette');
              with(win){
                  win.Pnl = add('panel', undefined, 'Date / Month / Year');
                  win.Pnl.orientation = 'row';
                  with(win.Pnl) {
                      win.Pnl.day = add('edittext');
                      win.Pnl.day.text = curDate.getDate();
                      win.Pnl.day.preferredSize = [30,20];
                     
                      win.Pnl.month = add('edittext');
                      win.Pnl.month.text = curDate.getMonth() + 1;
                      win.Pnl.month.preferredSize = [30,20];
                     
                      win.Pnl.year = add('edittext');
                      win.Pnl.year.text = curDate.getFullYear();
                      win.Pnl.year.preferredSize = [50,20];
                  }
                  win.btnOk = add('button', undefined, 'Import Comic');
                  win.btnOk.onClick = setDate;
              };
              win.center();
              win.show();
              return win;
          }
          function setDate() {
              date = win.Pnl.day.text;
              month = win.Pnl.month.text;
              year = win.Pnl.year.text;
              // OK we close the window and do the import
              //win.close();
              importComics();
          }

           

          function importComics() {
              try {
                  myDocument = app.open(new File(myTemplatePath));
              } catch (e) {
                  alert(e);
              }
              try {
                  //set comic1 to "macintosh Hd:users:marshall:documents:comics:" & DYear & Dmonth & Dday & "pzjud-a.tif"
                  var comics = new Array();
                  // REPLACE with own filepaths, could be
                  //comics.push(new File("/c/comics/" + year + month + date + "pzjud-a.tif"));
                  comics.push(new File("/c/Programmer/Adobe/Adobe%20InDesign%20CS3/Scripts/Scripts%20Panel/Nobrainer%20Scri pting/Adobe%20Forums%20Help/comic1-" + year + "-" + month + "-" + date + ".jpg"));
                  comics.push(new File("/c/Programmer/Adobe/Adobe%20InDesign%20CS3/Scripts/Scripts%20Panel/Nobrainer%20Scri pting/Adobe%20Forums%20Help/comic2-" + year + "-" + month + "-" + date + ".jpg"));
                  comics.push(new File("/c/Programmer/Adobe/Adobe%20InDesign%20CS3/Scripts/Scripts%20Panel/Nobrainer%20Scri pting/Adobe%20Forums%20Help/comic3-" + year + "-" + month + "-" + date + ".jpg"));
                  comics.push(new File("/c/Programmer/Adobe/Adobe%20InDesign%20CS3/Scripts/Scripts%20Panel/Nobrainer%20Scri pting/Adobe%20Forums%20Help/comic4-" + year + "-" + month + "-" + date + ".jpg"));
                  comics.push(new File("/c/Programmer/Adobe/Adobe%20InDesign%20CS3/Scripts/Scripts%20Panel/Nobrainer%20Scri pting/Adobe%20Forums%20Help/comic5-" + year + "-" + month + "-" + date + ".jpg"));
              } catch (e) {
                  alert("Error assigning images for import\n" + e);
              }
             
              for (i = 1; i <= comics.length; i++) {
                  // Script label of the rectangles/pageitems to place the graphics into
                  var myRect  = myDocument.pageItems.item("comic" + i);
                  try {
                  myRect.place(comics[i-1]);
                  } catch (e) {
                      alert(e);
                  }
                  myRect.fit(FitOptions.CONTENT_TO_FRAME);
              }
          }

           

          --

          Thomas B. Nielsen

          http://www.nobrainer.dk

          • 2. Re: Convert or repurpose Applescript to Javascript for InDesign?
            [Jongware] Most Valuable Participant

            Thomas, a few technical points. (I didn't try the script.)

             

            Why don't you put that long pathname into a variable, as it occurs several times. It means less typing for you, and the OP only has to change it in one position...

             

            It would be better to issue an "exit" in the catch after a possible import error -- if one fails now, the good ones after this will be placed into the wrong box number.

            • 3. Re: Convert or repurpose Applescript to Javascript for InDesign?
              Thomas B. Nielsen Level 3

              Hi Jongware,

               

              Thx for your response, you are right in both cases, but it was getting late so i just posted with very little finish.

              (Had to go to a meeting at my new job this morning).

               

              I will post the changes when i get home - this evening perhaps.

              • 4. Re: Convert or repurpose Applescript to Javascript for InDesign?
                [Jongware] Most Valuable Participant

                So it was a "quickie"!? ... Impressive ...

                • 5. Re: Convert or repurpose Applescript to Javascript for InDesign?
                  schnarkle Level 1

                  Thomas,

                   

                  All I can say is wow!  Fantastic and thank you so much!  This has saved us a ton of headaches. 

                  If you're ever in deep south Texas I'll buy you a pint. 

                  • 6. Re: Convert or repurpose Applescript to Javascript for InDesign?
                    schnarkle Level 1

                    Hi again Thomas,

                     

                    Upon clicking "Import Comic" I get a script alert that says "ReferenceError:app.open is not a function"

                     

                    any ideas?

                     

                    thanks!

                    • 7. Re: Convert or repurpose Applescript to Javascript for InDesign?
                      Thomas B. Nielsen Level 3

                      I dont get any error messages, so i need to know a bit more.

                       

                      Did it ever work on your machine?

                           If yes, when does it work and when not?

                      What version of Indesign are you using?

                      • 8. Re: Convert or repurpose Applescript to Javascript for InDesign?
                        schnarkle Level 1

                        No it hasn't worked yet.  I get the message right after clicking the date submit.

                        I did simplify the directory structure to c:\comics

                        I'm using CS4..

                        • 9. Final comic import script
                          Thomas B. Nielsen Level 3

                          A modified version, not sure if this will fix the "app.open is not a funtion error".

                           

                          #targetengine "session"
                          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
                          var date, month, year, myDocument;
                          var curDate = new Date();
                          //"Macintosh HD:users:marshall:documents:comix:GGDailyBlank.indd"
                          var myTemplatePath = "/c/Programmer/Adobe/Adobe%20InDesign%20CS3/Scripts/Scripts%20Panel/Nobrainer%20Scripting /Adobe%20Forums%20Help/comicImport.indd";
                          var myComicsPath = "/c/Programmer/Adobe/Adobe%20InDesign%20CS3/Scripts/Scripts%20Panel/Nobrainer%20Scripting /Adobe%20Forums%20Help/";
                          var myTemplate = new File(myTemplatePath);

                           

                          if (myTemplate.exists) {
                              try {
                                  myDocument = app.open(myTemplate);
                              } catch (e) {
                                  alert("Could not open template, exiting\n" + e);
                                  exit();
                              }
                              var win = showDialog();
                          } else {
                              alert("Could not locate template at:\n" + myTemplatePath + "\nexiting");
                          }

                           

                          function showDialog() {
                              var win = new Window('palette');
                              with(win){
                                  win.Pnl = add('panel', undefined, 'Date / Month / Year');
                                  win.Pnl.orientation = 'row';
                                  with(win.Pnl) {
                                      win.Pnl.day = add('edittext');
                                      win.Pnl.day.text = curDate.getDate();
                                      win.Pnl.day.preferredSize = [30,20];
                                     
                                      win.Pnl.month = add('edittext');
                                      win.Pnl.month.text = curDate.getMonth() + 1;
                                      win.Pnl.month.preferredSize = [30,20];
                                     
                                      win.Pnl.year = add('edittext');
                                      win.Pnl.year.text = curDate.getFullYear();
                                      win.Pnl.year.preferredSize = [50,20];
                                  }
                                  win.btnOk = add('button', undefined, 'Import Comic');
                                  win.btnOk.onClick = setDate;
                              };
                              win.center();
                              win.show();
                              return win;
                          }
                          function setDate() {
                              date = win.Pnl.day.text;
                              month = win.Pnl.month.text;
                              year = win.Pnl.year.text;
                              // OK we close the window and do the import
                              //win.close();
                              importComics();
                          }

                           

                          function importComics() {
                              try {
                                  //set comic1 to "macintosh Hd:users:marshall:documents:comics:" & DYear & Dmonth & Dday & "pzjud-a.tif"
                                  var comics = new Array();
                                  // REPLACE with own filepaths, could be
                                  //comics.push(new File("/c/comics/" + year + month + date + "pzjud-a.tif"));
                                  comics.push(new File(myComicsPath + "comic1-" + year + "-" + month + "-" + date + ".jpg"));
                                  comics.push(new File(myComicsPath + "comic2-" + year + "-" + month + "-" + date + ".jpg"));
                                  comics.push(new File(myComicsPath + "comic3-" + year + "-" + month + "-" + date + ".jpg"));
                                  comics.push(new File(myComicsPath + "comic4-" + year + "-" + month + "-" + date + ".jpg"));
                                  comics.push(new File(myComicsPath + "comic5-" + year + "-" + month + "-" + date + ".jpg"));
                              } catch (e) {
                                  alert("Error asigning images for import, stopping script\n" + e);
                                  exit();
                              }
                             
                              for (i = 1; i <= comics.length; i++) {
                                  // Script label of the rectangles/pageitems to place the graphics into
                                  try {
                                      var myRect  = myDocument.pageItems.item("comic" + i);
                                      myRect.label;
                                  } catch (e) {
                                      alert("Could not locate the frame with scriptlabel comic" + i + ", exiting");
                                      exit();
                                  }
                                  try {
                                      myRect.place(comics[i-1]);
                                  } catch (e) {
                                      alert(e);
                                  }
                                  myRect.fit(FitOptions.CONTENT_TO_FRAME);
                              }
                          }

                           

                          Message was edited by: Thomas B. Nielsen

                          • 10. Re: Convert or repurpose Applescript to Javascript for InDesign?
                            Thomas B. Nielsen Level 3

                            I only have CS3 atm so i can't test in CS4, but i can't imagine there is any difference between the two version regarding the app.open command.

                             

                            Have you tried the latest posted version?

                            • 11. Re: Convert or repurpose Applescript to Javascript for InDesign?
                              schnarkle Level 1

                              Ok I think the first issue was because I was a moron.  I had the ExtendScript option selected as the target app in the ESTK instead of Indesign when running. 

                               

                              But now the date window just flashes and disappears..

                              Here is the script with the directories I'm using...  I also attached the template..

                               

                              ------------------------------------------------

                               

                              #targetengine "session"
                              var date, month, year, myDocument;
                              var curDate = new Date();
                              //"Macintosh HD:users:marshall:documents:comix:GGDailyBlank.indd"
                              var myTemplatePath = "/c/Comic/comicImport.indd";
                              //app.open(myTemplate);

                               

                               

                               

                              var win = showDialog();

                               

                               

                               

                              function showDialog() {
                                  var win = new Window('palette');
                                  with(win){
                                      win.Pnl = add('panel', undefined, 'Date / Month / Year');
                                      win.Pnl.orientation = 'row';
                                      with(win.Pnl) {
                                          win.Pnl.day = add('edittext');
                                          win.Pnl.day.text = curDate.getDate();
                                          win.Pnl.day.preferredSize = [30,20];
                                        
                                          win.Pnl.month = add('edittext');
                                          win.Pnl.month.text = curDate.getMonth() + 1;
                                          win.Pnl.month.preferredSize = [30,20];
                                        
                                          win.Pnl.year = add('edittext');
                                          win.Pnl.year.text = curDate.getFullYear();
                                          win.Pnl.year.preferredSize = [50,20];
                                      }
                                      win.btnOk = add('button', undefined, 'Import Comic');
                                      win.btnOk.onClick = setDate;
                                  };
                                  win.center();
                                  win.show();
                                  return win;
                              }
                              function setDate() {
                                  date = win.Pnl.day.text;
                                  month = win.Pnl.month.text;
                                  year = win.Pnl.year.text;
                                  // OK we close the window and do the import
                                  //win.close();
                                  importComics();
                              }

                               

                               

                               

                              function importComics() {
                                  try {
                                      myDocument = app.open(new File(myTemplatePath));
                                  } catch (e) {
                                      alert(e);
                                  }
                                  try {
                                      //set comic1 to "macintosh Hd:users:marshall:documents:comics:" & DYear & Dmonth & Dday & "pzjud-a.tif"
                                      var comics = new Array();
                                      // REPLACE with own filepaths, could be
                                      //comics.push(new File("/c/comics/" + year + month + date + "pzjud-a.tif"));
                                      comics.push(new File("/c/Comic/Comics/comic1-" + year + "-" + month + "-" + date + ".jpg"));
                                      comics.push(new File("/c/Comic/Comics/comic2-" + year + "-" + month + "-" + date + ".jpg"));
                                      comics.push(new File("/c/Comic/Comics/comic3-" + year + "-" + month + "-" + date + ".jpg"));
                                      comics.push(new File("/c/Comic/Comics/comic4-" + year + "-" + month + "-" + date + ".jpg"));
                                      comics.push(new File("/c/Comic/Comics/comic5-" + year + "-" + month + "-" + date + ".jpg"));
                                  } catch (e) {
                                      alert("Error assigning images for import\n" + e);
                                  }
                                
                                  for (i = 1; i <= comics.length; i++) {
                                      // Script label of the rectangles/pageitems to place the graphics into
                                      var myRect  = myDocument.pageItems.item("comic" + i);
                                      try {
                                      myRect.place(comics[i-1]);
                                      } catch (e) {
                                          alert(e);
                                      }
                                      myRect.fit(FitOptions.CONTENT_TO_FRAME);
                                  }
                              }

                              • 12. Re: Convert or repurpose Applescript to Javascript for InDesign?
                                schnarkle Level 1

                                bah it won't let me attach the template

                                • 13. Re: Convert or repurpose Applescript to Javascript for InDesign?
                                  Larry G. Schneider Adobe Community Professional & MVP

                                  Zip the .indd file. You can attach the zipped file.

                                  • 15. Re: Convert or repurpose Applescript to Javascript for InDesign?
                                    Thomas B. Nielsen Level 3

                                    First, restart indesign then try to replace:

                                    #targetengine "session"

                                     

                                    with:

                                    #targetengine "session"

                                    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

                                     

                                    run the script from indesign.

                                     

                                    I would believe the flashing to have something to do with the script not being run in the session engine, but i don't know.


                                    The latest post of the script has some improvements as suggested by Jongware.

                                    • 16. Re: Convert or repurpose Applescript to Javascript for InDesign?
                                      Thomas B. Nielsen Level 3

                                      nvm the attachments, it takes a lot of time for them to get approved.

                                      • 17. Re: Convert or repurpose Applescript to Javascript for InDesign?
                                        schnarkle Level 1

                                        Ok running from inside InDesign I now get the date pop-up but when I select the date and submit I now get a Script Alert, ReferenceError: Object is invalid.

                                         

                                        Dang sorry for these hassles.

                                         

                                        Could it be an incorrect path or something?

                                        • 18. Re: Convert or repurpose Applescript to Javascript for InDesign?
                                          Thomas B. Nielsen Level 3

                                          Please try to use the latest posted script - with improvements as sugested by Jongware, it might give some better error messages.

                                           

                                          I think that reference error is due to a missing or not found image container - do you have 5 rectangles with the script label comic1 to 5?

                                          • 19. Re: Convert or repurpose Applescript to Javascript for InDesign?
                                            Thomas B. Nielsen Level 3

                                            try replacing the lines:

                                             

                                            // Script label of the rectangles/pageitems to place the graphics into

                                            var myRect  = myDocument.pageItems.item("comic" + i);

                                             

                                            with:


                                            // Script label of the rectangles/pageitems to place the graphics into
                                            try {
                                              var myRect  = myDocument.pageItems.item("comic" + i);
                                              myRect.label;
                                            } catch (e) {
                                              alert("Could not locate the frame with scriptlabel comic" + i + ", exiting");
                                              exit();
                                            }

                                             

                                            This should give you an error msg when a frame is not located.

                                            • 20. Re: Convert or repurpose Applescript to Javascript for InDesign?
                                              schnarkle Level 1

                                              ok getting real close I think...

                                               

                                              now getting IOError: The file c:\Comic\Comics\comic1-2009-7-08.tif was not found.

                                               

                                              I think now it's just a manner of naming conventions on my part correct?

                                              • 21. Re: Convert or repurpose Applescript to Javascript for InDesign?
                                                Thomas B. Nielsen Level 3

                                                Yes that is correct. Have you noticed that i use forward slashes in my paths? like "/c/Programmer/Adobe/"?

                                                I do it to avoid problems with \ which in JS is a special character that escapes the following char.

                                                 

                                                I use this little script to give me the path of a file (path shows in the estk after script is run):

                                                // Alert file - JavaScript Document
                                                var myFile = File.openDialog("Choose a File");
                                                if((myFile != "")&&(myFile != null)){
                                                    alert("Path: " + myFile + "\nType: " + myFile.type);
                                                    $.writeln("Path: " + myFile + "\nType: " + myFile.type);
                                                }

                                                • 22. Re: Convert or repurpose Applescript to Javascript for InDesign?
                                                  schnarkle Level 1

                                                  Great!  I named the files correctly now and it flows through like a charm!  I think I'm ready to roll now!

                                                   

                                                  Thanks so much for your continued help. 

                                                  • 23. Re: Convert or repurpose Applescript to Javascript for InDesign?
                                                    Thomas B. Nielsen Level 3

                                                    Ok nice and you are welcome.

                                                     

                                                    Please mark the last post with the updated script as answered correctly, so others may benefit from it and i can have my hard earned 10 points ;-)