Make active document by name

I’m trying to figure out how to check to see if a document is open, and if it is to make it the active one. I don’t know how to check the open documents titles to get their current _id. I can loop through them and check their title and this works, although ideally there is a better solution. Current code is below

async function testCode1() {

   if (app.activeDocument.title != "3D_temp.tif") {

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

         app.activeDocument = app.documents[i]

         if (app.activeDocument.title == "3D_temp.tif") {

            return

         } 

      }

   }

}

Hi, if I understand correctly you would like to select the document by the name, try this way :

async function selectByName(name){

    const batchPlay = require("photoshop").action.batchPlay;

    const result= await batchPlay(

    [

       {

          "_obj": "select",

          "_target": [

             {

                "_ref": "document",

                "_name": name

             }

          ],

        

          "_isCommand": true,

          "_options": {

             "dialogOptions": "silent"

          }

       }

    ],{

       "synchronousExecution": false,

       "modalBehavior": "fail"

    });

    if(result[0].message!=undefined){

    console.log("the document does not exist")
    }else{}
    }

    selectByName(name)

Amazing, thank you so much. So obvious once you point it out.