Skip to main content
wckdtall
Inspiring
May 15, 2018
Question

How do I cut and paste a group to a new document via actions?

  • May 15, 2018
  • 1 reply
  • 1586 views

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.

This topic has been closed for replies.

1 reply

wckdtall
wckdtallAuthor
Inspiring
May 16, 2018

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!

Silly-V
Legend
May 16, 2018

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.

wckdtall
wckdtallAuthor
Inspiring
May 16, 2018

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...


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();

}