select paragraph style from dropdown and dealing with targeting styles in folder

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / select paragraph style from dropdown and dealing with targeting styles in folder

Viewing 2 reply threads
  • Author
    Posts
    • #1237723
      Justin Sligh
      Member

      I have a script which I have hard coded the name of a paragraph style to be used in a script. I would like a user facing dropdown so that the end user could select the particular paragraph style that they want to use.

      Another issue is that I’m having problems with more complex paragraph style names. For example “TestParagraphStyle” but how would I handle something like “Test – Paragraph Style” in the folder “example” which is within the folder “2020”

      Im using

      app.findGrepPreferences.appliedParagraphStyle=”TestParagraphStyle”;

    • #1238013
      Keith Gilbert
      Participant

      Justin,

      When thinking about having ID search for a Pstyle in a style group, you should also consider what if it is in a style group within a style group, or nested even further? Here is a function that calls another function that takes care of this. I didn’t write this…I got it from another scripter (probably the very-smart Kris Coppieters years ago). But have used it in many, many scripts and it works great.

      // Here is an example of how to call the function
      app.changeGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.itemByID(locateParagraphStyle(“Table body, cond, center”));

      // Function to find a paragraph style possibly contained inside a style group. Return id of the paragraph style.
      function locateParagraphStyle(styleNameToLocate, pathSeparator) {
      var myDoc = app.activeDocument;
      var rootPath = “”;
      if (pathSeparator !== undefined) {
      rootPath = pathSeparator;
      }
      var retVal = locateParagraphStyleRecursive(myDoc, styleNameToLocate, pathSeparator, rootPath);
      if (! retVal) {
      return “not found”;
      }
      return retVal.id;
      }
      // Function to find paragraph styles that are in style groups. Returns the paragraph style or null if not found.
      function locateParagraphStyleRecursive(paragraphStyleContainer, styleNameToLocate, pathSeparator, parentPath) {
      var retVal = null;
      do {
      try {
      var paragraphStyles = paragraphStyleContainer.paragraphStyles;
      var numParagraphStyles = paragraphStyles.length;
      for (
      var paragraphStyleIdx = 0;
      retVal == null && paragraphStyleIdx < numParagraphStyles; paragraphStyleIdx++) { var paragraphStyle = paragraphStyles.item(paragraphStyleIdx); if (paragraphStyle.isValid) { var paragraphStyleName = paragraphStyle.name; if (pathSeparator) { paragraphStyleName = parentPath + paragraphStyleName; } if (paragraphStyleName == styleNameToLocate) { retVal = paragraphStyle; } } } if (retVal) { break; } var paragraphStyleGroups = paragraphStyleContainer.paragraphStyleGroups; var numParagraphStyleGroups = paragraphStyleGroups.length; for ( var paragraphStyleGroupIdx = 0; retVal == null && paragraphStyleGroupIdx < numParagraphStyleGroups; paragraphStyleGroupIdx++) { var paragraphStyleGroup = paragraphStyleGroups.item(paragraphStyleGroupIdx); if (paragraphStyleGroup.isValid) { var path = parentPath; if (pathSeparator) { var paragraphStyleGroupName = paragraphStyleGroup.name; path += paragraphStyleGroupName + pathSeparator; } retVal = locateParagraphStyleRecursive(paragraphStyleGroup, styleNameToLocate, pathSeparator, path); } } } catch (err) { } } while (false); return retVal; }

    • #1238023
      Keith Gilbert
      Participant

      As far as allowing a user to choose a paragraph style, the talented Harbs made this function available on the Adobe Scripting forum years ago. I’ve used this as well and it works great.

      function myInput (myDoc) {
      // Thanks to Harbs for the pstyle listview code! https://forums.adobe.com/thread/445911
      var 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;ihttps://forums.adobe.com/thread/445911
      function GetStylesWithGroups(doc,groupType,styleType,parent,dontSkipFirst) {
      var retVal=[];
      var styles = parent[styleType];
      for(var i=0;i

Viewing 2 reply threads
  • You must be logged in to reply to this topic.
>