2 Replies Latest reply on Jun 15, 2009 6:14 PM by ianmcarey

    Need help with insert graphics script


      Hello very wise folks,


      My team is belatedly making the jump from an all-Windows CS2 environment into a Mac CS4 environment. One big timesaver which we need to update is a replace-filename-with-graphic script--we previously used a vbs script which I got from these forums, but obviously that won't work on Macs or with CS4.


      So--I've downloaded the "ReplaceTextWithGraphic.jsx" script (from the Scripting Guide script zip archive) which has been mentioned in several threads here, but I admit I'm at a loss as to how to customize it for our needs--we basically just need to be able to point to a particular folder and have the script search for tagged filenames (we previously used "<<filename.eps>>") and insert the file(s). So far the script seems to just be creating example documents.


      Any help would be much appreciated. (And I know, I should really take a class sometime instead of relying on the kindness of strangers. I plan to.)




      Ian Carey

        • 1. Re: Need help with insert graphics script
          Olav Martin Kvern Level 3

          Hi Ian,


          All of the Scripting Guide scripts create example documents--that way, they can run without any preconditions. But I tried to make it easy for you to pull out the functions you might need. In the ReplaceTextWithGraphics.jsx script, the part that you need is in the "mySnippet" function. It's fairly easy to turn it into a script that will work on your documents. Here's a quick example:


          function main(){
          myFolder= Folder.selectDialog ("Choose a Folder");
          if(myFolder != null){
          function myReplaceGraphics(myFolder){
          var myInlineGraphic, myX1, myY1, myX2, myY2, myFoundItem;
          var myDocument = app.documents.item(0);
          myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
          myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
          var myRegExp = "(?i)<<(.*)>>"
          var myDocument = app.documents.item(0);
          //Clear the grep find/change preferences.
          app.findGrepPreferences = NothingEnum.nothing;
          app.changeGrepPreferences = NothingEnum.nothing;
          app.findGrepPreferences.findWhat = myRegExp;
          //Search the document.
          var myFoundItems = myDocument.findGrep(true);
          //Clear the find/change preferences after the search.
          app.findGrepPreferences = NothingEnum.nothing;
          app.changeGrepPreferences = NothingEnum.nothing;
          if(myFoundItems.length != 0){
            for(var myCounter = 0; myCounter < myFoundItems.length; myCounter ++){
             //A potential problem comes up here: what if the graphic is too big
             //to fit on the page? What if the text is overset? There are no
             //foolproof solutions that can handle all of the possible problems
             //that can arise; your best bet is to adjust your scripts to deal
             //with the specific layouts of specific documents rather than to try to
             //create a single function that can handle all situations for all
             //possible documents.
             //In this case, we simply make the frame a set size and place the graphic
             //into the frame, then fit the graphic to the frame proportionally, and
             //then fit the frame to the resized graphic.
             myFoundItem = myFoundItems[myCounter];
             myX1 = myFoundItem.characters.item(0).horizontalOffset;
             myY1 = myFoundItem.characters.item(0).baseline;
             //Change the following values to change the size of the frame.
             myX2 = myX1 + 72;
             myY2 = myY1 + 72;
             myFileName = myFoundItem.contents.replace(/<<(.*)>>/i, "$1");
             myFilePath = myFolder + "/" + myFileName;
             myFrame = myFoundItem.insertionPoints.item(0).rectangles.add();
             //Recompose the text after adding the inline frame.
             myFrame.geometricBounds = [myY1, myX1, myY2, myX2];
              myInlineGraphic = myFrame.place(File(myFilePath))[0];
             } catch(myError){}
          //Now replace the tags.
          app.findGrepPreferences.findWhat = myRegExp;
          app.changeGrepPreferences.changeTo = "";
          //Clear the find/change preferences after the search.
          app.findGrepPreferences = NothingEnum.nothing;
          app.changeGrepPreferences = NothingEnum.nothing;





          1 person found this helpful
          • 2. Re: Need help with insert graphics script
            ianmcarey Level 1

            Thank you so much Ole--that was exactly what I needed. And I was able to search the forum to figure out how to adjust it to set the frame to fit-to-content, and fill & stroke to none, so hopefully I'm a little less hopeless each time I come here.