From e968fdacd58edb15dbe64b5aa28a02c4020b92a8 Mon Sep 17 00:00:00 2001 From: Sergey Osokin Date: Sun, 5 May 2024 23:04:53 +0500 Subject: [PATCH] Added ExtractFromGroup.jsx Extract selected items from parent groups --- README.md | 1 + README.ru.md | 1 + jsx/ExtractFromGroup.jsx | 92 ++++++++++++++++++++++++++++++++++++++++ md/Group.md | 8 ++++ md/Group.ru.md | 8 ++++ 5 files changed, 110 insertions(+) create mode 100644 jsx/ExtractFromGroup.jsx diff --git a/README.md b/README.md index c3e2a06..77e28ca 100755 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ Click the category name to learn more about the scripts in the selected category ### [Group | Mask](md/Group.md) * [CenterClipsToArtboards](https://github.com/creold/illustrator-scripts/blob/master/md/Group.md#centerclipstoartboards) +* [ExtractFromGroup](https://github.com/creold/illustrator-scripts/blob/master/md/Group.md#extractfromgroup) `new, 05.05.2024` * [ExtUngroup](https://github.com/creold/illustrator-scripts/blob/master/md/Group.md#extungroup) * [MoveToGroup](https://github.com/creold/illustrator-scripts/blob/master/md/Group.md#movetogroup) `upd, 09.02.2024` * [TrimMasks](https://github.com/creold/illustrator-scripts/blob/master/md/Group.md#trimmasks) diff --git a/README.ru.md b/README.ru.md index ed3442f..adeece3 100755 --- a/README.ru.md +++ b/README.ru.md @@ -130,6 +130,7 @@ Скрипты для создания и управления группами, обтравочными масками * [CenterClipsToArtboards](https://github.com/creold/illustrator-scripts/blob/master/md/Group.ru.md#centerclipstoartboards) +* [ExtractFromGroup](https://github.com/creold/illustrator-scripts/blob/master/md/Group.ru.md#extractfromgroup) `new, 05.05.2024` * [ExtUngroup](https://github.com/creold/illustrator-scripts/blob/master/md/Group.ru.md#extungroup) * [MoveToGroup](https://github.com/creold/illustrator-scripts/blob/master/md/Group.ru.md#movetogroup) `upd, 09.02.2024` * [TrimMasks](https://github.com/creold/illustrator-scripts/blob/master/md/Group.ru.md#trimmasks) diff --git a/jsx/ExtractFromGroup.jsx b/jsx/ExtractFromGroup.jsx new file mode 100644 index 0000000..cf32d84 --- /dev/null +++ b/jsx/ExtractFromGroup.jsx @@ -0,0 +1,92 @@ +/* + ExtractFromGroup.jsx for Adobe Illustrator + Description: Extract selected items from parent groups + If Alt key is pressed, move item before the first parent group + If Alt key is not pressed, move item before the topmost level parent group + Date: May, 2024 + 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/aiscripts + - via Donatty https://donatty.com/sergosokin + - via DonatePay https://new.donatepay.ru/en/@osokin + - via YooMoney https://yoomoney.ru/to/410011149615582 + + NOTICE: + Tested with Adobe Illustrator CC 2019-2024 (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 (!/illustrator/i.test(app.name)) { + alert('Wrong application\nRun script from Adobe Illustrator', 'Script error'); + return; + } + + if (!documents.length) { + alert('No documents\nOpen a document and try again', 'Script error'); + return; + } + + if (!selection.length || selection.typename == 'TextRange') { + alert('No objects selected\nPlease select object(s) and try again', 'Script error'); + return; + } + + var isAltPressed = ScriptUI.environment.keyboardState.altKey; + + for (var i = 0, len = app.selection.length; i < len; i++) { + var item = app.selection[i]; + if (item.parent.typename === 'CompoundPathItem') { + item = item.parent; + } + var parent = getParentGroup(item, isAltPressed); + + if (parent.typename === 'GroupItem') { + item.move(parent, ElementPlacement.PLACEBEFORE); + } + } +} + +/** + * Find the parent GroupItem of the specified item based on the Alt key status + * @param {Object} item - The item for which to find the parent GroupItem + * @param {boolean} isAltKey - A boolean flag indicating whether the Alt key is pressed or not + * @returns {Object} - The parent GroupItem if found + */ +function getParentGroup(item, isAltKey) { + if (isAltKey) { + if (item.parent.typename === 'GroupItem') { + return item.parent; + } else { + return getParentGroup(item.parent, isAltKey); + } + } else { + var topParent = item; + while (topParent.parent.typename === 'GroupItem') { + topParent = topParent.parent; + } + return topParent; + } +} + +// Run script +try { + main(); +} catch (err) {} \ No newline at end of file diff --git a/md/Group.md b/md/Group.md index 4de1a4b..1b2168c 100755 --- a/md/Group.md +++ b/md/Group.md @@ -13,6 +13,7 @@ ## Scripts * [CenterClipsToArtboards](https://github.com/creold/illustrator-scripts/blob/master/md/Group.md#centerclipstoartboards) +* [ExtractFromGroup](https://github.com/creold/illustrator-scripts/blob/master/md/Group.md#extractfromgroup) `new, 05.05.2024` * [ExtUngroup](https://github.com/creold/illustrator-scripts/blob/master/md/Group.md#extungroup) * [MoveToGroup](https://github.com/creold/illustrator-scripts/blob/master/md/Group.md#movetogroup) `upd, 09.02.2024` * [TrimMasks](https://github.com/creold/illustrator-scripts/blob/master/md/Group.md#trimmasks) @@ -24,6 +25,13 @@ Aligns selected clip groups and their contents to the center of the parent artbo ![CenterClipsToArtboards](https://i.ibb.co/ykHy3rM/Center-Clips-To-Artboards.gif) +## ExtractFromGroup +[![Direct](https://img.shields.io/badge/Direct%20Link-ExtractFromGroup.jsx-FF6900.svg)](https://rebrand.ly/extrgrp) [![Download](https://img.shields.io/badge/Download%20All-Zip%20archive-0088CC.svg)](https://bit.ly/2M0j95N) + +Extracts the selected items from parent groups. By default, each item is moved before the top parent group. If Alt / Option (⌥) key is pressed, move item before the first parent group. + +![ExtractFromGroup](https://i.ibb.co/pK5yzqS/Extract-From-Group.gif) + ## ExtUngroup [![Direct](https://img.shields.io/badge/Direct%20Link-ExtUngroup.jsx-FF6900.svg)](https://rebrand.ly/extungrp) [![Download](https://img.shields.io/badge/Download%20All-Zip%20archive-0088CC.svg)](https://bit.ly/2M0j95N) diff --git a/md/Group.ru.md b/md/Group.ru.md index 6806b91..4a4ba17 100755 --- a/md/Group.ru.md +++ b/md/Group.ru.md @@ -13,6 +13,7 @@ ## Scripts * [CenterClipsToArtboards](https://github.com/creold/illustrator-scripts/blob/master/md/Group.ru.md#centerclipstoartboards) +* [ExtractFromGroup](https://github.com/creold/illustrator-scripts/blob/master/md/Group.ru.md#extractfromgroup) `new, 05.05.2024` * [ExtUngroup](https://github.com/creold/illustrator-scripts/blob/master/md/Group.ru.md#extungroup) * [MoveToGroup](https://github.com/creold/illustrator-scripts/blob/master/md/Group.ru.md#movetogroup) `upd, 09.02.2024` * [TrimMasks](https://github.com/creold/illustrator-scripts/blob/master/md/Group.ru.md#trimmasks) @@ -24,6 +25,13 @@ ![CenterClipsToArtboards](https://i.ibb.co/ykHy3rM/Center-Clips-To-Artboards.gif) +## ExtractFromGroup +[![Direct](https://img.shields.io/badge/Прямая%20ссылка-ExtractFromGroup.jsx-FF6900.svg)](https://rebrand.ly/extrgrp) [![Download](https://img.shields.io/badge/Скачать%20все-Zip--архив-0088CC.svg)](https://bit.ly/2M0j95N) + +Извлекает выбранные элементы из групп. По умолчанию каждый элемент извлечётся перед самой верхней группой. Если зажата клавиша Alt / Option (⌥), то элемент переместится перед первой родительской группой. + +![ExtractFromGroup](https://i.ibb.co/pK5yzqS/Extract-From-Group.gif) + ## ExtUngroup [![Direct](https://img.shields.io/badge/Прямая%20ссылка-ExtUngroup.jsx-FF6900.svg)](https://rebrand.ly/extungrp) [![Download](https://img.shields.io/badge/Скачать%20все-Zip--архив-0088CC.svg)](https://bit.ly/2M0j95N)