Copy link to clipboard
Copied
With js scripting is there away to select all the artboard items. Means everything text, shapes etc. and delete?
I tried
app.activeDocument.selection = true;
But that didn't do anything.
Copy link to clipboard
Copied
To select all items:
//select all items, by selecting each item individually, thru a loop
for (i = 0; i < activeDocument.pageItems.length; i++) {
activeDocument.pageItems[i].selected = true;
}
To delete all items, without needing to select them:
//delete all items
activeDocument.pageItems.removeAll()
I use CS6. I don't know if this works in CC. For example, I don't need to make app. explicit.
Copy link to clipboard
Copied
wouldnt it be easier to use an action? The script above - will it select locked items? you can create an action that will do it all.
Copy link to clipboard
Copied
If the goal is to simply select everything and delete, then an action would do fine. But assuming you only want to delete stuff that's on the artboard (and leave anything on the drawing area alone), an action won't suffice unfortunately. Actions can't conditionally decide what's on the artboard and what isn't.
It is possible to use "Insert Menu Item" to hit "Select All On Active Artboard", but if there is more than one artboard, there's no way for an action to decide which is the desired one.
Another potential drawback of actions is that I don't believe you can lock/unlock layers with an action. So if there are mutltiple layers in the document and one or more of them are locked, there's no way to unlock them afaik. You could do "Unlock All" via action, but that only works for objects, not layers.
The script above does not select or remove locked/hidden art, but the logic required to do so reliably is trivial and the results would be more predictable than with actions.
Copy link to clipboard
Copied
give this a shot. I'm assuming you mean you want to delete everything in the document. If you only want to delete stuff that's on the artboard, let me know and we can work that in as well. This will ensure that everything is unlocked and visible, and will then delete all pageItems per @femkeblanco's suggestion of the removeAll() method. If you want to leave stuff that's locked/hidden alone, then you can use femkeblanco's second option as it is.
#target Illustrator
//Description: reveal all pageItems and layers, then delete all pageItems
function test()
{
var docRef = app.activeDocument;
var layers = docRef.layers;
for(var l=0,len=layers.length;l<len;l++)
{
layers[l].locked = false;
layers[l].visible = true;
}
app.executeMenuCommand("unlockAll");
app.executeMenuCommand("showAll");
docRef.pageItems.removeAll();
}
test();