7 Replies Latest reply on Nov 11, 2014 1:03 AM by Kai Rübsamen

    HOW TO: set anchoredObjectSettings for .palce() object?

    oleh.melnyk Level 3

      Hi, I'm currently working on interesting script for GREP placing, and I'm wondering how I can set anchoredObjectSettings for .palce() object?

       

      for(i=0; i < found.length; i++)
      {
          foundElem = new File (myFolder + "/" + found[i].contents);
          found[i].place(foundElem); // Placing Ancored Object
         
          // HERE IS WHERE I NEED SOME HELP: how to set "anchoredObjectSettings" for just placed Ancored Object
          //anchoredObjectSettings.anchoredPosition = AnchorPosition.ABOVE_LINE;
          //anchoredObjectSettings.horizontalAlignment = HorizontalAlignment.TEXT_ALIGN;
      }
      

       

      Here you can download example files - script, InDesign file and images, that should be placed into InDesign file with script

      Dropbox - GREP placing.zip

       

      PS: I believe this script will be very useful, so if anybody have any ideas/suggestions, and want to help me with further development - this would be great!

        • 1. Re: HOW TO: set anchoredObjectSettings for .palce() object?
          Jump_Over Level 5

          Hi,

           

          line 04 returns an array of placed objects. Store it as variable and set properties of 1st element.

          mPlacedObj = found[i].place(foundElem)[0];
          mPlacedObj.anchoredObjectSettings.anchoredPosition = AnchorPosition.ABOVE_LINE;
          // and so on
          
          

           

          Jarek

          • 2. Re: HOW TO: set anchoredObjectSettings for .palce() object?
            oleh.melnyk Level 3

            Hi,

             

            Thanks for reply, but its seems its not working - I tried before asking here.. or it's work on your computer?))GREP place error.jpg

            /*
                GREP place files.
                This script will ask to select source folder with files to place,
                and then, with dialog box (or prompt) [this is not implemented yet, so I use static GREP value while developing]
                will ask to type GREP find expresion to search for text placeholder, that need to be replaced with file from source folder we just selected.
               
                TODO: for now, I want to figure out how I can set:
                anchoredObjectSettings.anchoredPosition = AnchorPosition.ABOVE_LINE;
                    AND
                anchoredObjectSettings.horizontalAlignment = HorizontalAlignment.TEXT_ALIGN;
                    once I placed file as ancoredObject (this part is at the very ending)
                   
                Also, this might be usefull to make anchored frame with column widh, and fit image proportionally
            */
            
            
            var myFilteredFiles;
            //Make certain that user interaction (display of dialogs, etc.) is turned on.
            app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
            
            
            var myExtensions = []; // initialize array
            
            
            myExtensions.push(".jpg", ".jpeg", ".png", ".gif"); // raster images
            myExtensions.push(".psd", ".tif", ".tiff", ".pdf"); // raster images (layered)
            myExtensions.push(".ai", ".eps", ".svg", ".cdr"); // vector graphics
            myExtensions.push(".mp3"); // audio files
            myExtensions.push(".mp4"); // video files
            myExtensions.push(".swf"); // flash files
            myExtensions.push(".doc", ".docx", ".rtf", ".txt"); // text documents
            myExtensions.push(".xls", ".xlsx"); // table documents   
            
            
            //Display the folder browser.
            var myFolder = Folder.selectDialog("Select the source folder with files for placing", "");
            
            
            //Get the path to the folder containing the files you want to place.
            if(myFolder != null)
            {
                if(File.fs == "Macintosh")
                {
                    myFilteredFiles = myMacOSFileFilter(myFolder);
                }
                else
                {
                    myFilteredFiles = myWinOSFileFilter(myFolder);
                }
                if(myFilteredFiles.length != 0)
                {
                    for(i = 0; i < myFilteredFiles.length; i++)
                    {
                        /* TEMPORARILY NOT USED var myTextFrame = app.selection[0];  var myPlaceFile = new File (myFilteredFiles[i]);  myTextFrame.place(myPlaceFile);        */
                    }
                }
            }
            
            
            //Windows version of the file filter.
            function myWinOSFileFilter(myFolder)
            {
              var myFiles = new Array;
              var myFilteredFiles = new Array;
              for(myExtensionCounter = 0; myExtensionCounter < myExtensions.length; myExtensionCounter++)
                {
                    myExtension = myExtensions[myExtensionCounter];
                    myFiles = myFolder.getFiles("*"+ myExtension);
              if(myFiles.length != 0)
                    {
                        for(var myFileCounter = 0; myFileCounter < myFiles.length; myFileCounter++)
                        {
              myFilteredFiles.push(myFiles[myFileCounter]);
                        }
                    }
              }
              return myFilteredFiles;
            }
            
            
            function myMacOSFileFilter(myFolder)
            {
              var myFilteredFiles = myFolder.getFiles(myFileFilter);
              return myFilteredFiles;
            }
            //Mac OS version of file filter
            //Have to provide a separate version because not all Mac OS users use file extensions and/or file extensions are sometimes hidden by the Finder.
            function myFileFilter(myFile)
            {
                var myFileType = myFile.type;
                switch (myFileType)
                {
                    case "JPEG":
                    case "EPSF":
                    case "PICT":
                    case "TIFF":
                    case "8BPS":
                    case "GIFf":
                    case "PDF ":
                        return true;
                        break;
                    default:
                    for(var myCounter = 0; myCounter<myExtensions.length; myCounter++)
                    {
                        var myExtension = myExtensions[myCounter];
                        if(myFile.name.indexOf(myExtension)>-1)
                        {
                            return true;
                            break;
                        }
                    }
              }
              return false;
            }
            
            
            findWhat = "\\<\\l+\\.\\l{2,4}\\>"; // \\< means "begining of the world", and \\> means end of the world;
            
            
            app.findGrepPreferences = app.changeGrepPreferences = null;
            app.findGrepPreferences.findWhat = findWhat; // our GREP that search for image placeholder text;
            
            
            found = app.activeDocument.findGrep();
            
            
            for(i=0; i < found.length; i++)
            {
                foundElem = new File (myFolder + "/" + found[i].contents);
                //found[i].place(foundElem); // Placing Ancored Object - this line works
               
                mPlacedObj = found[i].place(foundElem)[0]; // this one also working
               
                // those lines cause error
                //mPlacedObj.anchoredObjectSettings.anchoredPosition = AnchorPosition.ABOVE_LINE;
                //mPlacedObj.anchoredObjectSettings.horizontalAlignment = HorizontalAlignment.TEXT_ALIGN;
            }
            app.changeGrepPreferences.changeTo = "";
            app.activeDocument.changeGrep();
            
            • 3. Re: Re: HOW TO: set anchoredObjectSettings for .palce() object?
              Jump_Over Level 5

              Hi,

               

              Right, It was too fast...

               

              That line returns an array of placed graphics - and we have to set property for its container in fact.

               

              so make it for

              //.......
              mPlacedObj = found[i].place(foundElem)[0].parent;
              //.......
              
              

               

               

              Jarek

              • 4. Re: HOW TO: set anchoredObjectSettings for .palce() object?
                oleh.melnyk Level 3

                yeah! now it work just as expected! thank you so much!

                • 5. Re: Re: HOW TO: set anchoredObjectSettings for .palce() object?
                  Kai Rübsamen Adobe Community Professional

                  Hi Guys,

                   

                  can someone explain to me, why this whole filtering-thing (and Mac / PC is necessary) and if the above script is the right way to do that?

                   

                  To the grep-part:

                  1. The reset should be insert before the 'findWhat'-Part

                  2. If the file does not exists, the script will generate an error

                  3. There is no need for 'ChangeGrep', since the text will be replaced with the graphic

                   

                  I would do the GREP and place part in this way:

                   

                  app.findGrepPreferences = app.changeGrepPreferences = null;
                  app.findGrepPreferences.findWhat = /\b\l+\.\l{3,4}\b/.source;
                  var found = app.activeDocument.findGrep();
                  app.findGrepPreferences = app.changeGrepPreferences = null;
                  
                  var props = {
                      anchoredObjectSettings: {
                          anchoredPosition: AnchorPosition.ABOVE_LINE,
                          horizontalAlignment: HorizontalAlignment.TEXT_ALIGN
                      }
                  };
                  
                  for ( var i = 0; i < found.length; i++ ) {
                      var curFound = found[i];
                      var replaceElem = File ( myFolder + "/" + curFound.contents );
                      try {
                          var mPlacedObj = curFound.place( replaceElem )[0].parent; // Placing Ancored Object
                          mPlacedObj.properties = props;
                      } catch ( error ) {
                          alert ( error );
                      }
                  }
                  
                  

                   

                  – Kai

                  • 6. Re: Re: Re: HOW TO: set anchoredObjectSettings for .palce() object?
                    oleh.melnyk Level 3

                    Hi Kai, this script is still under development, here is updated version attached (now it can also place files/images by file name only, without extension):

                    #target indesign;
                    //#include  "! Basic functions.jsx"
                    /*
                        GREP place files.
                        This script will ask to select source folder with files to place,
                        and then, with dialog box (or prompt) [this is not implemented yet, so I use static GREP value while developing]
                        will ask to type GREP find expresion to search for text placeholder, that need to be replaced with file from source folder we just selected.
                      
                        TODO: Check how it works with other than image formats
                            Also, this might be usefull to make anchored frame with column widh, and fit image proportionally
                    */
                    
                    
                    scriptName = decodeURI(File(app.activeScript).name.slice(0, -4)); // detect name of current script without expression
                    
                    
                    function Alert(msg) // function for native-looking alerts
                    {
                        w = new Window ("dialog", scriptName, undefined, {closeButton: true});
                        w.preferredSize = [300,75]; // window width and height
                        w.margins = 15; // window margins
                        w.orientation = "column";
                        w.alignChildren = ["left", "top"];
                        w.add("statictext", undefined, msg);
                        close = w.add ("button", [0,0,96,20], "OK", {name: "Ok"});
                        close.alignment = ["right", "bottom"];
                        close.onClick = function(){exit();}
                        w.show();
                    }
                    
                    
                    main();
                    function main()
                    {
                        app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
                    
                        if(app.documents.length == 0)
                        {
                            Alert("No documents are open. Please open a document and try again."); exit();
                        }
                        else
                        {
                            //> START OF doUndoWraper
                            if (parseFloat(app.version) < 6) // "app.version < 6" if it's running under an earlier version than CS4, as earlier versions don't support "Undo" in scripts
                                doUndoWrapper();
                            else
                                app.doScript(doUndoWrapper, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, scriptName);
                            //< END OF doUnoWraper
                        }
                    }
                    
                    
                    function doUndoWrapper() // this is the wraper function UNDO everything script made  by single undo
                    {// START OF doUndoWrapper
                    
                    
                    defaultGREPexpression = "(?i)^[a-z0-9 _-]+\\.\\w{2,4}$"; // Paragraph that starts with upper or lower case latin character, digits, spaces, hyphen or underscore, and ends with .extension
                    
                    
                    var myFilteredFiles;
                    
                    
                    var myExtensions = []; // initialize array
                    
                    
                    myExtensions.push(".jpg", ".jpeg", ".png", ".gif"); // raster images
                    myExtensions.push(".psd", ".tif", ".tiff", ".pdf"); // raster images (layered)
                    myExtensions.push(".ai", ".eps", ".svg", ".cdr"); // vector graphics
                    myExtensions.push(".mp3"); // audio files
                    myExtensions.push(".mp4"); // video files
                    myExtensions.push(".swf"); // flash files
                    myExtensions.push(".doc", ".docx", ".rtf", ".txt"); // text documents
                    myExtensions.push(".xls", ".xlsx"); // table documents  
                    
                    
                    //Display the folder browser.
                    if(app.activeDocument.saved) // our document was saved before - we suggest to start search for source folder from were InDesign file saved
                    {
                        var myFolder =  Folder(app.activeDocument.filePath).selectDlg("Select the source folder with files for placing", "");
                    }
                    else // file was not saved before, so we don't know where to search > suggest to start from Desktop
                    {
                        var myFolder = Folder.selectDialog("Select the source folder with files for placing", "");
                    }
                    
                    
                    if(myFolder) // if folder was selected
                    {
                        //Get the path to the folder containing the files you want to place.
                        var files = new Object(); // This will assoc array with FILE_NAME => FILE_EXTENSION
                      
                        if(File.fs == "Macintosh")
                        {
                            myFilteredFiles = myMacOSFileFilter(myFolder);
                        }
                        else
                        {
                            myFilteredFiles = myWinOSFileFilter(myFolder);
                        }
                    
                    
                        if(myFilteredFiles.length != 0) // success: we have found supported files to place
                        {
                            for(i = 0; i < myFilteredFiles.length; i++)
                            {
                                var filename = myFilteredFiles[i].fsName.toString().replace(/^.*[\\\/]/, ""); // now we get only file names with extenstions
                              
                                var file = [];
                                file = filename.split("."); // separate file name from file extension          
                              
                                files[file[0]] = file[1]; // write FILE_NAME => FILE_EXTENSION as assoc array
                            }
                        }
                        else // error: There is no supported files for placing in specified folder
                        {
                            Alert("ERROR: There is no supported files for placing in specified folder.");
                            exit();
                        }
                    }
                    else // ERROR: we have not choose source folder
                    {
                        Alert("Folder with source files was not specified"); exit();
                    }
                    
                    
                    //Windows version of the file filter.
                    function myWinOSFileFilter(myFolder)
                    {
                      var myFiles = new Array;
                      var myFilteredFiles = new Array;
                      for(myExtensionCounter = 0; myExtensionCounter < myExtensions.length; myExtensionCounter++)
                        {
                            myExtension = myExtensions[myExtensionCounter];
                            myFiles = myFolder.getFiles("*"+ myExtension);
                      if(myFiles.length != 0)
                            {
                                for(var myFileCounter = 0; myFileCounter < myFiles.length; myFileCounter++)
                                {
                      myFilteredFiles.push(myFiles[myFileCounter]);
                                }
                            }
                      }
                      return myFilteredFiles;
                    }
                    
                    
                    function myMacOSFileFilter(myFolder)
                    {
                      var myFilteredFiles = myFolder.getFiles(myFileFilter);
                      return myFilteredFiles;
                    }
                    //Mac OS version of file filter
                    //Have to provide a separate version because not all Mac OS users use file extensions and/or file extensions are sometimes hidden by the Finder.
                    function myFileFilter(myFile)
                    {
                        var myFileType = myFile.type;
                        switch (myFileType)
                        {
                            case "JPEG":
                            case "EPSF":
                            case "PICT":
                            case "TIFF":
                            case "8BPS":
                            case "GIFf":
                            case "PDF ":
                                return true;
                                break;
                            default:
                            for(var myCounter = 0; myCounter<myExtensions.length; myCounter++)
                            {
                                var myExtension = myExtensions[myCounter];
                                if(myFile.name.indexOf(myExtension)>-1)
                                {
                                    return true;
                                    break;
                                }
                            }
                      }
                      return false;
                    }
                    
                    
                    //> START OF GREP expression dialog
                    w = new Window ("dialog", scriptName+": specify expression", undefined, {closeButton: true});
                    w.preferredSize = [300,75]; // window width and height
                    w.margins = 15; // window margins
                    w.orientation = "column";
                    w.alignChildren = ["left", "top"];
                    panel = w.add("panel", undefined, "Find what: (GREP expression)");
                    
                    
                    if(app.findGrepPreferences.findWhat != "")
                    {
                        grepExpression = app.findGrepPreferences.findWhat;
                        clearFindWhat = false;
                    }
                    else
                    {  
                        grepExpression = defaultGREPexpression; // use default GREP expression
                        //fgrepExpression = "\\[.+\\]"; // \\< means "begining of the world", and \\> means end of the world;
                        clearFindWhat = true;
                    }
                    
                    
                    var grepExpression = panel.add ("edittext", [0,0,270,20], grepExpression);
                    grepExpression.active = true;
                    
                    
                    panel.add("statictext", undefined, "GREP expression no need for for double \\\\ escaping");
                    
                    
                    ok = w.add ("button", [0,0,96,20], "Continue", {name: "Ok"});
                    ok.alignment = ["right", "bottom"];
                    
                    
                    ok.onClick = function()
                    {  
                        findWhat = grepExpression.text;  
                        w.hide();
                    }
                    w.show();
                    //< END OF GREP expression dialog
                    
                    
                    if(typeof findWhat !== "undefined") // check we have not left GREP expression field empy
                    {
                        app.findGrepPreferences.findWhat = findWhat; // our GREP that search for image placeholder text;
                    
                    
                        found = app.activeDocument.findGrep();
                    
                    
                        for(i=0; i < found.length; i++)
                        {
                            if(found[i].contents.indexOf(".") > -1) // we wroking with file name with extension
                            {
                                foundElem = new File (myFolder + "/" + found[i].contents);
                            }
                            else // we work with file name only, so we need to add file extension manually
                            {
                                found[i].contents = found[i].contents.replace(/[^a-z0-9 _-]/gi, ""); // remove all unwanted characters from file name: only letters, numbers, spcaces, minus and underscores allowed      
                                foundElem = new File (myFolder + "/" + found[i].contents + "." +  files[found[i].contents]);      
                            }  
                        
                          try
                            {
                                placedObj = found[i].place(foundElem)[0].parent; // THANKS TO: Jump_Over for help @ https://forums.adobe.com/message/6912489#6912489
                              
                                placedObj.anchoredObjectSettings.anchoredPosition = AnchorPosition.ABOVE_LINE;
                                placedObj.anchoredObjectSettings.horizontalAlignment = HorizontalAlignment.TEXT_ALIGN;
                                placedObj.frameFittingOptions.autoFit = true;
                                placedObj.frameFittingOptions.fittingOnEmptyFrame = EmptyFrameFittingOptions.FILL_PROPORTIONALLY;
                                placedObj.frameFittingOptions.fittingAlignment = AnchorPoint.CENTER_ANCHOR;          
                            }
                            catch(e)
                            {
                                Alert(e);          
                            }  
                        }
                        app.changeGrepPreferences.changeTo = "";
                        app.activeDocument.changeGrep();
                    
                    
                        if(clearFindWhat) // clearing only if typed GREP expression manually
                        {
                            app.findGrepPreferences = app.changeGrepPreferences = null; // clear Find/Change preferences once we finished
                        }
                    }
                    else
                    {
                        Alert("Find what GREP expression was not specified"); exit();
                    }
                    
                    
                    }// END OF doUndoWrapper
                    
                    

                    Top part with Windows/Mac filtering was copy-pasted from default InDeign script "ImageCatalog.jsx" as example and modified - I'm not sure if all this stuff is needed, I haven't test if it works the same without those filtering on both OS - if that's not needed - then thanks for tip!

                    PS: in your findWhat \l{3,4} will not catch .ai files

                    and what means .source at the end? is that doing necessary escaping, so with it it's posible to write \l instead of \\l

                    • 7. Re: Re: Re: HOW TO: set anchoredObjectSettings for .palce() object?
                      Kai Rübsamen Adobe Community Professional

                      Hi,

                       

                      thanks for your code.

                       

                      "PS: in your findWhat \l{3,4} will not catch .ai files"

                      That’s true. So leave it as your {2,4}

                       

                      "and what means .source at the end? is that doing necessary escaping, so with it it's posible to write \l instead of \\l"

                      Yes. With 'source' you can write it, as you would write it in the UI. This is sometimes better to read. Nothing more.