I've written a function that returns true when the item has dashes aligned. Figured I should post it here for anyone who happens along later.
- Mark
Edit: now works with compoundPaths.
/*
Get Stroke Dashes Alignment Style
For Adobe Illustrator 2022
by m1b,
here: https://community.adobe.com/t5/illustrator-discussions/script-api-for-stroke-align-dashes-to-path-corners/td-p/12689425
This function returns true if the item's stroke
dash alignment is set to 'aligning to corners
and path ends, adjusting lengths to fit'.
At this time there is no API access to this setting
so this method is quite hacky: it converts a model
duplicate of the item into outline stroke
and counts the number of dashes.
`item` must be a pathItem or compoundPathItem
and must have a dashed stroke.
*/
alert(strokeDashesAreAligned(app.activeDocument.selection[0]));
function strokeDashesAreAligned(item, keepSelection) {
if (
item == undefined || !(item.hasOwnProperty('pathPoints') || item.hasOwnProperty('pathItems'))
) return;
var dashesAreAligned;
// we only worry about storing the
// selection because the outline
// stroke test later destroys it
keepSelection = keepSelection || true;
var doc = getParentDocument(item),
selectedItems = [];
if (keepSelection) {
for (var i = 0; i < doc.selection.length; i++) {
selectedItems.push(doc.selection[i]);
}
}
if (item.typename == 'CompoundPathItem') {
// COMPOUND PATH ITEM
if (
item.pathItems[0].stroked == false
|| item.pathItems[0].strokeDashes.length == 0
) return;
// duplicate
var dup = item.duplicate();
doc.selection = null;
dup.selected = true;
// uncompound
app.executeMenuCommand('noCompoundPath');
// collect the resulting path items
var pathItemsToDelete = [];
for (var i = 0; i < doc.selection.length; i++)
pathItemsToDelete.push(doc.selection[i]);
// get result on just the first path
dashesAreAligned = strokeDashesAreAligned(pathItemsToDelete[0], keepSelection);
// remove uncompounded duplicates
for (var i = pathItemsToDelete.length - 1; i >= 0; i--)
pathItemsToDelete[i].remove();
} else {
// PATH ITEM
if (
item.stroked == false
|| item.strokeDashes.length == 0
) return;
// duplicate item
var modelPathItem = item.duplicate();
// standardise model
var standardPoints = [[0, 0], [3, 0]];
modelPathItem.filled = false;
modelPathItem.stroked = true;
modelPathItem.strokeWidth = 0.1;
modelPathItem.strokeDashes = [1];
modelPathItem.closed = false;
for (var i = modelPathItem.pathPoints.length - 1; i >= 0; i--) {
if (i >= standardPoints.length) {
modelPathItem.pathPoints[i].remove();
} else {
modelPathItem.pathPoints[i].anchor = standardPoints[i];
modelPathItem.pathPoints[i].leftDirection = standardPoints[i];
modelPathItem.pathPoints[i].rightDirection = standardPoints[i];
}
}
// convert to outlined stroke
// must clear selection to do this
doc.selection = null;
modelPathItem.selected = true;
app.executeMenuCommand('OffsetPath v22');
modelPathItem = doc.selection[0];
// the model, once converted to compoundPathItem,
// will have 2 pathItems if dashes are not aligned,
// or 3 if dashes are aligned
var dashesAreAligned = modelPathItem.pathItems.length == 3;
// remove the model and reselect the original
modelPathItem.remove();
// re-instate inital selection
for (var i = 0; i < selectedItems.length; i++) {
selectedItems[i].selected = true;
}
}
return dashesAreAligned;
}
function getParentDocument(obj) {
while (obj.hasOwnProperty('parent') && obj.constructor.name != 'Document')
obj = obj.parent;
return obj;
}