Checking current document name

Engaged ,
Jun 11, 2022 Jun 11, 2022

Copy link to clipboard

Copied

Hello guys! How do I check if there is a specific name at the beginning or end of the current document name?
It would be something like this:
"test myDocumente.jpg" or "myDocumente test.jpg"

TOPICS
Actions and scripting

Views

62

Likes

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 2 Correct answers

Adobe Community Professional , Jun 11, 2022 Jun 11, 2022

Edited 

I missed that both at the beginnang and at the end should qualify. 

I expect one RegExp would be possible but two seems easier. 

var theRegExp = new RegExp(/^test/i);
var theRegExp2 = new RegExp(/test$/i);
var docName = activeDocument.name;
try {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
catch (e) {basename = docName};
if (basename.match(theRegExp) != null || basename.match(theRegExp2) != null) {alert ("yes")}
else {alert ("no")};

Likes

Translate

Translate
Adobe Community Professional , Jun 12, 2022 Jun 12, 2022

Some other common minor variations include:

 

var docName = activeDocument.name.replace(/\.[^\.]+$/, '');
if (/ test$|^test /i.test(docName) === true) {
        alert("True");
    } else {
        alert("False");
}

 

var docName = activeDocument.name.replace(/\.[^\.]+$/, '');
if (docName.match(/ test$|^test /i)) {
        alert("True");
    } else {
        alert("False");
}

 

var docName = activeDocument.name.split('.')[0];
if (docName.match(/ test$|^test /i)) {
        alert("True");
    } els
...

Likes

Translate

Translate
Adobe Community Professional ,
Jun 11, 2022 Jun 11, 2022

Copy link to clipboard

Copied

Hi, what do you mean it should save with that name?

Ali Sajjad / Graphic Design Trainer / Freelancer / Adobe Certified Professional

Likes

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
Adobe Community Professional ,
Jun 11, 2022 Jun 11, 2022

Copy link to clipboard

Copied

Edited 

I missed that both at the beginnang and at the end should qualify. 

I expect one RegExp would be possible but two seems easier. 

var theRegExp = new RegExp(/^test/i);
var theRegExp2 = new RegExp(/test$/i);
var docName = activeDocument.name;
try {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
catch (e) {basename = docName};
if (basename.match(theRegExp) != null || basename.match(theRegExp2) != null) {alert ("yes")}
else {alert ("no")};

Likes

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
Engaged ,
Jun 11, 2022 Jun 11, 2022

Copy link to clipboard

Copied

Perfect, that's exactly what I needed. Thanks @c.pfaffenbichler 

Likes

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
Adobe Community Professional ,
Jun 11, 2022 Jun 11, 2022

Copy link to clipboard

Copied


@c.pfaffenbichler wrote:

I expect one RegExp would be possible but two seems easier.

 

Here it is in a single regular expression, I included the leading/trailing white space to tighten up the search:

 

var theRegExp = new RegExp(/ test$|^test /i);
var docName = activeDocument.name;
try {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
catch (e) {basename = docName}
if (basename.match(theRegExp) !== null) {alert ("yes")}
else {alert ("no")}

 

 

Likes

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
Adobe Community Professional ,
Jun 12, 2022 Jun 12, 2022

Copy link to clipboard

Copied

Some other common minor variations include:

 

var docName = activeDocument.name.replace(/\.[^\.]+$/, '');
if (/ test$|^test /i.test(docName) === true) {
        alert("True");
    } else {
        alert("False");
}

 

var docName = activeDocument.name.replace(/\.[^\.]+$/, '');
if (docName.match(/ test$|^test /i)) {
        alert("True");
    } else {
        alert("False");
}

 

var docName = activeDocument.name.split('.')[0];
if (docName.match(/ test$|^test /i)) {
        alert("True");
    } else {
        alert("False");
}

Likes

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
Engaged ,
Jun 13, 2022 Jun 13, 2022

Copy link to clipboard

Copied

LATEST

Very good @Stephen_A_Marsh ! Thanks for sharing

Likes

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