Skip to main content
Inspiring
August 19, 2015
Beantwortet

Checking if an object exists on a page

  • August 19, 2015
  • 3 Antworten
  • 4693 Ansichten

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

Dieses Thema wurde für Antworten geschlossen.
Beste Antwort von Laubender

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

3 Antworten

Peter Kahrel
Community Expert
Community Expert
August 19, 2015

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

LaubenderCommunity ExpertAntwort
Community Expert
August 19, 2015

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

Inspiring
August 19, 2015

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

Peter Kahrel
Community Expert
Community Expert
August 19, 2015

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

Vamitul
Legend
August 19, 2015

Please post your entire script and let's see if we can make it work. Your current snippet makes little to no sense.