Ah I see now. It is the v1 or v2 that is a problem here. You are now saying that it's okay to match a v2 with a v1 document, so that's likely why my script didn't find it. Here's an updated version. I just added a new argument: ignoreThis, which is a regex that will be completely ignored (it'll simply be removed before the comparison) so now it'll match documents that have differing version numbers with this regex /_v\d+/ . I hope I've understood now!
- Mark
var doc = app.activeDocument,
myA4Doc = getDocumentByNameFindChange(doc, /-US/, '-A4', /_v\d+/);
if (myA4Doc != undefined)
app.activeDocument = myA4Doc;
/**
* Make a document active, based on its
* name, relative to another document.
*
* @9397041 {activeDoc} activeDoc - a Document
* @9397041 {RegExp} findWhat - regex to find with
* @9397041 {String} changeTo - text to change to
* @9397041 {RegExp} ignoreThis - regex to ignore
* @Returns {Document} the matched Document
*/
function getDocumentByNameFindChange(activeDoc, findWhat, changeTo, ignoreThis) {
// store a version of the active doc's name
// but with the replacement text
var findThisName = activeDoc.name
.replace(findWhat, changeTo)
// and remove the ignored part
.replace(ignoreThis, '');
for (var i = 0; i < app.documents.length; i++) {
var doc = app.documents[i];
// don't want activeDoc
if (doc === activeDoc)
continue;
// check if name matches (minus the ignored part)
if (doc.name.replace(ignoreThis, '') == findThisName)
return doc;
}
// if flow gets to here
// no document was found
// so return nothing
}
And here's another version that will toggle back and forth between A4 and US versions. You configure it with the two arrays you pass as arguments. findWhat and changeTo can be arrays in this version.
- Mark
var doc = app.activeDocument,
myA4Doc = getDocumentByNameFindChange(doc, [/-US/, /-A4/], ['-A4', '-US'], /_v\d+/);
if (myA4Doc != undefined)
app.activeDocument = myA4Doc;
/**
* Make a document active, based on its
* name, relative to another document.
*
* findWhat and changeTo can be Arrays
* such that if findWhat[i] matches active
* document name then switch to document
* named after doing changeTo[i].
* @9397041 {activeDoc} activeDoc - a Document
* @9397041 {Array[RegExp]} findWhat - regex to find with
* @9397041 {Array[String]} changeTo - text to change to
* @9397041 {RegExp} ignoreThis - regex to ignore
* @Returns {Document} the matched Document
*/
function getDocumentByNameFindChange(activeDoc, findWhat, changeTo, ignoreThis) {
var activeName = activeDoc.name,
index = 0;
// work out which findWhat is in document
if (findWhat.hasOwnProperty('0')) {
for (index = 0; index < findWhat.length; index++)
if (findWhat[index].test(activeName))
break;
}
else {
// just put them in an array so we
// can handle them the same later
findWhat = [findWhat];
changeTo = [changeTo]
}
// store a version of the active doc's name
// but with the replacement text
var findThisName = activeDoc.name
.replace(findWhat[index], changeTo[index])
// and remove the ignored part
.replace(ignoreThis, '');
for (var i = 0; i < app.documents.length; i++) {
var doc = app.documents[i];
// don't want activeDoc
if (doc === activeDoc)
continue;
// check if name matches (minus the ignored part)
if (doc.name.replace(ignoreThis, '') == findThisName)
return doc;
}
// if flow gets to here
// no document was found
// so return nothing
}