Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Scripting stroke alignment (JSX)

New Here ,
Nov 28, 2019 Nov 28, 2019

In the Illustrator JavaScript reference book (2017) there's no documented method of changing the stroke alignment of a pathItem that I can see.

 

I have a set of dynamically created rounded rectangles in a document which all require a stroke to be aligned to the inside of the path. The only other option I can think of is to calculate a new position by subtracting the stroke width from the pathItem width to get a new position for the pathItem but this seems so fiddly to get right.

 

There must be a way to set this option via scripting? 

 

Code so far:

 

      var doc = app.activeDocument;
      var pageItems = doc.pageItems;
      var artLayer = doc.layers.getByName('newFrames');
      
      for(var i = 0; i < pageItems.length; i++)
      {
            if(pageItems[i].name.match("linkedFrame"))
            {
                  var top = pageItems[i].position[1];
                  var left = pageItems[i].position[0];
                  
                  var border = artLayer.pathItems.roundedRectangle
                  (
                        top,       /* y */
                        left,       /* x */
                        new UnitValue(527, 'mm').as('pt'), 
                        new UnitValue(127, 'mm').as('pt'), 
                        new UnitValue(10, 'mm').as('pt'), 
                        new UnitValue(10, 'mm').as('pt') 
                  );
                  
                  var black = new RGBColor();
                  black.red = 255;
                  black.green = 0;
                  black.blue = 0;
                  
                  border.filled = false;
                  border.stroked = true;
                  border.strokeWidth = new UnitValue(2, 'mm').as('pt');
                  border.strokeColor = black;
            }
      } 

 

TOPICS
Scripting
1.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Enthusiast ,
Nov 29, 2019 Nov 29, 2019

I'd never needed to do this before, and I'm surprised it doesn't show as a PathItem property like all the other stroke options too. One way to get around this, though it may be complete overkill and someone might come in and give a much better solution, is to dynamically create an Action which you load, execute, then unload when needed:

 

// Thanks Qwertyfly
// https://community.adobe.com/t5/illustrator/js-cs6-executemenucommand/m-p/5904772#M19673
function setStrokeAlignToInside() {
  if ((app.documents.length = 0)) {
    return;
  }
  var ActionString = [
    "/version 3",
    "/name [ 4",
    "	74657374",
    "]",
    "/isOpen 1",
    "/actionCount 1",
    "/action-1 {",
    "	/name [ 9",
    "		5365745374726f6b65",
    "	]",
    "	/keyIndex 0",
    "	/colorIndex 0",
    "	/isOpen 0",
    "	/eventCount 1",
    "	/event-1 {",
    "		/useRulersIn1stQuadrant 0",
    "		/internalName (ai_plugin_setStroke)",
    "		/localizedName [ 10",
    "			536574205374726f6b65",
    "		]",
    "		/isOpen 0",
    "		/isOn 1",
    "		/hasDialog 0",
    "		/parameterCount 7",
    "		/parameter-1 {",
    "			/key 2003072104",
    "			/showInPalette -1",
    "			/type (unit real)",
    "			/value 1.0",
    "			/unit 592476268",
    "		}",
    "		/parameter-2 {",
    "			/key 1667330094",
    "			/showInPalette -1",
    "			/type (enumerated)",
    "			/name [ 8",
    "				4275747420436170",
    "			]",
    "			/value 0",
    "		}",
    "		/parameter-3 {",
    "			/key 1836344690",
    "			/showInPalette -1",
    "			/type (real)",
    "			/value 10.0",
    "		}",
    "		/parameter-4 {",
    "			/key 1785686382",
    "			/showInPalette -1",
    "			/type (enumerated)",
    "			/name [ 10",
    "				4d69746572204a6f696e",
    "			]",
    "			/value 0",
    "		}",
    "		/parameter-5 {",
    "			/key 1684825454",
    "			/showInPalette -1",
    "			/type (integer)",
    "			/value 0",
    "		}",
    "		/parameter-6 {",
    "			/key 1684104298",
    "			/showInPalette -1",
    "			/type (boolean)",
    "			/value 0",
    "		}",
    "		/parameter-7 {",
    "			/key 1634494318",
    "			/showInPalette -1",
    "			/type (enumerated)",
    "			/name [ 6",
    "				496e73696465",
    "			]",
    "			/value 1",
    "		}",
    "	}",
    "}"
  ].join("\n");
  createAction(ActionString);
  var ActionString = null;
  app.doScript("SetStroke", "test", false);
  app.unloadAction("test", "");
  function createAction(str) {
    var f = new File("~/ScriptAction.aia");
    f.open("w");
    f.write(str);
    f.close();
    app.loadAction(f);
    f.remove();
  }
}

 

We can set the Stroke alignment within an Action, but one problem is that we don't set only the alignment -- we also set the width, dashes, etc. You can record any Action, save it, open the .aia file in a text editor, and replace the ActionString variable above with it's contents to dynamically run it within a script without needing the user to have this action.

 

For the above to work, you'd likely want to clear you app.selection at the beginning of the loop, then set each PathItem.selected to true, run this action just after the stroked = true, then do the styling, and PathItem.selected = false to remove it from the selection so future actions don't affect it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 30, 2019 Nov 30, 2019
LATEST

Salut!

La traduction en français est imprécise, pourriez vous poster une image afin de faciliter la compréhension ?

de elleere

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines