Skip to content

Commit

Permalink
Added AverageStrokesWidth.jsx
Browse files Browse the repository at this point in the history
Averages the stroke width of selected objects.
  • Loading branch information
Sergey Osokin committed Feb 7, 2023
1 parent 7c86c21 commit a7886a0
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Click the category name to learn more about the scripts in the selected category

### [Style](md/Style.md)

* [AverageStrokesWidth](https://github.com/creold/illustrator-scripts/blob/master/md/Style.md#averagestrokeswidth) `(new, 07.02.2023)`
* [ChangeOpacity](https://github.com/creold/illustrator-scripts/blob/master/md/Style.md#changeopacity) `(upd, 01.08.2022)`
* [GrayscaleToOpacity](https://github.com/creold/illustrator-scripts/blob/master/md/Style.md#grayscaletoopacity)
* [MakeTrappingStroke](https://github.com/creold/illustrator-scripts/blob/master/md/Style.md#maketrappingstroke) `(new, 14.12.2022)`
Expand Down
1 change: 1 addition & 0 deletions README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
### [Style](md/Style.ru.md)
Скрипты, стилизующие объекты

* [AverageStrokesWidth](https://github.com/creold/illustrator-scripts/blob/master/md/Style.ru.md#averagestrokeswidth) `(new, 07.02.2023)`
* [ChangeOpacity](https://github.com/creold/illustrator-scripts/blob/master/md/Style.ru.md#changeopacity) `(upd, 01.08.2022)`
* [GrayscaleToOpacity](https://github.com/creold/illustrator-scripts/blob/master/md/Style.ru.md#grayscaletoopacity)
* [MakeTrappingStroke](https://github.com/creold/illustrator-scripts/blob/master/md/Style.ru.md#maketrappingstroke) `(new, 14.12.2022)`
Expand Down
150 changes: 150 additions & 0 deletions jsx/AverageStrokesWidth.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
AverageStrokesWidth.jsx for Adobe Illustrator
Description: Averages the stroke width of selected objects, skipping those without strokes. Supports paths, compound paths and text objects
Date: February, 2023
Author: Sergey Osokin, email: hi@sergosokin.ru
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.1 Initial version
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee: https://www.buymeacoffee.com/osokin
- via FanTalks https://fantalks.io/r/sergey
- via DonatePay https://new.donatepay.ru/en/@osokin
- via YooMoney https://yoomoney.ru/to/410011149615582
NOTICE:
Tested with Adobe Illustrator CC 2019-2023 (Mac/Win).
This script is provided "as is" without warranty of any kind.
Free to use, not for sale
Released under the MIT license
http://opensource.org/licenses/mit-license.php
Check my other scripts: https://github.com/creold
*/

//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file

// Main function
function main() {
if (!isCorrectEnv('selection')) return;

var items = getStrokedItems(selection, true);
if (!items.length) return;

var totalW = getTotalWidth(items),
avgW = 1 * (totalW / items.length).toFixed(2);

items = getStrokedItems(selection, false);
for (var i = 0, len = items.length; i < len; i++) {
setStrokeWidth(items[i], avgW);
}
}

// Check the script environment
function isCorrectEnv() {
var args = ['app', 'document'];
args.push.apply(args, arguments);
for (var i = 0; i < args.length; i++) {
var arg = args[i].toString().toLowerCase();
switch (true) {
case /app/g.test(arg):
if (!/illustrator/i.test(app.name)) {
alert('Wrong application\nRun script from Adobe Illustrator', 'Script error');
return false;
}
break;
case /version/g.test(arg):
var rqdVers = parseFloat(arg.split(':')[1]);
if (parseFloat(app.version) < rqdVers) {
alert('Wrong app version\nSorry, script only works in Illustrator v.' + rqdVers + ' and later', 'Script error');
return false;
}
break;
case /document/g.test(arg):
if (!documents.length) {
alert('No documents\nOpen a document and try again', 'Script error');
return false;
}
break;
case /selection/g.test(arg):
if (!selection.length || selection.typename === 'TextRange') {
alert('Nothing selected\nPlease, select two or more paths', 'Script error');
return false;
}
break;
}
}
return true;
}

// Get paths from selection
function getStrokedItems(coll, isFirstPath) {
var out = [];
for (var i = 0; i < coll.length; i++) {
var item = coll[i];
if (isType(item, 'group') && item.pageItems.length) {
out = [].concat(out, getStrokedItems(item.pageItems));
} else if (isType(item, '^compound') && item.pathItems.length) {
// Get only first path from PathItems collection
if (isFirstPath) {
if (isHasStroke(item.pathItems[0])) out.push(item.pathItems[0]);
} else { // Get all PathItems
out = [].concat(out, getStrokedItems(item.pathItems));
}
} else if (isType(item, 'path|text')) {
if (isHasStroke(item)) out.push(item);
}
}
return out;
}

// Check the item typename by short name
function isType(item, type) {
var regexp = new RegExp(type, 'i');
return regexp.test(item.typename);
}

// Check for a stroke
function isHasStroke(item) {
if (isType(item, 'text')) {
var attr = item.textRange.characterAttributes;
return !/nocolor/i.test(attr.strokeColor) && attr.strokeWeight > 0;
}
return item.stroked && item.strokeWidth > 0;
}

// Get the sum of the stroke widths
function getTotalWidth(coll) {
var total = 0;
for (var i = 0, len = coll.length; i < len; i++) {
if (isType(coll[i], 'text')) {
var attr = coll[i].textRange.characterAttributes;
total += attr.strokeWeight;
} else {
total += coll[i].strokeWidth;
}
}
return total;
}

// Set stroke width
function setStrokeWidth(item, val) {
if (val == undefined || val == 0) return;
if (isType(item, 'text')) {
var attr = item.textRange.characterAttributes;
attr.strokeWeight = val;
} else {
item.strokeWidth = val;
}
}

// Run script
try {
main();
} catch (err) {}
3 changes: 1 addition & 2 deletions jsx/TrimOpenEnds.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee: https://www.buymeacoffee.com/osokin
- via FanTalks https://fantalks.io/r/sergey
- via DonatePay https://new.donatepay.ru/en/@osokin
- via Donatty https://donatty.com/sergosokin
- via YooMoney https://yoomoney.ru/to/410011149615582
- via QIWI https://qiwi.com/n/OSOKIN
NOTICE:
Tested with Adobe Illustrator CC 2019-2023 (Mac/Win).
Expand Down
8 changes: 8 additions & 0 deletions md/Style.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
3. Press <kbd>Cmd/Ctrl</kbd> + <kbd>S</kbd> for download.

## Scripts
* [AverageStrokesWidth](https://github.com/creold/illustrator-scripts/blob/master/md/Style.md#averagestrokeswidth) `(new, 07.02.2023)`
* [ChangeOpacity](https://github.com/creold/illustrator-scripts/blob/master/md/Style.md#changeopacity) `(upd, 01.08.2022)`
* [GrayscaleToOpacity](https://github.com/creold/illustrator-scripts/blob/master/md/Style.md#grayscaletoopacity)
* [MakeTrappingStroke](https://github.com/creold/illustrator-scripts/blob/master/md/Style.md#maketrappingstroke) `(new, 14.12.2022)`
Expand All @@ -19,6 +20,13 @@
* [StrokesWeightUp](https://github.com/creold/illustrator-scripts/blob/master/md/Style.md#strokesweight) `upd, 14.10.2022`
* [StrokesWeightDown](https://github.com/creold/illustrator-scripts/blob/master/md/Style.md#strokesweight) `upd, 14.10.2022`

## AverageStrokesWidth
[![Direct](https://img.shields.io/badge/Direct%20Link-AverageStrokesWidth.jsx-FF6900.svg)](https://rebrand.ly/avgstrwd) [![Download](https://img.shields.io/badge/Download%20All-Zip%20archive-0088CC.svg)](https://bit.ly/2M0j95N)

Averages the stroke width of selected objects, skipping those without strokes. Supports paths, compound paths and text objects.

![AverageStrokesWidth](https://i.ibb.co/3shb651/Average-Strokes-Width.gif)

## ChangeOpacity
[![Direct](https://img.shields.io/badge/Direct%20Link-ChangeOpacity.jsx-FF6900.svg)](http://bit.do/chngopa) [![Download](https://img.shields.io/badge/Download%20All-Zip%20archive-0088CC.svg)](https://bit.ly/2M0j95N)

Expand Down
8 changes: 8 additions & 0 deletions md/Style.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
3. Нажмите <kbd>Cmd/Ctrl</kbd> + <kbd>S</kbd>, чтобы сохранить файл на диск.

## Scripts
* [AverageStrokesWidth](https://github.com/creold/illustrator-scripts/blob/master/md/Style.ru.md#averagestrokeswidth) `(new, 07.02.2023)`
* [ChangeOpacity](https://github.com/creold/illustrator-scripts/blob/master/md/Style.ru.md#changeopacity) `(upd, 01.08.2022)`
* [GrayscaleToOpacity](https://github.com/creold/illustrator-scripts/blob/master/md/Style.ru.md#grayscaletoopacity)
* [MakeTrappingStroke](https://github.com/creold/illustrator-scripts/blob/master/md/Style.ru.md#maketrappingstroke) `(new, 14.12.2022)`
Expand All @@ -19,6 +20,13 @@
* [StrokesWeightUp](https://github.com/creold/illustrator-scripts/blob/master/md/Style.ru.md#strokesweight) `upd, 14.10.2022`
* [StrokesWeightDown](https://github.com/creold/illustrator-scripts/blob/master/md/Style.ru.md#strokesweight) `upd, 14.10.2022`

## AverageStrokesWidth
[![Direct](https://img.shields.io/badge/Прямая%20ссылка-AverageStrokesWidth.jsx-FF6900.svg)](https://rebrand.ly/avgstrwd) [![Download](https://img.shields.io/badge/Скачать%20все-Zip--архив-0088CC.svg)](https://bit.ly/2M0j95N)

Усредняет толщину обводок выбранных объектов, пропуская те, что без обводки. Поддерживает пути, компаунды и текстовые объекты.

![AverageStrokesWidth](https://i.ibb.co/3shb651/Average-Strokes-Width.gif)

## ChangeOpacity
[![Direct](https://img.shields.io/badge/Прямая%20ссылка-ChangeOpacity.jsx-FF6900.svg)](http://bit.do/chngopa) [![Download](https://img.shields.io/badge/Скачать%20все-Zip--архив-0088CC.svg)](https://bit.ly/2M0j95N)

Expand Down

0 comments on commit a7886a0

Please sign in to comment.