Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Enthusiast ,
May 15, 2018 May 15, 2018

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.

TOPICS
Scripting
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Enthusiast ,
May 15, 2018 May 15, 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 15, 2018 May 15, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 15, 2018 May 15, 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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 16, 2018 May 16, 2018
LATEST

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

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines