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

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

206

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

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

Votes

Translate

Translate
Community Expert , 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
...

Votes

Translate

Translate
Adobe
Community Expert ,
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

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

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

Copy link to clipboard

Copied

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

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

 

 

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

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

Copy link to clipboard

Copied

LATEST

Very good @Stephen_A_Marsh ! Thanks for sharing

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