• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Javascript to switch to another open document based on string

Contributor ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

387

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 20, 2022 Jun 20, 2022

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++) {
        va
...

Votes

Translate

Translate
Contributor ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

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;
 }

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

You would send the regex as the argument for the function:

 

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

 

Note that this will find only the first document with -A4 in name. If you want to find multiple documents, you need to push them into an array inside the function, and return that array.

- Mark

 

P.S. I used the test method of RegExp, rather than the match method of String as you did. For this purpose using match was just fine, I only used test because it simply returns a Boolean which is logically what we are using. match works fine too because it returns a non-zero-ish result which is automatically coerced into Boolean.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 20, 2022 Jun 20, 2022

Copy link to clipboard

Copied

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;
 }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 20, 2022 Jun 20, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 21, 2022 Jun 21, 2022

Copy link to clipboard

Copied

Thanks for explaining it Mark. I wasn't quite sure what Return meant. Now I see it's to return the result rather than have InDesign show the doc. 
I love how the function is being used in a variable. I didn't know that was possible. 
All the best, J

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 21, 2022 Jun 21, 2022

Copy link to clipboard

Copied

LATEST

Yeah sorry, I didn't mean to make you chase the answer—I just didn't remember what your scripting knowledge was so I wasn't putting it all together.

 

The return statment appears inside a function block and it sends the flow back out to the point where the function was called. But if the function returns something then that returned thing is left behind where the function call was made. So if the function returns a Document object, and the script assigns the function call to a variable, then the returned Document object is assigned to that variable. If the function doesn't find any documents with /-A4/, then it returns nothing (a Function always implicitly returns undefined at the end of it if no explicit return statement is made). So that's why in the code above we have to check if myA4doc is undefined, before trying to use it.

- Mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines