11 Replies Latest reply on Sep 9, 2011 1:55 PM by Andres_Mendoza

    ParagraphStyleGroups: am I the last to realize?

    Dave Saunders Level 4

      If you issue:

       

      myPstyle = app.documents[0].paragraphStyles.item("SomeName").getElements()[0];

       

      where "SomeName" is the name of a ParagraphStyleGroup then myPstyle will indeed be a reference to said ParagraphStyleGroup.

       

      You must do the getElements()[0] to get this to work otherwise you the item thinks its a paragraph style with the wrong properties.

       

      Dave

        • 1. Re: ParagraphStyleGroups: am I the last to realize?
          Harbs. Level 6

          That sounds to me like a bug...

           

          Harbs

          • 2. Re: ParagraphStyleGroups: am I the last to realize?
            Dave Saunders Level 4

            I'm not so sure. It's very useful when analyzing a treeview that has the paragraph styles/groups in it and you want to know what corresponds to the selection.

             

            It reminds me of the way that itemByID behaves or even how PageItems behave (a bit).

             

            Dave

            • 3. Re: ParagraphStyleGroups: am I the last to realize?
              Dave Saunders Level 4

              Here's the function I'm using:

               

              function getParaStyle(obj) {
                  // obj is a listItem in a treeview
                  // function returns a paragraph style, a style group,

                  // a document or the application
                  // depending on what the listItem is and

                  // whether or not a document is open
                  var names = new Array();
                  while (obj.parent instanceof TreeView === false) {
                      names.push(obj.text);
                      obj =obj.parent;
                  }
                  var range = app.documents.length === 0
                      ? app
                      : app.documents[0];
                  if (names.length === 0) return range;
                  var folder = range;
                  while (names.length > 1) {
                      var name = names.pop();
                      folder  = folder.paragraphStyleGroups.item(name);
                  }
                  return folder.paragraphStyles.item(names[0]).getElements()[0];
              }

               

              Dave

              • 4. Re: ParagraphStyleGroups: am I the last to realize?
                Harbs. Level 6

                Hi Dave,

                 

                I have a very cool function for what you're trying to do. If you're 

                interested, let me know...

                 

                Harbs

                • 5. Re: ParagraphStyleGroups: am I the last to realize?
                  Dave Saunders Level 4

                  Now that's I've noodled it out this far, it won't feel like cheating to see how you did it, so yes please, I'm interested.

                   

                  Thanks,

                   

                  Dave

                  • 6. Re: ParagraphStyleGroups: am I the last to realize?
                    Harbs. Level 6

                    I sent you an email...

                     

                    Harbs

                    • 7. Re: ParagraphStyleGroups: am I the last to realize?
                      Peter Kahrel Adobe Community Professional & MVP

                      Harbs,

                       

                      >If you're interested, let me know...

                       

                      Can we all have a peek? I'm intrigued now!

                       

                      Peter

                      • 8. Re: ParagraphStyleGroups: am I the last to realize?
                        Harbs. Level 6

                        Hi Peter,

                         

                        I was rather proud of my method and I wasn't really planning on sharing this with the whole world. It takes a total of two lines of code to create the tree view, and no code at all to get the references.

                         

                        But, what the heck...

                         

                        Here's the function I use to build the tree structure:

                         

                            GetStylesWithGroups = function (doc,groupType,styleType,parent,dontSkipFirst){
                                var retVal=[];
                                var styles = parent[styleType];
                                for(var i=0;i<styles.length;i++){
                                    if(!dontSkipFirst){
                                        if(styles[i]==doc[styleType][0]){continue}
                                    }
                                    retVal.push({
                                        type:"style",
                                        name:styles[i].name,
                                        reference:styles[i]
                                        });
                                    }
                                var styleGroups = parent[groupType];
                                for(var i=0;i<styleGroups.length;i++){
                                    retVal.push({
                                        type:"node",
                                        name:styleGroups[i].name,
                                        reference:styleGroups[i],
                                        children:GetStylesWithGroups(doc,groupType,styleType,styleGroups[i],dontSkipFirst)
                                        });
                                    }
                                return retVal;
                                }

                         

                        It's usable with any kind of style. If you want the base (no style) to appear, dontSkipFirst should be true.

                         

                        To get the structure of paragraph styles, you'd do something like this:

                        styleStructure = GetStylesWithGroups (doc,"paragraphStyleGroups","paragraphStyles",doc,true);

                         

                        I then create the tree like this:

                        treeview = new HarbsUI.TreeView(w.group,styleStructure,"Tree Name",undefined,yourBounds);

                         

                        Here's the function which creates the tree view.

                         

                        HarbsUI = {}
                            HarbsUI.TreeView = function(parent,structure,name,nodeIcon,bounds){
                                if(bounds instanceof Array){
                                    var width = bounds[0] || undefined;
                                    var height = bounds[1] || undefined;
                                }
                                // Create the panel for the TreeView
                                var panel = parent.add("panel", undefined, name);
                                panel.alignment = ["fill", "fill"];
                                panel.alignChildren = ["fill", "fill"]
                            
                                // Create the TreeView list
                                var treeview = panel.add("treeview");
                                if(width){treeview.preferredSize.width = width;}
                                if(height){treeview.preferredSize.height = height;}
                                AddItemsToTreeView(treeview,structure,nodeIcon);
                                return treeview;
                                function AddItemsToTreeView(parent,structure,icon){
                                    for(var i=0;i<structure.length;i++){
                                        if(structure[i].type=="node"){
                                            var node = parent.add("node",structure[i].name);
                                            if(icon){node.image = icon;}
                                            node.reference = structure[i].reference;
                                            AddItemsToTreeView(node,structure[i].children,icon);
                                        }
                                        else{
                                            var item = parent.add("item",structure[i].name);
                                            item.reference = structure[i].reference;
                                        }
                                    }
                                    //return parent;
                                }
                            }

                         

                        Then any time the selection changes in your tree view, you can just refer to the selection.reference and you'll have a valid reference to your style or group! It works great!

                        • 9. Re: ParagraphStyleGroups: am I the last to realize?
                          Peter Kahrel Adobe Community Professional & MVP

                          Very nice work! The dialog looks good -- but you can't select multiple styles. I've been using the function quoted below. It uses a list rather than a tree as you can select multiple items in a list. It doesn't look as nice as a treeview (I should tart it up using indents or something like that), and it doesn't generalise across style types (this one does just character styles).

                           

                          Peter

                           

                          function select_character_styles ()
                              {
                              var cs_array = fetch_stylenames (app.activeDocument.allCharacterStyles);
                              var w = new Window ('dialog', 'Character styles', undefined, {resizeable: true});
                              w.alignChildren = ['fill','fill'];
                              var cs_list = w.add ('listbox', undefined, cs_array, {multiselect:true});
                              cs_list.maximumSize.height = 800;

                              var buttons = w.add ('group');
                                  buttons.alignChildren = ['right', 'bottom'];
                                  buttons.orientation = 'row';
                                  buttons.add ('button', undefined, 'OK', {name: 'ok'});
                                  buttons.add ('button', undefined, 'Cancel', {name:'cancel'});

                              function get_selected_styles (listbox)
                                  {
                                  var array = [];
                                  for (var i = 0; i < listbox.items.length; i++)
                                      if (listbox.items[i].selected)
                                          array.push (listbox.items[i].text)
                                  return array
                                  }

                              function fetch_stylenames (all_names)
                                  {
                                  for (var i = 0; i < all_names.length; i++)
                                      all_names[i] = fetch_path (all_names[i], all_names[i].name);
                                  all_names.shift();
                                  return all_names;

                                  function fetch_path (s, str)
                                      {
                                      while (s.parent.constructor.name != 'Document')
                                          return fetch_path (s.parent, s.parent.name + ' > ' + str);
                                      return str;
                                      }
                                  }

                              if (w.show () == 1)
                                  return get_selected_styles (cs_list)
                              else
                                  exit()
                              }

                          • 10. Re: ParagraphStyleGroups: am I the last to realize?
                            Andres_Mendoza Level 1

                            Harbs. wrote:

                             

                            Hi Dave,

                             

                            I have a very cool function for what you're trying to do. If you're 

                            interested, let me know...

                             

                            Harbs

                             

                            Hello Harbs,

                             

                            I need to move out every ParagraphStyle from its folder recursively, and delete these empty groups. This is, each ParagraphStyle should be to be part of paragraphStyles, and not paragraphStyleGroups.[anyGroup].

                             

                            The function you mention would help in this case?

                             

                            Right now I am using the move function. This works fine if you want to change its parent group... but I don't know what parameter to send if you want it to be not part of any group:

                             

                            actualParagraphStyleFromGroupToMove = myParagraphStyleGroups[0].paragraphStyles[0].move(LocationOptions.atEnd, myParagraphStyleGroups[1])

                             

                            Note: ParagraphStyle names should be different.

                             

                            Thanks in advance,

                            Andres

                            • 11. Re: ParagraphStyleGroups: am I the last to realize?
                              Andres_Mendoza Level 1

                              I found it:

                              Each one of them has to be moved to the "Document" object:

                               

                              actualParagraphStyleFromGroupToMove = myParagraphStyleGroups[0].paragraphStyles[0].move(LocationOptions.atEnd, app.activeDocument);

                               

                              Now I just have to realize how to search from groups within groups.