Copy link to clipboard
Copied
Hi,
I'm trying to automate adding commonly-used elements on existing documents and I want to be able to check if an object already exists on the page so that it doesn't get added twice. I'm pretty new to JavaScript but pretty confident in Indesign CC.
Here is the script fragment that I want to do the job:
// add address window
if(!doesObjectExist(myAddressWindow)){
var myAddressWindow=myDocument.pages.item(0).textFrames.add();
var myCorners=CornerOptions.ROUNDED_CORNER;
myAddressWindow.properties={name:"myAddressWindow",geometricBounds:["33mm", "20mm", "78mm", "110mm"],strokeWeight:0.5,strokeColor:"Magenta",topLeftCornerOption:myCorners,topRightCornerOption:myCorners,bottomLeftCornerOption:myCorners,bottomRightCornerOption:myCorners};
}
function doesObjectExist(objectName){
with(myDocument.pages.everyItem){
if(name==objectName){
return true;
}
}
the myAddressWindow element always get added, even if it already exists and I can't figure out why.
Thanks and regards,
Malcolm
1 Correct answer
Hi Malcom,
you could use itemByName() on the pageItems in the document, if that element you are looking for is not nested ( inside a group or inside a graphic frame or anchored to a text frame etc.pp).:
...testSomething("TheNameOfTheThing");
function testSomething(/*String*/myName){
if( app.documents[0].pageItems.itemByName(myName).isValid ){
//Nothing to do:
$.writeln("Nothing to do.")
return;
};
else{ doSomething() };
};
function doSomething(){
Copy link to clipboard
Copied
Please post your entire script and let's see if we can make it work. Your current snippet makes little to no sense.
Copy link to clipboard
Copied
Hi Malcom,
you could use itemByName() on the pageItems in the document, if that element you are looking for is not nested ( inside a group or inside a graphic frame or anchored to a text frame etc.pp).:
testSomething("TheNameOfTheThing");
function testSomething(/*String*/myName){
if( app.documents[0].pageItems.itemByName(myName).isValid ){
//Nothing to do:
$.writeln("Nothing to do.")
return;
};
else{ doSomething() };
};
function doSomething(){
/* Your code for adding something should follow here: */
$.writeln("Build something.");
};
You could also test for is not valid, and do something, if true.
The above code is just an exmple not optimized for anything.
Uwe
Copy link to clipboard
Copied
Thanks Uwe - that works well! I think I was confusing myself because I was confusing the Javascript object name with the Indesign object name.
Thanks also to you Peter - both solutions work well, but I would prefer to keep it as a function so that it can be reused to check for any object at any time.
Regards,
Malcolm
Copy link to clipboard
Copied
Malcolm -- With all due respect, you function makes no sense at all. You want to check if a frame occurs on a page, but the function doesObjectExist() inspects -- or so it seems -- all pages. Furthermore, 'name' in the function doesn't refer to anything.
If you want to check any object on a given page rather than just text frames, do
if (!myDocument.pages.item(0).pageItems.item(. . .)
Peter
Copy link to clipboard
Copied
Ha ha no worries Peter - I'm here to learn after all...
Here is the script in its entirety - it's working except for when I try to lock the layer on line 54.
var myDocument=app.documents.item(0);
// set bleed and slug options
with(myDocument.documentPreferences){
documentBleedTopOffset="3mm";
documentBleedUniformSize=true;
slugTopOffset=0;
documentSlugUniformSize=true;
}
removeEmptyLayers();
// add position guide layer (if it doesn't already exist)
try {
myPositionLayer=myDocument.layers.add({name:"WMPosition Guide",printable:false});
}
catch(myError){
};
// add fold lines
var foldLineWeight=0.5;
var foldLineColor="Magenta";
var foldLineType="Dashed";
// add first fold
if(!doesObjectExist("myFirstFold")){
var myFirstFold=myDocument.pages.item(0).graphicLines.add();
myFirstFold.properties={name:"myFirstFold",geometricBounds:["98mm", "-3mm", "98mm", "213mm"],strokeWeight:foldLineWeight,strokeColor:foldLineColor,strokeType:foldLineType};
}
if(!doesObjectExist("mySecondFold")){
var mySecondFold=myDocument.pages.item(0).graphicLines.add();
mySecondFold.properties={name:"mySecondFold",geometricBounds:["198mm", "-3mm", "198mm", "213mm"],strokeWeight:foldLineWeight,strokeColor:foldLineColor,strokeType:foldLineType};
}
// add address window
if(!doesObjectExist("myAddressWindow")){
var myAddressWindow=myDocument.pages.item(0).textFrames.add();
var myCorners=CornerOptions.ROUNDED_CORNER;
// geometricBounds order: top left y,top left x, bottom right y, bottom right x
myAddressWindow.properties={name:"myAddressWindow",geometricBounds:["33mm", "20mm", "78mm", "110mm"],strokeWeight:0.5,strokeColor:"Magenta",topLeftCornerOption:myCorners,topRightCornerOption:myCorners,bottomLeftCornerOption:myCorners,bottomRightCornerOption:myCorners};
}
// lock position guide layer
myDocument.layers.item(myPositionLayer).properties={locked:true};
function removeEmptyLayers(){
var layers=myDocument.layers.everyItem().getElements();
for(var i=layers.length-1;i>0;i--){
if(layers.pageItems.length==0){
layers.remove();
}
}
}
function doesObjectExist(objectName){
if(app.documents[0].pageItems.itemByName(objectName).isValid){
return true;
}
}
The reason that I want to use a function to check for the existence of an object is that I may want to use it again and again for different objects. I like the idea of reusable chunks of code like that.
Regards,
Malcolm
Copy link to clipboard
Copied
Thanks, Laubender, this is very helpful. For many years I've been writing plugins for Mac and Windows, and using AppleScript, and am just starting to work with JS with Visual Studio Code. With AI 2023 the .isValid property doesn't appear in the list when I stop at a breakpoint. Calling "if (myFrame.isValid)" does not get flagged as an error, but is treated as false when I know that the object can indeed be operated on when I skip the "valid" test (getByName found the text frame). Have things changed in recent years?
I have these three extensions installed in Visual Studio Code that I thought should address code completion, debugging, etc.: Adobe Script Runner, ExtendScript, ExtendScript Debugger.
Thanks for any suggestions.
Copy link to clipboard
Copied
Hi @Rick E Johnson ,
as far as I can see .isValid is a property that comes with ExtendScript for InDesign.
Not with ExtendScript for Adobe Illustrator, not with ExtendScript for PhotoShop.
So, nothing new here.
I'd ask that question in the Adobe Illustrator forum:
Regards,
Uwe Laubender
( Adobe Community Expert )
Copy link to clipboard
Copied
I appreciate your getting back to me on that. It makes perfect sense. There’s a lot to like about InDesign’s scriptability and grep support.
Have a great New Year!
— Rick
Copy link to clipboard
Copied
The function you use to check if a frame exists has an error: with(myDocument.pages.everyItem){ should be with(myDocument.pages.everyItem()){
But that function is unnecessary, no need to cycly through all frames on a page. You can do this:
if (!myDocument.pages.item(0).textFrames.item('myAddressWindow').isValid){
var myAddressWindow=myDocument.pages.item(0).textFrames.add();
var myCorners=CornerOptions.ROUNDED_CORNER;
myAddressWindow.properties={name:"myAddressWindow",geometricBounds:["33mm", "20mm", "78mm", "110mm"],strokeWeight:0.5,strokeColor:"Magenta",topLeftCornerOption:myCorners,topRightCornerOption:myCorners,bottomLeftCornerOption:myCorners,bottomRightCornerOption:myCorners};
}
Peter

