Copy link to clipboard
Copied
I wrote a script to cut and paste groups in the first layer of a document, into new documents to isolate each group. While it works great via ESTK, and running as a script menu item, it cuts the groups from the original document, but will not paste them into newly created documents when running this menu item through an action. Is there a way to get this to run properly?
origDoc = app.activeDocument;
length = origDoc.layers[0].groupItems.length;
for(i=length; i>0; i--){
origDoc.activate();
app.executeMenuCommand('deselectall');
origDoc.layers[0].groupItems[i-1].selected = true;
app.cut();
newDoc = app.documents.add();
newDoc.activate();
app.paste();
}
Skipping the loops, even trying to run it only on the first group in a layer will not work as an action:
origDoc = app.activeDocument;
origDoc.activate();
app.executeMenuCommand('deselectall');
origDoc.layers[0].groupItems[0].selected = true;
app.cut();
newDoc = app.documents.add();
app.paste();
The other odd behavior is that the original document still seems to have items within the group selected after running the script even if I try to deselect all. Running the script again, the new documents show up is empty.
Copy link to clipboard
Copied
It appears this may work just as well without having to rely on cutting and pasting:
origDoc = app.activeDocument;
length = origDoc.layers[0].groupItems.length;
for(i=length; i>0; i--){newDoc = app.documents.add();
origDoc.layers[0].groupItems[i-1].duplicate(newDoc);
}
If anyone has any other insight please share!
Copy link to clipboard
Copied
Indeed, duplicating appears to be the superior method to copy items over between documents, in more ways than one.
I had an experience where during my routine, the application crashed for an unknown reason at the end of execution. After many hours I was able to trace the problem to the app.copy and .paste() methods I was using. Thereafter, I replaced those lines with the .duplicate command and the error was solved.
Copy link to clipboard
Copied
Thanks for the confirmation! I still have a hurdle left to figure out, as I have some functions that save and close the new files that are causing Illustrator to crash. The fun never stops...
Copy link to clipboard
Copied
For the crash issue I had mentioned, I found after saving a document and closing via the batch dialog playing an action, I got a crash. After a bit of experimentation, I was able to stop the crash by changing the order of documents closing:
//Close the original doc first to prevent crashing, moving this below this close loop causes a crash when running a script through an action.
origDoc.close(SaveOptions.DONOTSAVECHANGES);
//In previous loop, add new document names to an array to close after saves are complete
for(i=0; i<closingTime.length; i++){
app.documents.getByName(closingTime).close();
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now