Copy link to clipboard
Copied
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
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
The complete code:
javascript:; wrote
#target Illustrator-21
// Main Code [Execution of script begins here]
// uncomment to suppress Illustrator warning dialogs
// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var scriptID = "stack v0.1";
var destFolder, sourceFolder, files, fileType, sourceDoc, doc, targetFile, mySel;
var targetFile = app.documents.add(); // upcoming ERROR: CMYK instead of RGB
// Pre-defined starting point
folder = Folder.myDocuments;
sourceFolder = folder.selectDlg("source");
alert (sourceFolder);
// If a valid folder is selected
if(sourceFolder != null){
files = new Array();
fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', '*.pdf' );
// Get all files matching the pattern
files = sourceFolder.getFiles( fileType );
if ( files.length > 0 ){ // number of files
for ( j = 1; j <= 13; j++ ){ // horizontal grid
for ( k = 1; k <= 4; k++ ){ // vertical grid
for ( i = 0; i < files.length; i++ ){ // iterate through files num
var sourceDoc = app.open(files); // returns the document object
var doc = app.activeDocument;
for (l = 0; l < doc.pageItems.length-1; l++) {
doc.pageItems
.selected = true; //alert(scriptID + "\nSelection was empty. Did select " + doc.selection.length.toString() + " page items");
}
if ( activeDocument.selection.length > 0 ) {
var mySel = app.activeDocument.selection;
// New Coords for dupicated object
newX = (85.5*(j - 1) -50)*2.83465;
newY = (280*(k - 1) -500)*2.83465;
newItem = app.activeDocument.selection.duplicate(targetFile); //CURRENT ERROR
newItem.position = [newX,newY];
newItem.resize(0.07, 0.07);
}else{
alert( 'empty selection' );
}
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
var doc = null
alert( 'Files are saved as xxx in ' + destFolder );
}
//else{
// alert( 'No matching files found' );
//}
}
}
}
}
Found link about duplicate() but I don't understand any further https://forums.adobe.com/thread/288171
code:
Sanat wrote
Code :
main();
function main(){
if (app.documents.length != 0){
if (app.selection.length > 0){
//myDocument = app.documents.add();
var myObjectList = new Array;
var myObjectList1 = new Array;
for (i=0; i<app.selection.length; i++)
myObjectList.push (app.selection);
//alert ("length of myObjectList is "+myObjectList.length);
var myGroup = app.activeDocument.groups.add(myObjectList);
var myGroup1=myGroup.duplicate(myGroup1,[22,2]);<br />myGroup1.absoluteHorizontalScale=150;
myGroup1.absoluteVerticalScale=150;
}
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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 ( :
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
When you use
var newItem = targetFile.selection[0];
that mean that first item of targetFile.selection array can be moved. But each item as a relative position in the image, meaning that if it is moved separately the image would change of form (this is a composition of paths with true png picture)
So I would rather use group (as you've said) it to move it as a whole ( :
thanks anyway I really feel ot is going further each day. But as it's time consuming (I don't have estk or Ai at home) I have to do it by hand (the copy resize thing) using silly action script for the most part ahah
Copy link to clipboard
Copied
Hi William,
I'm also facing the issue in this line:
var templateDoc = app.open(new File(extPath + '/images/zcdp_palette_template_land.ai'));
var dupeDoc = templateDoc.duplicate(filePath.match(/[^\/]+$/)[0]);
templateDoc.close(SaveOptions.DONOTSAVECHANGES);
//error: templateDoc.duplicate is not a function
Copy link to clipboard
Copied
Does duplicate() accepts documents or a document? I don't think so. For sure it works with single pageItem
You may want to look for these threads and head for a saveAs. It must already exists as scripting in js works for years now
For a quick solution you can use Adobe's sample "Save as PDFs" it's in the program files, on CS5 it was
C:\Program Files (x86)\Adobe\Adobe Illustrator CS5\Scripting\Sample Scripts\JavaScript\Miscellaneous
Copy link to clipboard
Copied
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