Skip to main content
Inspiring
June 19, 2022
Answered

Javascript to switch to another open document based on string

Hi all,

Is it possible to make a document active by using a regex search? (this script doesn't work but is something like this possible?)

var A4 = app.documents.itemByName(.*-A4);
app.activeDocument = app.documents.itemByName(A4); //Switch to source doc containing -A4 in filename

Many thanks

This topic is closed to new replies. Start a new post to keep the conversation going.
m1b
m1bCorrect answer
Community Expert
June 20, 2022

Finally able to take a look at your reply. Unfortunately, I can't get it to work. I'm struggling to make sense of it.

The error is gone but it doesn't switch to the doc with -A4 in the name. Should it? 

getDocumentByRegex(/(-A4)/g);

function getDocumentByRegex(regex) {
    for (var i = 0; i < app.documents.length; i++)
        var doc = app.documents[i];
        if (regex.test(doc.name))
            return doc;
 }

 


Hi @JustyR, the first problem is I left out a pair of braces in my function code earlier—I typed it on my phone away from my computer—so it wasn't working right.

The second issue is that the function only gets the document. You then need to make it the active document. See the whole thing here:

var myA4Doc = getDocumentByRegex(/-A4/g);

if (myA4Doc != undefined)
    app.activeDocument = myA4Doc;

function getDocumentByRegex(regex) {
    for (var i = 0; i < app.documents.length; i++) {
        var doc = app.documents[i];
        if (regex.test(doc.name))
            return doc;
    }
}

So first we get the document (myA4Doc) and then—if it was found—we set the activeDocument to be it.

- Mark

8 replies

JustyRAuthor
Inspiring
June 19, 2022

I've worked out the search bit, so the switch will be easy to add in due course:

var firstDoc = app.documents[0].name; //first open and active document
var secondDoc = app.documents[1].name; //second open document

	if (firstDoc.match(/(-A4)/g)) {
        alert ("Filename contains '-A4'!");
	}
        
    else {
    alert ("Filename does NOT contain '-A4");
    }
Translate
m1b
Community Expert
Community Expert
June 19, 2022

Something like this (I'm not at computer so can't test):

 

function getDocumentByRegex(regex) {
    for (var i = 0; i < app.documents.length; i++) {
        var doc = app.documents[i];
        if (regex.test(doc.name))
            return doc;
    }
}

 

EDIT: left out a pair of braces on the code. Fixed now.

Translate
JustyRAuthor
Inspiring
June 19, 2022

Thanks for helping. I have so much to learn.

I can see how this would work but I don't know where to put my regex "/-A4/" or in what format. I'm assuming it goes in line 1, yes?

It keeps throwing an error on line 8, undefined is not an object "if (regex.test(doc.name))"

var regex = (/(-A4)/g);

getDocumentByRegex();

function getDocumentByRegex(regex) {
    for (var i = 0; i < app.documents.length; i++)
        var doc = app.documents[i];
        if (regex.test(doc.name))
            return doc;
 }

 

 

Translate