Skip to main content
Inspiring
June 19, 2022
Answered

Javascript to switch to another open document based on string

  • June 19, 2022
  • 1 reply
  • 1398 views

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 has been closed for replies.
Correct answer m1b

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

1 reply

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");
    }
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.

m1b
Community Expert
m1bCommunity ExpertCorrect 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