Copy link to clipboard
Copied
Hi,
I'm beginning with this so I apologise in advance if this seems trivial. I'm struggling to script the placement of an image into a frame using JavaScript for InDesign CC 2017 (ExtendScript Toolkit). On my last line, I get "object is invalid" for the frame - which I've labelled with a script lael "Image1" in the InDesign file.
Any help would be really appreciated where it seems in terms of syntax I am doing the same as a number of tutorials out there.
var folder = "/c/Users/d_a_g/OneDrive/Work/InDesign/ContentTest/"
var myFolder = Folder(folder),
myTemplate = File(myFolder + "/ContentTemplate.indd"),
//myPics = myFolder.getFiles("*.jpg"),
newDoc = app.open(myTemplate),
myDoc = app.activeDocument,
myPages = myDoc.pages,
i = 0,
lastPage = 0;
firstPage = myDoc.pages.item(0)
firstFrame = firstPage.rectangles.item("Image1");
$.writeln(firstPage);
$.writeln(firstFrame);
var myGraphicFile = File.openDialog ("Choose a file");
$.writeln(myGraphicFile);
firstFrame.place(myGraphicFile);
Try this:
...var main = function() {
var folder = "/c/Users/d_a_g/OneDrive/Work/InDesign/ContentTest/",
myTemplate, newDoc, firstPage, myGraphicFile;
//Checking folder existence
if ( !folder.exists ) {
alert(folder.fsName+" doesn't exist" );
return;
}
myTemplate = File(myFolder + "/ContentTemplate.indd"),
//Checking myTemplate existence
if ( !myTemplate.exists ) {
alert(myTemplate.fsName+" doesn't exist" );
return;
}
//Referencing newDoc as the document being opened i.e. the activeDocu
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Apologies - when I searched to find this - I couldn't initially find it. Thanks for the link. Will put any future posts there.
Copy link to clipboard
Copied
Try this:
var main = function() {
var folder = "/c/Users/d_a_g/OneDrive/Work/InDesign/ContentTest/",
myTemplate, newDoc, firstPage, myGraphicFile;
//Checking folder existence
if ( !folder.exists ) {
alert(folder.fsName+" doesn't exist" );
return;
}
myTemplate = File(myFolder + "/ContentTemplate.indd"),
//Checking myTemplate existence
if ( !myTemplate.exists ) {
alert(myTemplate.fsName+" doesn't exist" );
return;
}
//Referencing newDoc as the document being opened i.e. the activeDocument
newDoc = app.open(myTemplate),
firstPage = newDoc.pages[0];
//Referencing the first frame
firstFrame = firstPage.rectangles[0];
//Checking firstFrame validity
if ( !firstFrame.isValid ) {
alert(firstFrame+" doesn't exist" );
doc.close(SaveOptions.NO);
return;
}
myGraphicFile = File.openDialog ("Choose a file");
//Exit if no file opened
if ( !myGraphicFile ) return;
//Placing the image
firstFrame.place(myGraphicFile);
}
main();
HTH
Loic