Copy link to clipboard
Copied
I wonder if it is possible to selectAll by Script without using MenuCommand…
app.executeMenuCommand("selectall");
Any simple solution anyone?
Nils
is executeMenuCommand giving you issues?
another easy way is to loop through your layers and turn hasSelectedArtwork property to true
all hidden and locked items will get selected
hi CarlosCanto,
>> is executeMenuCommand giving you issues?
not at all—indeed it is running quite well and super smooth
I was just curios if it might work without and if there might be a new scripting event...
so in this case your solution is the best one!
Thanks again!
Nils
For those who want to copy the ready-made Script lines:
var docRef = app.activeDocument;
for (i=0; i<docRef.layers.length; i++) {
docRef.layers[i].hasSelectedArtwork = true;
}
Copy link to clipboard
Copied
Hi @Nils M. Barner, it is surprisingly difficult. I've had a crack at it for you—first by using a convenience function I wrote previously itemsInsideGroupItems and passing that a filter function that attempts to filter out anything that cannot be selected. It's largely untested and it will almost certainly fail in some edge cases, but still might be a good start for you. Or maybe there are better options out there.
Here is the script:
/**
* Attempt to Select All without using executeMenuCommand.
* @author m1b
* @discussion https://community.adobe.com/t5/illustrator-discussions/script-for-illustrator-selectall-by-script/m-p/14125111
*/
(function () {
var doc = app.activeDocument;
// set selection to everything that is unlocked and visible
doc.selection = itemsInsideGroupItems(doc.pageItems, unlockedAndVisible, true);
})();
/**
* Returns true if the item, and all of its
* descendents, are unlocked and visible.
* @author m1b
* @version 2023-10-01
* @param {PageItem} item an Illustrator page item.
* @returns {Boolean}
*/
function unlockedAndVisible(item) {
if (item.hasOwnProperty('pageItems'))
for (var i = 0; i < item.pageItems.length; i++)
if (!unlockedAndVisible(item.pageItems[i]))
return false;
return (
item.locked == false
&& item.hidden == false
);
};
/**
* Gets PageItems, excluding GroupItems
* from `items` supplied. Will include
* items inside GroupItems.
* @author m1b
* @version 2023-09-20
* Example of filter function:
* function (item, level) { return item.height > 100 };
*
* @param {Array|collection} items - an array or collection of Illustrator PageItems.
* @param {Function} [filter] - function that returns boolean, given item and level.
* @param {Boolean} [includeGroupItems] - whether to include GroupItems in the result (default: false).
* @param {Boolean} [returnFirstMatch] - whether to return just the first item found (default: false).
* @param {Number} [level] - the level of recursion (internal use).
* @returns {Array|PageItem} - array of PageItems (or PageItem if `returnFirstFound`).
*/
function itemsInsideGroupItems(items, filter, includeGroupItems, returnFirstMatch, level) {
var found = [];
items = items || [];
filter = filter || function () { return true };
includeGroupItems = includeGroupItems === true;
returnFirstMatch = returnFirstMatch === true;
level = level || 0;
for (var i = 0; i < items.length; i++) {
var item = items[i];
if (
item.uuid == undefined
|| item.typename == undefined
)
continue;
if (
item.typename == 'GroupItem'
&& !includeGroupItems
)
continue;
if (
filter == undefined
|| filter(item, level) == true
)
// add the item
found.push(item);
if (item.typename == 'GroupItem')
// look inside the group
found = found.concat(itemsInsideGroupItems(item.pageItems, filter, includeGroupItems, returnFirstMatch, level + 1));
}
if (
found.length > 0
&& returnFirstMatch
)
found = [found[0]];
if (level == 0)
return returnFirstMatch ? found[0] || undefined : removeDuplicateItems(found)
else
return returnFirstMatch ? found[0] || undefined : found;
};
/**
* Returns array of PageItems
* with no duplicate items.
* @author m1b
* @version 2022-07-25 - Mittens.js
* @param {Array|collection} items - an array or collection of PageItems.
* @returns {Array}
*/
function removeDuplicateItems(items) {
var found = [],
uuids = {};
for (var i = 0; i < items.length; i++)
if (uuids[items[i].uuid] == undefined) {
uuids[items[i].uuid] = 1;
found.push(items[i]);
}
return found;
};
Warning: it doesn't behave the same way as the menu command in this particular: if you have a group that contains locked or hidden items, then select all menu command will still select the group, and moving the group will move the children, even the locked ones. I could not get this to work with my script. If you don't expect to have anything locked or hidden, then things are easier. in fact, if you know nothing is locked or hidden, it is *much* easier, so let me know if that's the case.
- Mark
Copy link to clipboard
Copied
hi m1b,
thank you very much for your solution, but this is far too complicate 😉
Copy link to clipboard
Copied
is executeMenuCommand giving you issues?
another easy way is to loop through your layers and turn hasSelectedArtwork property to true
all hidden and locked items will get selected
Copy link to clipboard
Copied
Hey @CarlosCanto, we are forming a habit:
1. First I write a ridiculously long script that *almost* works right, and then
2. Carlos says how to do it in one line that does the job properly!
😂
Thanks Carlos. I always learn from you.
- Mark
Copy link to clipboard
Copied
Copy link to clipboard
Copied
hi CarlosCanto,
>> is executeMenuCommand giving you issues?
not at all—indeed it is running quite well and super smooth
I was just curios if it might work without and if there might be a new scripting event...
so in this case your solution is the best one!
Thanks again!
Nils
For those who want to copy the ready-made Script lines:
var docRef = app.activeDocument;
for (i=0; i<docRef.layers.length; i++) {
docRef.layers[i].hasSelectedArtwork = true;
}