6 Replies Latest reply on Mar 21, 2017 9:52 AM by karaw65065845

    Script to set size of object that has object style applied

    kschroeder78

      many of my projects include catalogs, and one of the most tedious tasks is when the client asks me to change the size of the color swatches throughout the catalog. Can a script be written to define the size of the objects with the object style "swatch" applied, and then if they need to be changed, to edit the script to change the size?

        • 1. Re: Script to set size of object that has object style applied
          [Jongware] Most Valuable Participant

          It's far easier to write a script that only sets the size of the objects to a predefined value, whether they "need to be changed" or not. (This does assume that all objects with this style should be set to the same size -- which is how I understand your post.)

           

          The top 2 lines in this quick & dirty javascript are the new width & height of the objects, in your current measurement units. The script changes the size from the top left point; that point stays on the original location. It might or might not be what you are doing manually -- of course, just about every other way is possible as well.

           

          This is a javascript. Copy, paste into a plain text editor (Notepad, TextEdit in plain text mode, or Adobe's own ESTK Editor). Save as "ChangeSwatchObjectSize.jsx" into your User Scripts folder. Double-click to run.

           

          //DESCRIPTION:Change all Swatch Objects size to a predefined value
          
          change_width = 10;
          change_height = 20;
          for (a=0; a<app.activeDocument.allPageItems.length; a++)
          {
           if (app.activeDocument.allPageItems[a].appliedObjectStyle.name == "swatch")
           {
            gb = app.activeDocument.allPageItems[a].geometricBounds;
          
            app.activeDocument.allPageItems[a].geometricBounds = [gb[0], gb[1], gb[0]+change_height, gb[1]+change_width];
          
           }
          }
          
          1 person found this helpful
          • 2. Re: Script to set size of object that has object style applied
            kschroeder78 Level 1

            Thanks so much, that's exactly what I needed!


            • 3. Re: Script to set size of object that has object style applied
              [Jongware] Most Valuable Participant

              You're welcome. Please mark the question "Answered"..

              • 4. Re: Script to set size of object that has object style applied
                Martin Fischer Level 2

                You also may give the following script a try (in german language, sorry about that).

                 

                 
                
                // setBoundsOfObjectsWithAppliedObjectStyle.jsx   
                 
                //DESCRIPTION: change the metrics of objects applied with certain object style
                // 2010/02   
                   
                var doc = app.documents.firstItem();   
                var the_operator = ['absolut', 'addieren', 'multiplizieren', 'prozentual skalieren'];  
                var r = get_param(doc.objectStyles.everyItem().getElements().slice(0),  the_operator);   
                var  f_objects = get_objects ( r.style);   
                apply_values(f_objects, r, the_operator);   
                   
                   
                function apply_values(the_objects, r)   
                {   
                    for (var i = the_objects.length - 1; i >= 0; i--)   
                    {   
                        var gb = the_objects[i].visibleBounds;   
                        if (r.op == the_operator[0]) //'absolut')   
                        {   
                            var _y2 = (r.height != 0)   
                                ? gb[0] + r.height   
                                : gb[2];   
                            var _x2 = (r.width != 0)   
                                ? gb[1] + r.width   
                                : gb[3];   
                        }   
                        else if (r.op == the_operator[2] || r.op == the_operator[3]) //'multiplizieren' oder 'prozentual skalieren'  
                        {   
                            var the_div = (r.op == the_operator[2]) //'multiplizieren')  
                                ? 1  
                                : 100;  
                            var _y2 = (r.height != 0)   
                                ? gb[0] + (gb[2]-gb[0]) * r.height/the_div   
                                : gb[2];   
                            var _x2 = (r.width != 0)   
                                ? gb[1] + (gb[3]-gb[1]) * r.width/the_div   
                                : gb[3];   
                        }   
                        else if (r.op == the_operator[1]) //'addieren' )  
                        {  
                            var _y2 = gb[2] + r.height;   
                            var _x2 = gb[3] + r.width;  
                        }  
                        the_objects[i].visibleBounds = [gb[0], gb[1],  _y2 , _x2];    
                    }   
                }   
                   
                function get_objects(a_style)   
                {   
                    app.findObjectPreferences = null;   
                    app.findChangeObjectOptions.objectType = ObjectTypes.ALL_FRAMES_TYPE;   
                    app.findObjectPreferences.appliedObjectStyles =a_style;   
                    var the_result = doc.findObject();   
                    app.findObjectPreferences = null;   
                    return the_result;   
                }   
                   
                function get_param(_styles, the_operator)   
                {   
                    var style_names = new Array;   
                    //Ausklammern von '[Einfaches Raster]'   
                    for (var a = 1; a < _styles.length; a++)   
                        style_names.push(_styles[a].name);   
                    var min_width = 140;  
                    var field_width = 40;  
                    var myDialog = app.dialogs.add({name:"Breite und Höhe von Objekten verändern"});    
                    with( myDialog.dialogColumns.add())    
                    {    
                        with(dialogRows.add())    
                        {   
                            with(dialogColumns.add())    
                            {    
                                staticTexts.add({staticLabel:"Objektstil:",    minWidth:min_width});    
                            }    
                            with(dialogColumns.add())    
                            {    
                                var myStylesDropDown = dropdowns.add({stringList:style_names,    selectedIndex:style_names.length-1, minWidth:min_width});    
                            }    
                        }    
                        with(dialogRows.add())    
                        {    
                            with(dialogRows.add())    
                            {    
                                with(dialogColumns.add())    
                                {    
                                    staticTexts.add({staticLabel:"Breite:",    minWidth:min_width});    
                                }    
                                with(dialogColumns.add())    
                                {    
                                    var myWidthTextEditbox = textEditboxes.add({editContents:"0",    minWidth:field_width});    
                                }    
                            }    
                            with(dialogRows.add())    
                            {    
                                with(dialogColumns.add())    
                                {    
                                    staticTexts.add({staticLabel:"Höhe:",    minWidth:min_width});    
                                }    
                                with(dialogColumns.add())    
                                {    
                                    var myHeightTextEditbox = textEditboxes.add({editContents:"0",    minWidth:field_width});    
                                }    
                            }    
                            with ( dialogRows.add () ) {    
                                with(dialogColumns.add())  {                      
                                staticTexts.add( {staticLabel: "Operation: ", minWidth: min_width} );  
                                }  
                                with(dialogColumns.add())    
                                {  
                                    var radio_buttons = radiobuttonGroups.add();  
                                    with(radio_buttons) {  
                                        radiobuttonControls.add({staticLabel: the_operator[0], checkedState: true, minWidth: min_width});  
                                        radiobuttonControls.add({staticLabel: the_operator[1], checkedState: false, minWidth: min_width});  
                                        radiobuttonControls.add({staticLabel: the_operator[2], checkedState: false, minWidth: min_width});  
                                        radiobuttonControls.add({staticLabel: the_operator[3], checkedState: false, minWidth: min_width});  
                                    }  
                                }  
                            }  
                        }   
                    }    
                    var myResult = myDialog.show();    
                    if(myResult == true)    
                    {    
                        var a_style = doc.objectStyles.item(myStylesDropDown.selectedIndex);    
                        var a_width = Number(myWidthTextEditbox.editContents.replace(',','.'));   
                        var a_height = Number(myHeightTextEditbox.editContents.replace(',','.'));   
                        var a_operator = the_operator[radio_buttons.selectedButton]   
                        if (isNaN (a_width))   
                            a_width = 0;   
                        if (isNaN (a_height))   
                            a_height = 0;   
                        myDialog.destroy();    
                    }    
                    else    
                    {    
                        myDialog.destroy();    
                        exit();   
                    }    
                    return {style:a_style, width: a_width, height: a_height, op: a_operator};    
                }  
                

                 

                Kind regards

                Martin Fischer

                • 5. Re: Script to set size of object that has object style applied
                  listseekers

                  Hi Martin,

                   

                  I have a Similar problem which is posted in this Forum:

                   

                  http://forums.adobe.com/community/international_forums/deutsche?view=d iscussions&amp;start=0

                   

                  I can see that you are very good in scripting, mabe you can help me.

                   

                  Vielen Dank.

                  • 6. Re: Script to set size of object that has object style applied
                    karaw65065845

                    Hi,

                    I don't know how to write a new script.  Can anyone walk me through this?  Its exactly what I need.

                    Thanks!