Skip to main content
et3d_
Known Participant
July 19, 2017
Answered

Javascript for Illustrator : Copy a document on another specific document issue

  • July 19, 2017
  • 3 replies
  • 6424 views

Hi,

I've managed to select elements on a source file but it fails when copying. The selection should be copied on targetFile.

There is like 5 to 10 files by sourceFolder (there is an error in opening each file but this is not the matter for now - only the first one is openend)

var targetFile = app.documents.add();                                   //this is my output file - it works

folder = Folder.myDocuments;                                               //this paragraph works for now

sourceFolder = folder.selectDlg("source");

for ( i = 0; i < files.length; i++ ){

     var sourceDoc = app.open(files);

     var doc = app.activeDocument;

     for (i = 0; i < doc.pageItems.length; i++) {

                doc.pageItems.selected = true;

    }

     var mySel = app.activeDocument.selection;          //this paragraph need rework

     newItem = mySel[0].duplicate();                              //mysel.duplicate() is not a function

}

Thanks I copy a document, resize it and place it then I print the output

This topic has been closed for replies.
Correct answer Disposition_Dev

Ok. So the main issue is that you're attempting to duplicate the entire array (which you can't do with the duplicate method).

You need to either group the selection together, then duplicate the group, or run another loop to duplicate each item in the selection array.

As a proof of concept, try using this line in place of the line you've put in bold:

newItem = app.activeDocument.selection[0].duplicate(targetFile);

This should give you the first element of the array duplicated into the new file.

3 replies

Participating Frequently
September 29, 2017

I want to save the document in JPG/PNG format with custom text and color rectangle on it.

I did it successfully in PS extension but struggling with AI.

If you want I can share the code with you

Disposition_Dev
Disposition_DevCorrect answer
Legend
July 20, 2017

Ok. So the main issue is that you're attempting to duplicate the entire array (which you can't do with the duplicate method).

You need to either group the selection together, then duplicate the group, or run another loop to duplicate each item in the selection array.

As a proof of concept, try using this line in place of the line you've put in bold:

newItem = app.activeDocument.selection[0].duplicate(targetFile);

This should give you the first element of the array duplicated into the new file.

et3d_
et3d_Author
Known Participant
July 20, 2017

You might have solved my issue, I'm gonna try tomorrow. So let's groupe it and see.

Anyway I can use action script to rebuild layers but I just care for image layer and cut layer so grouping is perfectly fine (cut layer use a specific ccolor and thickness)

I guess that resize should works the same way?

offtopic : I'm thinking about using other opensource source softwares for those specific tasks on pdf because scripting is very technical, there should be methods for all shorcut cmd like CTRL A / CTRL V... I don't know this seems wierd.

EDIT: I can't manage to copy group using duplicate:

javascript:;  wrote

var mySel = app.activeDocument.selection;

                var newItem = app.activeWindow.activeSpread.groups.add(mySel);

                // New Coords for dupicated object

                var newX = (85.5*(j - 1) -50)*2.83465;

                var newY = (280*(k - 1) -500)*2.83465;

                newitem.duplicate(targetFile); //CURRENT ERROR

                newItem.position = [newX,newY];

                mySel.resize(0.07, 0.07);

INSTEAD, I've found a working copy method using app.executeMenuCommand() (which is obviously simple)

javascript:;  wrote

var mySel = app.activeDocument.selection;

                app.executeMenuCommand('copy');

                targetFile.activate();

                newItem = app.executeMenuCommand('paste');

               // New Coords for dupicated object

                var newX = (85.5*(j - 1) -50)*2.83465;

                var newY = (280*(k - 1) -500)*2.83465;

                newItem.position = [newX,newY];                //stuck here now

                mySel.resize(0.07, 0.07);

https://forums.adobe.com/thread/2088728

OFFTOPIC EDIT: My algo is "wrong" for building the pattern, it will paste overlapped same image 50 times, so it will have 50*50 images. Hopefully this script don't work yet otherwise the computer would proceed for more than a day ( :

Disposition_Dev
Legend
July 21, 2017

Ok. your syntax is correct for the position property, the problem you have now is that you're setting the variable "newItem" equal to the result of the method "executeMenuCommand('paste')".

This method does not return a value, so newItem remains undefined. What you need to do is simply call the paste command, then save the current selection to the "newItem" variable and then you can change the position. like so:

var mySel = app.activeDocument.selection;

app.executeMenuCommand("copy");

targetFile.activate();

app.executeMenuCommand("paste");

var newItem = targetFile.selection[0];

var newX = ( 85.5 * (j-1) - 50 ) * 2.83465;

var newY = ( 280 * (k-1) - 500 ) * 2.83465

newItem.position = [newX,newY];

//my guess is this last line is not correct.. I imagine you want to resize newItem, not the original selection.

//If it is indeed the original selection you want to resize, you'll need to group it before you can resize it.

//resize function can't be used on an array.

mySel.resize(0.07,0.07);

Disposition_Dev
Legend
July 19, 2017

So i just typed out this whole thing about how the wrong document was probably active and you weren't actually getting any selection. then i re-read your code and realized i'm dumb. that's not the issue.

now that i'm looking at it, the issue you may be having is that you're reusing the same loop variable in nested loops. change the loop variable for the inner loop from 'i' to 'j' and see if that changes anything.

It also seems that you're using a variable called 'files' that doesn't appear to have been declared anywhere? is this just a snippet of a larger script?

et3d_
et3d_Author
Known Participant
July 19, 2017

Yes thanks you are totally right and this is completely obvious I need to review my code more often
I will check that tomorrow.

yes files is used to open the same pattern of extension here: pdf

Then is the duplicate() thing used correctly?

Disposition_Dev
Legend
July 19, 2017

understood. but files was never declared/defined. if you want to get all of the files in a folder you'll actually have to declare a variable 'files' and set it equal to the resulting array from the function '[folder].getFiles()'.

So for your case you should use the following line:

var files = sourceFolder.getFiles();

getFiles() returns an array of file objects that you can work with.